blob: e2596f031e6f314d406ccdeb0657fc79729be585 [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";
2865 mFakePolicy->addInputUniqueIdAssociation(DEVICE_NAME, DISPLAY_UNIQUE_ID);
2866 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08002890// --- InputMapperTest ---
2891
2892class InputMapperTest : public testing::Test {
2893protected:
2894 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002895 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002896 static const int32_t DEVICE_ID;
2897 static const int32_t DEVICE_GENERATION;
2898 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002899 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002900 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002901
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002902 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002903 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002904 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002905 std::unique_ptr<InstrumentedInputReader> mReader;
2906 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002907
Chris Ye1b0c7342020-07-28 21:57:03 -07002908 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002909 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002910 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002911 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002912 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002913 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002914 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002915 }
2916
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002917 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002918 SetUp(DEVICE_CLASSES);
2919 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002920
Chris Yea52ade12020-08-27 16:49:20 -07002921 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002922 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002923 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002924 }
2925
2926 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002927 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002928 }
2929
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002930 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002931 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002932 mReader->requestRefreshConfiguration(changes);
2933 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002934 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002935 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2936 }
2937
arthurhungdcef2dc2020-08-11 14:47:50 +08002938 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2939 const std::string& location, int32_t eventHubId,
2940 Flags<InputDeviceClass> classes) {
2941 InputDeviceIdentifier identifier;
2942 identifier.name = name;
2943 identifier.location = location;
2944 std::shared_ptr<InputDevice> device =
2945 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2946 identifier);
2947 mReader->pushNextDevice(device);
2948 mFakeEventHub->addDevice(eventHubId, name, classes);
2949 mReader->loopOnce();
2950 return device;
2951 }
2952
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002953 template <class T, typename... Args>
2954 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002955 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002956 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002957 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002958 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002959 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002960 }
2961
2962 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002963 int32_t orientation, const std::string& uniqueId,
2964 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002965 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2966 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002967 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2968 }
2969
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002970 void clearViewports() {
2971 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002972 }
2973
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002974 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2975 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002976 RawEvent event;
2977 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002978 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002979 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002980 event.type = type;
2981 event.code = code;
2982 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002983 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002984 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002985 }
2986
2987 static void assertMotionRange(const InputDeviceInfo& info,
2988 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2989 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002990 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002991 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2992 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2993 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2994 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2995 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2996 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2997 }
2998
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002999 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3000 float size, float touchMajor, float touchMinor, float toolMajor,
3001 float toolMinor, float orientation, float distance,
3002 float scaledAxisEpsilon = 1.f) {
3003 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3004 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003005 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3006 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003007 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3008 scaledAxisEpsilon);
3009 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3010 scaledAxisEpsilon);
3011 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3012 scaledAxisEpsilon);
3013 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3014 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003015 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3016 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3017 }
3018
Michael Wright17db18e2020-06-26 20:51:44 +01003019 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003020 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003021 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003022 ASSERT_NEAR(x, actualX, 1);
3023 ASSERT_NEAR(y, actualY, 1);
3024 }
3025};
3026
3027const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003028const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003029const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003030const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3031const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07003032const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3033 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003034const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003035
3036// --- SwitchInputMapperTest ---
3037
3038class SwitchInputMapperTest : public InputMapperTest {
3039protected:
3040};
3041
3042TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003043 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003044
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003045 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003046}
3047
3048TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003049 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003050
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003051 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003052 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003053
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003054 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003055 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003056}
3057
3058TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003059 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003060
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3062 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3063 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3064 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003065
3066 NotifySwitchArgs args;
3067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
3068 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003069 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3070 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003071 args.switchMask);
3072 ASSERT_EQ(uint32_t(0), args.policyFlags);
3073}
3074
Chris Ye87143712020-11-10 05:05:58 +00003075// --- VibratorInputMapperTest ---
3076class VibratorInputMapperTest : public InputMapperTest {
3077protected:
3078 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3079};
3080
3081TEST_F(VibratorInputMapperTest, GetSources) {
3082 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3083
3084 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3085}
3086
3087TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3088 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3089
3090 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3091}
3092
3093TEST_F(VibratorInputMapperTest, Vibrate) {
3094 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003095 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003096 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3097
3098 VibrationElement pattern(2);
3099 VibrationSequence sequence(2);
3100 pattern.duration = std::chrono::milliseconds(200);
3101 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3102 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3103 sequence.addElement(pattern);
3104 pattern.duration = std::chrono::milliseconds(500);
3105 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3106 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3107 sequence.addElement(pattern);
3108
3109 std::vector<int64_t> timings = {0, 1};
3110 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3111
3112 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003113 // Start vibrating
3114 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003115 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003116 // Verify vibrator state listener was notified.
3117 mReader->loopOnce();
3118 NotifyVibratorStateArgs args;
3119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3120 ASSERT_EQ(DEVICE_ID, args.deviceId);
3121 ASSERT_TRUE(args.isOn);
3122 // Stop vibrating
3123 mapper.cancelVibrate(VIBRATION_TOKEN);
3124 ASSERT_FALSE(mapper.isVibrating());
3125 // Verify vibrator state listener was notified.
3126 mReader->loopOnce();
3127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
3128 ASSERT_EQ(DEVICE_ID, args.deviceId);
3129 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003130}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003131
Chris Yef59a2f42020-10-16 12:55:26 -07003132// --- SensorInputMapperTest ---
3133
3134class SensorInputMapperTest : public InputMapperTest {
3135protected:
3136 static const int32_t ACCEL_RAW_MIN;
3137 static const int32_t ACCEL_RAW_MAX;
3138 static const int32_t ACCEL_RAW_FUZZ;
3139 static const int32_t ACCEL_RAW_FLAT;
3140 static const int32_t ACCEL_RAW_RESOLUTION;
3141
3142 static const int32_t GYRO_RAW_MIN;
3143 static const int32_t GYRO_RAW_MAX;
3144 static const int32_t GYRO_RAW_FUZZ;
3145 static const int32_t GYRO_RAW_FLAT;
3146 static const int32_t GYRO_RAW_RESOLUTION;
3147
3148 static const float GRAVITY_MS2_UNIT;
3149 static const float DEGREE_RADIAN_UNIT;
3150
3151 void prepareAccelAxes();
3152 void prepareGyroAxes();
3153 void setAccelProperties();
3154 void setGyroProperties();
3155 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3156};
3157
3158const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3159const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3160const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3161const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3162const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3163
3164const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3165const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3166const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3167const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3168const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3169
3170const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3171const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3172
3173void SensorInputMapperTest::prepareAccelAxes() {
3174 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3175 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3176 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3177 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3178 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3179 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3180}
3181
3182void SensorInputMapperTest::prepareGyroAxes() {
3183 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3184 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3185 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3186 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3187 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3188 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3189}
3190
3191void SensorInputMapperTest::setAccelProperties() {
3192 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3193 /* sensorDataIndex */ 0);
3194 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3195 /* sensorDataIndex */ 1);
3196 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3197 /* sensorDataIndex */ 2);
3198 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3199 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3200 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3201 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3202 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3203}
3204
3205void SensorInputMapperTest::setGyroProperties() {
3206 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3207 /* sensorDataIndex */ 0);
3208 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3209 /* sensorDataIndex */ 1);
3210 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3211 /* sensorDataIndex */ 2);
3212 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3213 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3214 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3215 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3216 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3217}
3218
3219TEST_F(SensorInputMapperTest, GetSources) {
3220 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3221
3222 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3223}
3224
3225TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3226 setAccelProperties();
3227 prepareAccelAxes();
3228 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3229
3230 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3231 std::chrono::microseconds(10000),
3232 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003233 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3236 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3237 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3238 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003239
3240 NotifySensorArgs args;
3241 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3242 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3243 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3244
3245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3246 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3247 ASSERT_EQ(args.deviceId, DEVICE_ID);
3248 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3249 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3250 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3251 ASSERT_EQ(args.values, values);
3252 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3253}
3254
3255TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3256 setGyroProperties();
3257 prepareGyroAxes();
3258 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3259
3260 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3261 std::chrono::microseconds(10000),
3262 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003263 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3265 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3266 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3268 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003269
3270 NotifySensorArgs args;
3271 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3272 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3273 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3274
3275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3276 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3277 ASSERT_EQ(args.deviceId, DEVICE_ID);
3278 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3279 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3280 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3281 ASSERT_EQ(args.values, values);
3282 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3283}
3284
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285// --- KeyboardInputMapperTest ---
3286
3287class KeyboardInputMapperTest : public InputMapperTest {
3288protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003289 const std::string UNIQUE_ID = "local:0";
3290
3291 void prepareDisplay(int32_t orientation);
3292
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003293 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003294 int32_t originalKeyCode, int32_t rotatedKeyCode,
3295 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003296};
3297
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003298/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3299 * orientation.
3300 */
3301void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003302 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3303 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003304}
3305
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003306void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003307 int32_t originalScanCode, int32_t originalKeyCode,
3308 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003309 NotifyKeyArgs args;
3310
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003311 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3313 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3314 ASSERT_EQ(originalScanCode, args.scanCode);
3315 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003316 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003317
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003318 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3320 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3321 ASSERT_EQ(originalScanCode, args.scanCode);
3322 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003323 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003324}
3325
Michael Wrightd02c5b62014-02-10 15:10:22 -08003326TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003327 KeyboardInputMapper& mapper =
3328 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3329 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003330
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003331 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003332}
3333
3334TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3335 const int32_t USAGE_A = 0x070004;
3336 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003337 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3338 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003339 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3340 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3341 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003342
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003343 KeyboardInputMapper& mapper =
3344 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3345 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003346 // Initial metastate to AMETA_NONE.
3347 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3348 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003349
3350 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352 NotifyKeyArgs args;
3353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3354 ASSERT_EQ(DEVICE_ID, args.deviceId);
3355 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3356 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3357 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3358 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3359 ASSERT_EQ(KEY_HOME, args.scanCode);
3360 ASSERT_EQ(AMETA_NONE, args.metaState);
3361 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3362 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3363 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3364
3365 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003366 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3368 ASSERT_EQ(DEVICE_ID, args.deviceId);
3369 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3370 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3371 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3372 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3373 ASSERT_EQ(KEY_HOME, args.scanCode);
3374 ASSERT_EQ(AMETA_NONE, args.metaState);
3375 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3376 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3377 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3378
3379 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003380 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
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, args.eventTime);
3386 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3387 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3388 ASSERT_EQ(0, 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 up 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 + 1, READ_TIME, EV_KEY, 0, 0);
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 + 1, args.eventTime);
3401 ASSERT_EQ(AKEY_EVENT_ACTION_UP, 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 down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3411 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
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, args.eventTime);
3416 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3417 ASSERT_EQ(0, args.keyCode);
3418 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3419 ASSERT_EQ(AMETA_NONE, args.metaState);
3420 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3421 ASSERT_EQ(0U, args.policyFlags);
3422 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3423
3424 // Key up 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 + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
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 + 1, args.eventTime);
3431 ASSERT_EQ(AKEY_EVENT_ACTION_UP, 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
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003440/**
3441 * Ensure that the readTime is set to the time when the EV_KEY is received.
3442 */
3443TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3444 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3445
3446 KeyboardInputMapper& mapper =
3447 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3448 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3449 NotifyKeyArgs args;
3450
3451 // Key down
3452 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3454 ASSERT_EQ(12, args.readTime);
3455
3456 // Key up
3457 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3459 ASSERT_EQ(15, args.readTime);
3460}
3461
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003463 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3464 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003465 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3466 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3467 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003468
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003469 KeyboardInputMapper& mapper =
3470 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3471 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003472
arthurhungc903df12020-08-11 15:08:42 +08003473 // Initial metastate to AMETA_NONE.
3474 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3475 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003476
3477 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003479 NotifyKeyArgs args;
3480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3481 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003482 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003483 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003484
3485 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003486 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3488 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003489 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003490
3491 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003492 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3494 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003495 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003496
3497 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003498 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3500 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003501 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003502 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503}
3504
3505TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003506 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3507 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3508 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3509 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003511 KeyboardInputMapper& mapper =
3512 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3513 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003514
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003515 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003516 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3517 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3518 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3519 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3520 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3521 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3522 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3523 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3524}
3525
3526TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003527 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3528 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3529 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3530 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531
Michael Wrightd02c5b62014-02-10 15:10:22 -08003532 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003533 KeyboardInputMapper& mapper =
3534 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3535 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003536
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003537 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003538 ASSERT_NO_FATAL_FAILURE(
3539 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3540 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3541 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3542 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3543 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3544 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3545 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003547 clearViewports();
3548 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003549 ASSERT_NO_FATAL_FAILURE(
3550 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3551 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3552 AKEYCODE_DPAD_UP, DISPLAY_ID));
3553 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3554 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3555 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3556 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003558 clearViewports();
3559 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003560 ASSERT_NO_FATAL_FAILURE(
3561 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3562 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3563 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3564 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3565 AKEYCODE_DPAD_UP, DISPLAY_ID));
3566 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3567 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003568
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003569 clearViewports();
3570 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003571 ASSERT_NO_FATAL_FAILURE(
3572 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3573 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3574 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3575 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3576 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3577 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3578 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003579
3580 // Special case: if orientation changes while key is down, we still emit the same keycode
3581 // in the key up as we did in the key down.
3582 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003583 clearViewports();
3584 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3587 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3588 ASSERT_EQ(KEY_UP, args.scanCode);
3589 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3590
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003591 clearViewports();
3592 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3595 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3596 ASSERT_EQ(KEY_UP, args.scanCode);
3597 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3598}
3599
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003600TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3601 // If the keyboard is not orientation aware,
3602 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003603 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003604
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003605 KeyboardInputMapper& mapper =
3606 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3607 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003608 NotifyKeyArgs args;
3609
3610 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003611 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3615 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3616
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003617 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003618 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003620 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3622 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3623}
3624
3625TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3626 // If the keyboard is orientation aware,
3627 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003628 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003629
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003630 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003631 KeyboardInputMapper& mapper =
3632 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3633 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003634 NotifyKeyArgs args;
3635
3636 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3637 // ^--- already checked by the previous test
3638
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003639 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003640 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003643 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3645 ASSERT_EQ(DISPLAY_ID, args.displayId);
3646
3647 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003648 clearViewports();
3649 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003650 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003651 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003653 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3655 ASSERT_EQ(newDisplayId, args.displayId);
3656}
3657
Michael Wrightd02c5b62014-02-10 15:10:22 -08003658TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003659 KeyboardInputMapper& mapper =
3660 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3661 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003662
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003663 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003664 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003665
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003666 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003667 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003668}
3669
Philip Junker4af3b3d2021-12-14 10:36:55 +01003670TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3671 KeyboardInputMapper& mapper =
3672 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3673 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3674
3675 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3676 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3677 << "If a mapping is available, the result is equal to the mapping";
3678
3679 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3680 << "If no mapping is available, the result is the key location";
3681}
3682
Michael Wrightd02c5b62014-02-10 15:10:22 -08003683TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003684 KeyboardInputMapper& mapper =
3685 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3686 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003687
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003688 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003689 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003690
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003691 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003692 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003693}
3694
3695TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003696 KeyboardInputMapper& mapper =
3697 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3698 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003699
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003700 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003701
3702 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3703 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003704 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003705 ASSERT_TRUE(flags[0]);
3706 ASSERT_FALSE(flags[1]);
3707}
3708
3709TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003710 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3711 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3712 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3713 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3714 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3715 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003716
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003717 KeyboardInputMapper& mapper =
3718 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3719 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003720 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003721 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3722 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003723
3724 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003725 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3726 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3727 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728
3729 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003730 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3731 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003732 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3733 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3734 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003735 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003736
3737 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003738 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3739 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003740 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3741 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3742 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003743 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003744
3745 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003748 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3749 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3750 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003751 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003752
3753 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003754 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3755 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003756 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3757 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3758 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003759 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003760
3761 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003762 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3763 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003764 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3765 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3766 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003767 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003768
3769 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3771 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003772 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3773 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3774 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003775 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003776}
3777
Chris Yea52ade12020-08-27 16:49:20 -07003778TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3779 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3780 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3781 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3782 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3783
3784 KeyboardInputMapper& mapper =
3785 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3786 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3787
3788 // Initial metastate should be AMETA_NONE as no meta keys added.
3789 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3790 // Meta state should be AMETA_NONE after reset
3791 mapper.reset(ARBITRARY_TIME);
3792 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3793 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3794 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3795 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3796
3797 NotifyKeyArgs args;
3798 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3801 ASSERT_EQ(AMETA_NONE, args.metaState);
3802 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3803 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3804 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3805
3806 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003807 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3809 ASSERT_EQ(AMETA_NONE, args.metaState);
3810 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3811 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3812 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3813}
3814
Arthur Hung2c9a3342019-07-23 14:18:59 +08003815TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3816 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003817 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3818 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3819 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3820 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003821
3822 // keyboard 2.
3823 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003824 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003825 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003826 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003827 std::shared_ptr<InputDevice> device2 =
3828 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3829 Flags<InputDeviceClass>(0));
3830
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003831 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3832 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3833 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3834 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003835
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003836 KeyboardInputMapper& mapper =
3837 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3838 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003839
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003840 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003841 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003842 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003843 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3844 device2->reset(ARBITRARY_TIME);
3845
3846 // Prepared displays and associated info.
3847 constexpr uint8_t hdmi1 = 0;
3848 constexpr uint8_t hdmi2 = 1;
3849 const std::string SECONDARY_UNIQUE_ID = "local:1";
3850
3851 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3852 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3853
3854 // No associated display viewport found, should disable the device.
3855 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3856 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3857 ASSERT_FALSE(device2->isEnabled());
3858
3859 // Prepare second display.
3860 constexpr int32_t newDisplayId = 2;
3861 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003862 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003863 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003864 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003865 // Default device will reconfigure above, need additional reconfiguration for another device.
3866 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3867 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3868
3869 // Device should be enabled after the associated display is found.
3870 ASSERT_TRUE(mDevice->isEnabled());
3871 ASSERT_TRUE(device2->isEnabled());
3872
3873 // Test pad key events
3874 ASSERT_NO_FATAL_FAILURE(
3875 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3876 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3877 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3878 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3879 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3880 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3881 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3882
3883 ASSERT_NO_FATAL_FAILURE(
3884 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3885 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3886 AKEYCODE_DPAD_RIGHT, newDisplayId));
3887 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3888 AKEYCODE_DPAD_DOWN, newDisplayId));
3889 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3890 AKEYCODE_DPAD_LEFT, newDisplayId));
3891}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003892
arthurhungc903df12020-08-11 15:08:42 +08003893TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3894 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3895 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3896 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3897 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3898 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3899 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3900
3901 KeyboardInputMapper& mapper =
3902 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3903 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3904 // Initial metastate to AMETA_NONE.
3905 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3906 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3907
3908 // Initialization should have turned all of the lights off.
3909 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3910 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3911 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3912
3913 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003914 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3915 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003916 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3917 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3918
3919 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003920 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3921 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003922 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3923 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3924
3925 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003926 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3927 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003928 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3929 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3930
3931 mFakeEventHub->removeDevice(EVENTHUB_ID);
3932 mReader->loopOnce();
3933
3934 // keyboard 2 should default toggle keys.
3935 const std::string USB2 = "USB2";
3936 const std::string DEVICE_NAME2 = "KEYBOARD2";
3937 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3938 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3939 std::shared_ptr<InputDevice> device2 =
3940 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3941 Flags<InputDeviceClass>(0));
3942 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3943 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3944 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3945 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3946 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3947 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3948
arthurhung6fe95782020-10-05 22:41:16 +08003949 KeyboardInputMapper& mapper2 =
3950 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3951 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003952 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3953 device2->reset(ARBITRARY_TIME);
3954
3955 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3956 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3957 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003958 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3959 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003960}
3961
Arthur Hungcb40a002021-08-03 14:31:01 +00003962TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3963 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3964 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3965 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3966
3967 // Suppose we have two mappers. (DPAD + KEYBOARD)
3968 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3969 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3970 KeyboardInputMapper& mapper =
3971 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3972 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3973 // Initialize metastate to AMETA_NUM_LOCK_ON.
3974 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3975 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3976
3977 mReader->toggleCapsLockState(DEVICE_ID);
3978 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3979}
3980
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003981// --- KeyboardInputMapperTest_ExternalDevice ---
3982
3983class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3984protected:
Chris Yea52ade12020-08-27 16:49:20 -07003985 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003986};
3987
3988TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003989 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3990 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003991
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003992 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3993 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3994 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3995 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003996
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003997 KeyboardInputMapper& mapper =
3998 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3999 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004000
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004002 NotifyKeyArgs args;
4003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4004 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4005
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004006 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4008 ASSERT_EQ(uint32_t(0), args.policyFlags);
4009
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004010 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4012 ASSERT_EQ(uint32_t(0), args.policyFlags);
4013
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004014 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4016 ASSERT_EQ(uint32_t(0), args.policyFlags);
4017
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4020 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4021
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004022 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4024 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4025}
4026
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004027TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004028 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004029
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004030 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4031 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4032 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004033
Powei Fengd041c5d2019-05-03 17:11:33 -07004034 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004035 KeyboardInputMapper& mapper =
4036 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4037 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004038
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004040 NotifyKeyArgs args;
4041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4042 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4043
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004044 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4046 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4047
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004048 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4050 ASSERT_EQ(uint32_t(0), args.policyFlags);
4051
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004052 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4054 ASSERT_EQ(uint32_t(0), args.policyFlags);
4055
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004056 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4058 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4059
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004060 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4062 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4063}
4064
Michael Wrightd02c5b62014-02-10 15:10:22 -08004065// --- CursorInputMapperTest ---
4066
4067class CursorInputMapperTest : public InputMapperTest {
4068protected:
4069 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4070
Michael Wright17db18e2020-06-26 20:51:44 +01004071 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072
Chris Yea52ade12020-08-27 16:49:20 -07004073 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004074 InputMapperTest::SetUp();
4075
Michael Wright17db18e2020-06-26 20:51:44 +01004076 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004077 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078 }
4079
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004080 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4081 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004082
4083 void prepareDisplay(int32_t orientation) {
4084 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004085 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004086 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4087 orientation, uniqueId, NO_PORT, viewportType);
4088 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004089
4090 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4091 float pressure) {
4092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4093 0.0f, 0.0f, 0.0f, EPSILON));
4094 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004095};
4096
4097const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4098
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004099void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4100 int32_t originalY, int32_t rotatedX,
4101 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004102 NotifyMotionArgs args;
4103
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004104 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4105 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004109 ASSERT_NO_FATAL_FAILURE(
4110 assertCursorPointerCoords(args.pointerCoords[0],
4111 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4112 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004113}
4114
4115TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004116 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004117 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004118
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004119 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004120}
4121
4122TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004123 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004124 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004126 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004127}
4128
4129TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004131 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004132
4133 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004134 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004135
4136 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004137 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4138 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004139 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4140 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4141
4142 // When the bounds are set, then there should be a valid motion range.
4143 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4144
4145 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004146 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004147
4148 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4149 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4150 1, 800 - 1, 0.0f, 0.0f));
4151 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4152 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4153 2, 480 - 1, 0.0f, 0.0f));
4154 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4155 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4156 0.0f, 1.0f, 0.0f, 0.0f));
4157}
4158
4159TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004161 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004162
4163 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004164 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165
4166 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4167 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4168 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4169 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4170 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4171 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4172 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4173 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4174 0.0f, 1.0f, 0.0f, 0.0f));
4175}
4176
4177TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004178 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004179 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180
arthurhungdcef2dc2020-08-11 14:47:50 +08004181 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004182
4183 NotifyMotionArgs args;
4184
4185 // Button press.
4186 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4190 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4191 ASSERT_EQ(DEVICE_ID, args.deviceId);
4192 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4193 ASSERT_EQ(uint32_t(0), args.policyFlags);
4194 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4195 ASSERT_EQ(0, args.flags);
4196 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4197 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4198 ASSERT_EQ(0, args.edgeFlags);
4199 ASSERT_EQ(uint32_t(1), args.pointerCount);
4200 ASSERT_EQ(0, args.pointerProperties[0].id);
4201 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004202 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004203 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4204 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4205 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4206
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4208 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4209 ASSERT_EQ(DEVICE_ID, args.deviceId);
4210 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4211 ASSERT_EQ(uint32_t(0), args.policyFlags);
4212 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4213 ASSERT_EQ(0, args.flags);
4214 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4215 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4216 ASSERT_EQ(0, args.edgeFlags);
4217 ASSERT_EQ(uint32_t(1), args.pointerCount);
4218 ASSERT_EQ(0, args.pointerProperties[0].id);
4219 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004220 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004221 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4222 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4223 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4224
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004226 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4227 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4229 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4230 ASSERT_EQ(DEVICE_ID, args.deviceId);
4231 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4232 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004233 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4234 ASSERT_EQ(0, args.flags);
4235 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4236 ASSERT_EQ(0, args.buttonState);
4237 ASSERT_EQ(0, args.edgeFlags);
4238 ASSERT_EQ(uint32_t(1), args.pointerCount);
4239 ASSERT_EQ(0, args.pointerProperties[0].id);
4240 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004241 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004242 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4243 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4244 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4245
4246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4247 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4248 ASSERT_EQ(DEVICE_ID, args.deviceId);
4249 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4250 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004251 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4252 ASSERT_EQ(0, args.flags);
4253 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4254 ASSERT_EQ(0, args.buttonState);
4255 ASSERT_EQ(0, args.edgeFlags);
4256 ASSERT_EQ(uint32_t(1), args.pointerCount);
4257 ASSERT_EQ(0, args.pointerProperties[0].id);
4258 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004259 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4261 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4262 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4263}
4264
4265TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004267 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004268
4269 NotifyMotionArgs args;
4270
4271 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004272 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004276 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4277 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4278 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279
4280 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4284 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004285 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4286 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004287}
4288
4289TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004291 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004292
4293 NotifyMotionArgs args;
4294
4295 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
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_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004300 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004301
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4303 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004304 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004305
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004310 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004311 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004312
4313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004314 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004315 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316}
4317
4318TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004319 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004320 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321
4322 NotifyMotionArgs args;
4323
4324 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4326 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4327 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4328 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4330 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004331 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4332 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4333 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4336 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004337 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4338 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4339 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004340
Michael Wrightd02c5b62014-02-10 15:10:22 -08004341 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4343 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004347 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4348 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4349 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350
4351 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004352 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4353 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004355 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004356 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004357
4358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004360 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004361}
4362
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004363TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004365 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4366 // need to be rotated.
4367 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004368 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004369
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004370 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4372 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4373 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4374 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4375 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4376 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4377 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4378 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4379}
4380
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004381TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004383 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4384 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004385 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004386
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004387 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004388 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4389 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4390 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4391 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4392 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4393 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4394 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4395 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4396
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004397 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004398 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4399 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4400 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4401 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4402 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4403 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4404 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4405 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004407 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004408 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4409 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4410 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4411 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4412 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4413 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4414 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4415 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4416
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004417 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004418 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4419 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4420 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4421 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4422 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4423 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4424 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4425 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004426}
4427
4428TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004430 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431
4432 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4433 mFakePointerController->setPosition(100, 200);
4434 mFakePointerController->setButtonState(0);
4435
4436 NotifyMotionArgs motionArgs;
4437 NotifyKeyArgs keyArgs;
4438
4439 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4441 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4443 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4444 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4445 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004446 ASSERT_NO_FATAL_FAILURE(
4447 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4450 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4451 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4452 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004453 ASSERT_NO_FATAL_FAILURE(
4454 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004455
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004456 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004459 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460 ASSERT_EQ(0, motionArgs.buttonState);
4461 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004462 ASSERT_NO_FATAL_FAILURE(
4463 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464
4465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004466 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004467 ASSERT_EQ(0, motionArgs.buttonState);
4468 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004469 ASSERT_NO_FATAL_FAILURE(
4470 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004471
4472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004474 ASSERT_EQ(0, motionArgs.buttonState);
4475 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004476 ASSERT_NO_FATAL_FAILURE(
4477 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004478
4479 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004480 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4482 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4484 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4485 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4486 motionArgs.buttonState);
4487 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4488 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004489 ASSERT_NO_FATAL_FAILURE(
4490 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004491
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4493 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4494 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4495 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4496 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004497 ASSERT_NO_FATAL_FAILURE(
4498 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004499
4500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4501 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4502 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4503 motionArgs.buttonState);
4504 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4505 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004506 ASSERT_NO_FATAL_FAILURE(
4507 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004508
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4510 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004512 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004513 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4514 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004515 ASSERT_NO_FATAL_FAILURE(
4516 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004517
4518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004519 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004520 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4521 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004522 ASSERT_NO_FATAL_FAILURE(
4523 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004524
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004525 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004528 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4529 ASSERT_EQ(0, motionArgs.buttonState);
4530 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004531 ASSERT_NO_FATAL_FAILURE(
4532 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004533 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4534 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004535
4536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004537 ASSERT_EQ(0, motionArgs.buttonState);
4538 ASSERT_EQ(0, mFakePointerController->getButtonState());
4539 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004540 ASSERT_NO_FATAL_FAILURE(
4541 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004542
Michael Wrightd02c5b62014-02-10 15:10:22 -08004543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4544 ASSERT_EQ(0, motionArgs.buttonState);
4545 ASSERT_EQ(0, mFakePointerController->getButtonState());
4546 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004547 ASSERT_NO_FATAL_FAILURE(
4548 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004549
4550 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004551 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4552 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4554 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4555 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004556
Michael Wrightd02c5b62014-02-10 15:10:22 -08004557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004558 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004559 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4560 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004561 ASSERT_NO_FATAL_FAILURE(
4562 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004563
4564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4565 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4566 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4567 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004568 ASSERT_NO_FATAL_FAILURE(
4569 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004570
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4572 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004574 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004575 ASSERT_EQ(0, motionArgs.buttonState);
4576 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004577 ASSERT_NO_FATAL_FAILURE(
4578 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004579
4580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004581 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004582 ASSERT_EQ(0, motionArgs.buttonState);
4583 ASSERT_EQ(0, mFakePointerController->getButtonState());
4584
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004585 ASSERT_NO_FATAL_FAILURE(
4586 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4588 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4589 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4590
4591 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4595 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4596 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004597
Michael Wrightd02c5b62014-02-10 15:10:22 -08004598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004599 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004600 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4601 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004602 ASSERT_NO_FATAL_FAILURE(
4603 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004604
4605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4606 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4607 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4608 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004609 ASSERT_NO_FATAL_FAILURE(
4610 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004615 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616 ASSERT_EQ(0, motionArgs.buttonState);
4617 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004618 ASSERT_NO_FATAL_FAILURE(
4619 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004620
4621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4622 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4623 ASSERT_EQ(0, motionArgs.buttonState);
4624 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004625 ASSERT_NO_FATAL_FAILURE(
4626 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004627
Michael Wrightd02c5b62014-02-10 15:10:22 -08004628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4629 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4630 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4631
4632 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004633 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4636 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4637 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004638
Michael Wrightd02c5b62014-02-10 15:10:22 -08004639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004640 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004641 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4642 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004643 ASSERT_NO_FATAL_FAILURE(
4644 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004645
4646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4647 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4648 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4649 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004650 ASSERT_NO_FATAL_FAILURE(
4651 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004652
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004653 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004656 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004657 ASSERT_EQ(0, motionArgs.buttonState);
4658 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004659 ASSERT_NO_FATAL_FAILURE(
4660 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004661
4662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4663 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4664 ASSERT_EQ(0, motionArgs.buttonState);
4665 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004666 ASSERT_NO_FATAL_FAILURE(
4667 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004668
Michael Wrightd02c5b62014-02-10 15:10:22 -08004669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4670 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4671 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4672
4673 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004674 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4675 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4677 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4678 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004679
Michael Wrightd02c5b62014-02-10 15:10:22 -08004680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004681 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004682 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4683 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004684 ASSERT_NO_FATAL_FAILURE(
4685 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004686
4687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4688 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4689 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4690 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004691 ASSERT_NO_FATAL_FAILURE(
4692 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004693
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004694 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4695 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004697 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004698 ASSERT_EQ(0, motionArgs.buttonState);
4699 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004700 ASSERT_NO_FATAL_FAILURE(
4701 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004702
4703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4704 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4705 ASSERT_EQ(0, motionArgs.buttonState);
4706 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004707 ASSERT_NO_FATAL_FAILURE(
4708 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004709
Michael Wrightd02c5b62014-02-10 15:10:22 -08004710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4711 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4712 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4713}
4714
4715TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004716 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004717 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004718
4719 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4720 mFakePointerController->setPosition(100, 200);
4721 mFakePointerController->setButtonState(0);
4722
4723 NotifyMotionArgs args;
4724
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004725 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4726 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4727 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004729 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4730 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4732 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 +01004733 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004734}
4735
4736TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004737 addConfigurationProperty("cursor.mode", "pointer");
4738 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004739 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004740
4741 NotifyDeviceResetArgs resetArgs;
4742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4743 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4744 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4745
4746 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4747 mFakePointerController->setPosition(100, 200);
4748 mFakePointerController->setButtonState(0);
4749
4750 NotifyMotionArgs args;
4751
4752 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4754 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4755 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4757 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4758 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4760 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 +01004761 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004762
4763 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004764 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4765 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4767 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4768 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4770 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4772 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4773 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4774 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4775 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4776
4777 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004778 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4779 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4781 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4782 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4784 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4786 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4787 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4789 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4790
4791 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4794 process(mapper, ARBITRARY_TIME, 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_MOVE, args.action);
4798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4799 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 +01004800 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004801
4802 // Disable pointer capture and check that the device generation got bumped
4803 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004804 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004805 mFakePolicy->setPointerCapture(false);
4806 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004807 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004808
4809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4810 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4811 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4812
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004813 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4814 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4815 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4817 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004818 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4819 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4820 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 +01004821 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004822}
4823
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004824TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004825 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004826
Garfield Tan888a6a42020-01-09 11:39:16 -08004827 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004828 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004829 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4830 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004831 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4832 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004833 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4834 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4835
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004836 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4837 mFakePointerController->setPosition(100, 200);
4838 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004839
4840 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004841 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4842 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4843 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4845 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4846 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4847 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4848 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 +01004849 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004850 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4851}
4852
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853// --- TouchInputMapperTest ---
4854
4855class TouchInputMapperTest : public InputMapperTest {
4856protected:
4857 static const int32_t RAW_X_MIN;
4858 static const int32_t RAW_X_MAX;
4859 static const int32_t RAW_Y_MIN;
4860 static const int32_t RAW_Y_MAX;
4861 static const int32_t RAW_TOUCH_MIN;
4862 static const int32_t RAW_TOUCH_MAX;
4863 static const int32_t RAW_TOOL_MIN;
4864 static const int32_t RAW_TOOL_MAX;
4865 static const int32_t RAW_PRESSURE_MIN;
4866 static const int32_t RAW_PRESSURE_MAX;
4867 static const int32_t RAW_ORIENTATION_MIN;
4868 static const int32_t RAW_ORIENTATION_MAX;
4869 static const int32_t RAW_DISTANCE_MIN;
4870 static const int32_t RAW_DISTANCE_MAX;
4871 static const int32_t RAW_TILT_MIN;
4872 static const int32_t RAW_TILT_MAX;
4873 static const int32_t RAW_ID_MIN;
4874 static const int32_t RAW_ID_MAX;
4875 static const int32_t RAW_SLOT_MIN;
4876 static const int32_t RAW_SLOT_MAX;
4877 static const float X_PRECISION;
4878 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004879 static const float X_PRECISION_VIRTUAL;
4880 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004881
4882 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004883 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004884
4885 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4886
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004887 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004888 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004889
Michael Wrightd02c5b62014-02-10 15:10:22 -08004890 enum Axes {
4891 POSITION = 1 << 0,
4892 TOUCH = 1 << 1,
4893 TOOL = 1 << 2,
4894 PRESSURE = 1 << 3,
4895 ORIENTATION = 1 << 4,
4896 MINOR = 1 << 5,
4897 ID = 1 << 6,
4898 DISTANCE = 1 << 7,
4899 TILT = 1 << 8,
4900 SLOT = 1 << 9,
4901 TOOL_TYPE = 1 << 10,
4902 };
4903
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004904 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4905 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004906 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004907 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004908 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004909 int32_t toRawX(float displayX);
4910 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004911 int32_t toRotatedRawX(float displayX);
4912 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004913 float toCookedX(float rawX, float rawY);
4914 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004915 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004916 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004917 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004918 float toDisplayY(int32_t rawY, int32_t displayHeight);
4919
Michael Wrightd02c5b62014-02-10 15:10:22 -08004920};
4921
4922const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4923const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4924const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4925const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4926const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4927const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4928const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4929const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004930const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4931const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004932const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4933const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4934const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4935const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4936const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4937const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4938const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4939const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4940const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4941const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4942const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4943const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004944const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4945 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4946const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4947 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004948const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4949 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004950
4951const float TouchInputMapperTest::GEOMETRIC_SCALE =
4952 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4953 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4954
4955const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4956 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4957 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4958};
4959
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004960void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004961 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4962 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004963}
4964
4965void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4966 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4967 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004968}
4969
Santos Cordonfa5cf462017-04-05 10:37:00 -07004970void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004971 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4972 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4973 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004974}
4975
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004977 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4978 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4979 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4980 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004981}
4982
Jason Gerecke489fda82012-09-07 17:19:40 -07004983void TouchInputMapperTest::prepareLocationCalibration() {
4984 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4985}
4986
Michael Wrightd02c5b62014-02-10 15:10:22 -08004987int32_t TouchInputMapperTest::toRawX(float displayX) {
4988 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4989}
4990
4991int32_t TouchInputMapperTest::toRawY(float displayY) {
4992 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4993}
4994
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004995int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4996 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4997}
4998
4999int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5000 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5001}
5002
Jason Gerecke489fda82012-09-07 17:19:40 -07005003float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5004 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5005 return rawX;
5006}
5007
5008float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5009 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5010 return rawY;
5011}
5012
Michael Wrightd02c5b62014-02-10 15:10:22 -08005013float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005014 return toDisplayX(rawX, DISPLAY_WIDTH);
5015}
5016
5017float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5018 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005019}
5020
5021float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005022 return toDisplayY(rawY, DISPLAY_HEIGHT);
5023}
5024
5025float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5026 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005027}
5028
5029
5030// --- SingleTouchInputMapperTest ---
5031
5032class SingleTouchInputMapperTest : public TouchInputMapperTest {
5033protected:
5034 void prepareButtons();
5035 void prepareAxes(int axes);
5036
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005037 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5038 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5039 void processUp(SingleTouchInputMapper& mappery);
5040 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5041 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5042 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5043 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5044 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5045 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005046};
5047
5048void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005049 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005050}
5051
5052void SingleTouchInputMapperTest::prepareAxes(int axes) {
5053 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005054 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5055 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005056 }
5057 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005058 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5059 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060 }
5061 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005062 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5063 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005064 }
5065 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005066 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5067 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005068 }
5069 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005070 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5071 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005072 }
5073}
5074
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005075void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005076 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5078 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005079}
5080
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005081void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005082 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005084}
5085
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005086void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005087 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005088}
5089
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005090void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005091 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005092}
5093
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005094void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5095 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005097}
5098
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005099void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005100 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005101}
5102
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005103void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5104 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005105 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005107}
5108
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005109void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5110 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005111 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005112}
5113
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005114void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005115 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116}
5117
Michael Wrightd02c5b62014-02-10 15:10:22 -08005118TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005119 prepareButtons();
5120 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005121 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005122
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005123 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005124}
5125
5126TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005127 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
5128 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005129 prepareButtons();
5130 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005131 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005132
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005133 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005134}
5135
5136TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005137 prepareButtons();
5138 prepareAxes(POSITION);
5139 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005140 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005141
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005142 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005143}
5144
5145TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005146 prepareButtons();
5147 prepareAxes(POSITION);
5148 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005149 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005150
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005151 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005152}
5153
5154TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005155 addConfigurationProperty("touch.deviceType", "touchScreen");
5156 prepareDisplay(DISPLAY_ORIENTATION_0);
5157 prepareButtons();
5158 prepareAxes(POSITION);
5159 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005160 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005161
5162 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005163 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005164
5165 // Virtual key is down.
5166 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5167 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5168 processDown(mapper, x, y);
5169 processSync(mapper);
5170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5171
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005172 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005173
5174 // Virtual key is up.
5175 processUp(mapper);
5176 processSync(mapper);
5177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5178
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005179 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005180}
5181
5182TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005183 addConfigurationProperty("touch.deviceType", "touchScreen");
5184 prepareDisplay(DISPLAY_ORIENTATION_0);
5185 prepareButtons();
5186 prepareAxes(POSITION);
5187 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005188 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005189
5190 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005191 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005192
5193 // Virtual key is down.
5194 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5195 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5196 processDown(mapper, x, y);
5197 processSync(mapper);
5198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5199
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005200 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005201
5202 // Virtual key is up.
5203 processUp(mapper);
5204 processSync(mapper);
5205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5206
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005207 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005208}
5209
5210TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005211 addConfigurationProperty("touch.deviceType", "touchScreen");
5212 prepareDisplay(DISPLAY_ORIENTATION_0);
5213 prepareButtons();
5214 prepareAxes(POSITION);
5215 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005216 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005217
5218 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5219 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005220 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005221 ASSERT_TRUE(flags[0]);
5222 ASSERT_FALSE(flags[1]);
5223}
5224
5225TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
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
arthurhungdcef2dc2020-08-11 14:47:50 +08005233 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005234
5235 NotifyKeyArgs args;
5236
5237 // Press virtual key.
5238 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5239 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5240 processDown(mapper, x, y);
5241 processSync(mapper);
5242
5243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5244 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5245 ASSERT_EQ(DEVICE_ID, args.deviceId);
5246 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5247 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5248 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5249 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5250 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5251 ASSERT_EQ(KEY_HOME, args.scanCode);
5252 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5253 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5254
5255 // Release virtual key.
5256 processUp(mapper);
5257 processSync(mapper);
5258
5259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5260 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5261 ASSERT_EQ(DEVICE_ID, args.deviceId);
5262 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5263 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5264 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5265 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5266 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5267 ASSERT_EQ(KEY_HOME, args.scanCode);
5268 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5269 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5270
5271 // Should not have sent any motions.
5272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5273}
5274
5275TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005276 addConfigurationProperty("touch.deviceType", "touchScreen");
5277 prepareDisplay(DISPLAY_ORIENTATION_0);
5278 prepareButtons();
5279 prepareAxes(POSITION);
5280 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005281 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005282
arthurhungdcef2dc2020-08-11 14:47:50 +08005283 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005284
5285 NotifyKeyArgs keyArgs;
5286
5287 // Press virtual key.
5288 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5289 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5290 processDown(mapper, x, y);
5291 processSync(mapper);
5292
5293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5294 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5295 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5296 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5297 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5298 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5299 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5300 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5301 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5302 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5303 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5304
5305 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5306 // into the display area.
5307 y -= 100;
5308 processMove(mapper, x, y);
5309 processSync(mapper);
5310
5311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5312 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5313 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5314 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5315 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5316 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5317 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5318 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5319 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5320 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5321 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5322 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5323
5324 NotifyMotionArgs motionArgs;
5325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5326 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5327 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5328 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5329 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5330 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5331 ASSERT_EQ(0, motionArgs.flags);
5332 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5333 ASSERT_EQ(0, motionArgs.buttonState);
5334 ASSERT_EQ(0, motionArgs.edgeFlags);
5335 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5336 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5337 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5339 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5340 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5341 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5342 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5343
5344 // Keep moving out of bounds. Should generate a pointer move.
5345 y -= 50;
5346 processMove(mapper, x, y);
5347 processSync(mapper);
5348
5349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5350 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5351 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5352 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5353 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5355 ASSERT_EQ(0, motionArgs.flags);
5356 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5357 ASSERT_EQ(0, motionArgs.buttonState);
5358 ASSERT_EQ(0, motionArgs.edgeFlags);
5359 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5360 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5363 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5364 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5365 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5366 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5367
5368 // Release out of bounds. Should generate a pointer up.
5369 processUp(mapper);
5370 processSync(mapper);
5371
5372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5373 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5374 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5375 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5376 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5377 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5378 ASSERT_EQ(0, motionArgs.flags);
5379 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5380 ASSERT_EQ(0, motionArgs.buttonState);
5381 ASSERT_EQ(0, motionArgs.edgeFlags);
5382 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5383 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5384 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5386 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5387 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5388 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5389 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5390
5391 // Should not have sent any more keys or motions.
5392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5394}
5395
5396TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005397 addConfigurationProperty("touch.deviceType", "touchScreen");
5398 prepareDisplay(DISPLAY_ORIENTATION_0);
5399 prepareButtons();
5400 prepareAxes(POSITION);
5401 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005402 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403
arthurhungdcef2dc2020-08-11 14:47:50 +08005404 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005405
5406 NotifyMotionArgs motionArgs;
5407
5408 // Initially go down out of bounds.
5409 int32_t x = -10;
5410 int32_t y = -10;
5411 processDown(mapper, x, y);
5412 processSync(mapper);
5413
5414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5415
5416 // Move into the display area. Should generate a pointer down.
5417 x = 50;
5418 y = 75;
5419 processMove(mapper, x, y);
5420 processSync(mapper);
5421
5422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5423 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5424 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5425 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5426 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5427 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5428 ASSERT_EQ(0, motionArgs.flags);
5429 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5430 ASSERT_EQ(0, motionArgs.buttonState);
5431 ASSERT_EQ(0, motionArgs.edgeFlags);
5432 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5433 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5434 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5436 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5437 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5438 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5439 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5440
5441 // Release. Should generate a pointer up.
5442 processUp(mapper);
5443 processSync(mapper);
5444
5445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5446 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5447 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5448 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5449 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5450 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5451 ASSERT_EQ(0, motionArgs.flags);
5452 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5453 ASSERT_EQ(0, motionArgs.buttonState);
5454 ASSERT_EQ(0, motionArgs.edgeFlags);
5455 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5456 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5457 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5458 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5459 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5460 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5461 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5462 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5463
5464 // Should not have sent any more keys or motions.
5465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5467}
5468
Santos Cordonfa5cf462017-04-05 10:37:00 -07005469TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005470 addConfigurationProperty("touch.deviceType", "touchScreen");
5471 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5472
5473 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5474 prepareButtons();
5475 prepareAxes(POSITION);
5476 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005477 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005478
arthurhungdcef2dc2020-08-11 14:47:50 +08005479 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005480
5481 NotifyMotionArgs motionArgs;
5482
5483 // Down.
5484 int32_t x = 100;
5485 int32_t y = 125;
5486 processDown(mapper, x, y);
5487 processSync(mapper);
5488
5489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5490 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5491 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5492 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5493 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5494 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5495 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5496 ASSERT_EQ(0, motionArgs.flags);
5497 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5498 ASSERT_EQ(0, motionArgs.buttonState);
5499 ASSERT_EQ(0, motionArgs.edgeFlags);
5500 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5501 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5502 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5504 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5505 1, 0, 0, 0, 0, 0, 0, 0));
5506 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5507 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5508 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5509
5510 // Move.
5511 x += 50;
5512 y += 75;
5513 processMove(mapper, x, y);
5514 processSync(mapper);
5515
5516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5517 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5518 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5519 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5520 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5521 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5522 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5523 ASSERT_EQ(0, motionArgs.flags);
5524 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5525 ASSERT_EQ(0, motionArgs.buttonState);
5526 ASSERT_EQ(0, motionArgs.edgeFlags);
5527 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5528 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5529 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5531 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5532 1, 0, 0, 0, 0, 0, 0, 0));
5533 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5534 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5535 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5536
5537 // Up.
5538 processUp(mapper);
5539 processSync(mapper);
5540
5541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5542 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5543 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5544 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5545 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5546 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5547 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5548 ASSERT_EQ(0, motionArgs.flags);
5549 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5550 ASSERT_EQ(0, motionArgs.buttonState);
5551 ASSERT_EQ(0, motionArgs.edgeFlags);
5552 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5553 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5554 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5556 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5557 1, 0, 0, 0, 0, 0, 0, 0));
5558 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5559 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5560 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5561
5562 // Should not have sent any more keys or motions.
5563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5565}
5566
Michael Wrightd02c5b62014-02-10 15:10:22 -08005567TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005568 addConfigurationProperty("touch.deviceType", "touchScreen");
5569 prepareDisplay(DISPLAY_ORIENTATION_0);
5570 prepareButtons();
5571 prepareAxes(POSITION);
5572 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005573 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005574
arthurhungdcef2dc2020-08-11 14:47:50 +08005575 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005576
5577 NotifyMotionArgs motionArgs;
5578
5579 // Down.
5580 int32_t x = 100;
5581 int32_t y = 125;
5582 processDown(mapper, x, y);
5583 processSync(mapper);
5584
5585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5586 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5587 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5588 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5589 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5590 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5591 ASSERT_EQ(0, motionArgs.flags);
5592 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5593 ASSERT_EQ(0, motionArgs.buttonState);
5594 ASSERT_EQ(0, motionArgs.edgeFlags);
5595 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5596 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5597 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5599 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5600 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5601 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5602 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5603
5604 // Move.
5605 x += 50;
5606 y += 75;
5607 processMove(mapper, x, y);
5608 processSync(mapper);
5609
5610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5611 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5612 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5613 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5614 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5615 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5616 ASSERT_EQ(0, motionArgs.flags);
5617 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5618 ASSERT_EQ(0, motionArgs.buttonState);
5619 ASSERT_EQ(0, motionArgs.edgeFlags);
5620 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5621 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5622 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5624 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5625 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5626 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5627 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5628
5629 // Up.
5630 processUp(mapper);
5631 processSync(mapper);
5632
5633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5634 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5635 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5636 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5637 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5638 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5639 ASSERT_EQ(0, motionArgs.flags);
5640 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5641 ASSERT_EQ(0, motionArgs.buttonState);
5642 ASSERT_EQ(0, motionArgs.edgeFlags);
5643 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5644 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5645 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5646 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5647 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5648 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5649 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5650 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5651
5652 // Should not have sent any more keys or motions.
5653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5655}
5656
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005657TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005658 addConfigurationProperty("touch.deviceType", "touchScreen");
5659 prepareButtons();
5660 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005661 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5662 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005663 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005664
5665 NotifyMotionArgs args;
5666
5667 // Rotation 90.
5668 prepareDisplay(DISPLAY_ORIENTATION_90);
5669 processDown(mapper, toRawX(50), toRawY(75));
5670 processSync(mapper);
5671
5672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5673 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5674 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5675
5676 processUp(mapper);
5677 processSync(mapper);
5678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5679}
5680
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005681TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005682 addConfigurationProperty("touch.deviceType", "touchScreen");
5683 prepareButtons();
5684 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005685 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5686 // orientation-aware are affected by display rotation.
5687 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005688 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005689
5690 NotifyMotionArgs args;
5691
5692 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005693 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005694 prepareDisplay(DISPLAY_ORIENTATION_0);
5695 processDown(mapper, toRawX(50), toRawY(75));
5696 processSync(mapper);
5697
5698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5699 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5700 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5701
5702 processUp(mapper);
5703 processSync(mapper);
5704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5705
5706 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005707 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005708 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005709 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005710 processSync(mapper);
5711
5712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5713 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5714 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5715
5716 processUp(mapper);
5717 processSync(mapper);
5718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5719
5720 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005721 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005722 prepareDisplay(DISPLAY_ORIENTATION_180);
5723 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5724 processSync(mapper);
5725
5726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5727 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5728 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5729
5730 processUp(mapper);
5731 processSync(mapper);
5732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5733
5734 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005735 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005736 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005737 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005738 processSync(mapper);
5739
5740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5741 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5742 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5743
5744 processUp(mapper);
5745 processSync(mapper);
5746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5747}
5748
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005749TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5750 addConfigurationProperty("touch.deviceType", "touchScreen");
5751 prepareButtons();
5752 prepareAxes(POSITION);
5753 addConfigurationProperty("touch.orientationAware", "1");
5754 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5755 clearViewports();
5756 prepareDisplay(DISPLAY_ORIENTATION_0);
5757 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5758 NotifyMotionArgs args;
5759
5760 // Orientation 0.
5761 processDown(mapper, toRawX(50), toRawY(75));
5762 processSync(mapper);
5763
5764 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5765 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5766 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5767
5768 processUp(mapper);
5769 processSync(mapper);
5770 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5771}
5772
5773TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5774 addConfigurationProperty("touch.deviceType", "touchScreen");
5775 prepareButtons();
5776 prepareAxes(POSITION);
5777 addConfigurationProperty("touch.orientationAware", "1");
5778 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5779 clearViewports();
5780 prepareDisplay(DISPLAY_ORIENTATION_0);
5781 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5782 NotifyMotionArgs args;
5783
5784 // Orientation 90.
5785 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5786 processSync(mapper);
5787
5788 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5789 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5790 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5791
5792 processUp(mapper);
5793 processSync(mapper);
5794 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5795}
5796
5797TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5798 addConfigurationProperty("touch.deviceType", "touchScreen");
5799 prepareButtons();
5800 prepareAxes(POSITION);
5801 addConfigurationProperty("touch.orientationAware", "1");
5802 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5803 clearViewports();
5804 prepareDisplay(DISPLAY_ORIENTATION_0);
5805 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5806 NotifyMotionArgs args;
5807
5808 // Orientation 180.
5809 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5810 processSync(mapper);
5811
5812 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5813 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5814 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5815
5816 processUp(mapper);
5817 processSync(mapper);
5818 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5819}
5820
5821TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5822 addConfigurationProperty("touch.deviceType", "touchScreen");
5823 prepareButtons();
5824 prepareAxes(POSITION);
5825 addConfigurationProperty("touch.orientationAware", "1");
5826 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5827 clearViewports();
5828 prepareDisplay(DISPLAY_ORIENTATION_0);
5829 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5830 NotifyMotionArgs args;
5831
5832 // Orientation 270.
5833 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5834 processSync(mapper);
5835
5836 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5837 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5838 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5839
5840 processUp(mapper);
5841 processSync(mapper);
5842 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5843}
5844
5845TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5846 addConfigurationProperty("touch.deviceType", "touchScreen");
5847 prepareButtons();
5848 prepareAxes(POSITION);
5849 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5850 // orientation-aware are affected by display rotation.
5851 addConfigurationProperty("touch.orientationAware", "0");
5852 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5853 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5854
5855 NotifyMotionArgs args;
5856
5857 // Orientation 90, Rotation 0.
5858 clearViewports();
5859 prepareDisplay(DISPLAY_ORIENTATION_0);
5860 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5861 processSync(mapper);
5862
5863 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5864 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5865 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5866
5867 processUp(mapper);
5868 processSync(mapper);
5869 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5870
5871 // Orientation 90, Rotation 90.
5872 clearViewports();
5873 prepareDisplay(DISPLAY_ORIENTATION_90);
5874 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
5875 processSync(mapper);
5876
5877 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5878 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5879 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5880
5881 processUp(mapper);
5882 processSync(mapper);
5883 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5884
5885 // Orientation 90, Rotation 180.
5886 clearViewports();
5887 prepareDisplay(DISPLAY_ORIENTATION_180);
5888 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5889 processSync(mapper);
5890
5891 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5892 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5893 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5894
5895 processUp(mapper);
5896 processSync(mapper);
5897 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5898
5899 // Orientation 90, Rotation 270.
5900 clearViewports();
5901 prepareDisplay(DISPLAY_ORIENTATION_270);
5902 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
5903 RAW_Y_MAX - toRotatedRawY(75) + 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08005915TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005916 addConfigurationProperty("touch.deviceType", "touchScreen");
5917 prepareDisplay(DISPLAY_ORIENTATION_0);
5918 prepareButtons();
5919 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005920 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005921
5922 // These calculations are based on the input device calibration documentation.
5923 int32_t rawX = 100;
5924 int32_t rawY = 200;
5925 int32_t rawPressure = 10;
5926 int32_t rawToolMajor = 12;
5927 int32_t rawDistance = 2;
5928 int32_t rawTiltX = 30;
5929 int32_t rawTiltY = 110;
5930
5931 float x = toDisplayX(rawX);
5932 float y = toDisplayY(rawY);
5933 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5934 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5935 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5936 float distance = float(rawDistance);
5937
5938 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5939 float tiltScale = M_PI / 180;
5940 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5941 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5942 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5943 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5944
5945 processDown(mapper, rawX, rawY);
5946 processPressure(mapper, rawPressure);
5947 processToolMajor(mapper, rawToolMajor);
5948 processDistance(mapper, rawDistance);
5949 processTilt(mapper, rawTiltX, rawTiltY);
5950 processSync(mapper);
5951
5952 NotifyMotionArgs args;
5953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5954 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5955 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5956 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5957}
5958
Jason Gerecke489fda82012-09-07 17:19:40 -07005959TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005960 addConfigurationProperty("touch.deviceType", "touchScreen");
5961 prepareDisplay(DISPLAY_ORIENTATION_0);
5962 prepareLocationCalibration();
5963 prepareButtons();
5964 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005965 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005966
5967 int32_t rawX = 100;
5968 int32_t rawY = 200;
5969
5970 float x = toDisplayX(toCookedX(rawX, rawY));
5971 float y = toDisplayY(toCookedY(rawX, rawY));
5972
5973 processDown(mapper, rawX, rawY);
5974 processSync(mapper);
5975
5976 NotifyMotionArgs args;
5977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5978 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5979 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5980}
5981
Michael Wrightd02c5b62014-02-10 15:10:22 -08005982TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005983 addConfigurationProperty("touch.deviceType", "touchScreen");
5984 prepareDisplay(DISPLAY_ORIENTATION_0);
5985 prepareButtons();
5986 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005987 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005988
5989 NotifyMotionArgs motionArgs;
5990 NotifyKeyArgs keyArgs;
5991
5992 processDown(mapper, 100, 200);
5993 processSync(mapper);
5994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5995 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5996 ASSERT_EQ(0, motionArgs.buttonState);
5997
5998 // press BTN_LEFT, release BTN_LEFT
5999 processKey(mapper, BTN_LEFT, 1);
6000 processSync(mapper);
6001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6002 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6003 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6004
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6006 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6007 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6008
Michael Wrightd02c5b62014-02-10 15:10:22 -08006009 processKey(mapper, BTN_LEFT, 0);
6010 processSync(mapper);
6011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006012 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006013 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006014
6015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006016 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006017 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006018
6019 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6020 processKey(mapper, BTN_RIGHT, 1);
6021 processKey(mapper, BTN_MIDDLE, 1);
6022 processSync(mapper);
6023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6025 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6026 motionArgs.buttonState);
6027
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6029 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6030 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6031
6032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6033 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6034 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6035 motionArgs.buttonState);
6036
Michael Wrightd02c5b62014-02-10 15:10:22 -08006037 processKey(mapper, BTN_RIGHT, 0);
6038 processSync(mapper);
6039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006040 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006041 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006042
6043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006044 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006045 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006046
6047 processKey(mapper, BTN_MIDDLE, 0);
6048 processSync(mapper);
6049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006050 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006051 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006052
6053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006054 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006055 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006056
6057 // press BTN_BACK, release BTN_BACK
6058 processKey(mapper, BTN_BACK, 1);
6059 processSync(mapper);
6060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6061 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6062 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006063
Michael Wrightd02c5b62014-02-10 15:10:22 -08006064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006065 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006066 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6067
6068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6069 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6070 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006071
6072 processKey(mapper, BTN_BACK, 0);
6073 processSync(mapper);
6074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006075 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006076 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006077
6078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006079 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006080 ASSERT_EQ(0, motionArgs.buttonState);
6081
Michael Wrightd02c5b62014-02-10 15:10:22 -08006082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6083 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6084 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6085
6086 // press BTN_SIDE, release BTN_SIDE
6087 processKey(mapper, BTN_SIDE, 1);
6088 processSync(mapper);
6089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6090 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6091 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006092
Michael Wrightd02c5b62014-02-10 15:10:22 -08006093 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(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6096
6097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6098 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6099 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006100
6101 processKey(mapper, BTN_SIDE, 0);
6102 processSync(mapper);
6103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006104 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006105 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006106
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006109 ASSERT_EQ(0, motionArgs.buttonState);
6110
Michael Wrightd02c5b62014-02-10 15:10:22 -08006111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6112 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6113 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6114
6115 // press BTN_FORWARD, release BTN_FORWARD
6116 processKey(mapper, BTN_FORWARD, 1);
6117 processSync(mapper);
6118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6119 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6120 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006121
Michael Wrightd02c5b62014-02-10 15:10:22 -08006122 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(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6125
6126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6127 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6128 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129
6130 processKey(mapper, BTN_FORWARD, 0);
6131 processSync(mapper);
6132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006133 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006134 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006135
6136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006138 ASSERT_EQ(0, motionArgs.buttonState);
6139
Michael Wrightd02c5b62014-02-10 15:10:22 -08006140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6141 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6142 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6143
6144 // press BTN_EXTRA, release BTN_EXTRA
6145 processKey(mapper, BTN_EXTRA, 1);
6146 processSync(mapper);
6147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6148 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6149 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006150
Michael Wrightd02c5b62014-02-10 15:10:22 -08006151 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(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6154
6155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6156 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6157 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006158
6159 processKey(mapper, BTN_EXTRA, 0);
6160 processSync(mapper);
6161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006162 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006163 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006164
6165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006167 ASSERT_EQ(0, motionArgs.buttonState);
6168
Michael Wrightd02c5b62014-02-10 15:10:22 -08006169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6170 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6171 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6172
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6174
Michael Wrightd02c5b62014-02-10 15:10:22 -08006175 // press BTN_STYLUS, release BTN_STYLUS
6176 processKey(mapper, BTN_STYLUS, 1);
6177 processSync(mapper);
6178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006180 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6181
6182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6184 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006185
6186 processKey(mapper, BTN_STYLUS, 0);
6187 processSync(mapper);
6188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006189 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006190 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006191
6192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006193 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006194 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006195
6196 // press BTN_STYLUS2, release BTN_STYLUS2
6197 processKey(mapper, BTN_STYLUS2, 1);
6198 processSync(mapper);
6199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006201 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6202
6203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6204 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6205 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006206
6207 processKey(mapper, BTN_STYLUS2, 0);
6208 processSync(mapper);
6209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006210 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006211 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006212
6213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006215 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006216
6217 // release touch
6218 processUp(mapper);
6219 processSync(mapper);
6220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6221 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6222 ASSERT_EQ(0, motionArgs.buttonState);
6223}
6224
6225TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006226 addConfigurationProperty("touch.deviceType", "touchScreen");
6227 prepareDisplay(DISPLAY_ORIENTATION_0);
6228 prepareButtons();
6229 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006230 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006231
6232 NotifyMotionArgs motionArgs;
6233
6234 // default tool type is finger
6235 processDown(mapper, 100, 200);
6236 processSync(mapper);
6237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6238 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6239 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6240
6241 // eraser
6242 processKey(mapper, BTN_TOOL_RUBBER, 1);
6243 processSync(mapper);
6244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6246 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6247
6248 // stylus
6249 processKey(mapper, BTN_TOOL_RUBBER, 0);
6250 processKey(mapper, BTN_TOOL_PEN, 1);
6251 processSync(mapper);
6252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6255
6256 // brush
6257 processKey(mapper, BTN_TOOL_PEN, 0);
6258 processKey(mapper, BTN_TOOL_BRUSH, 1);
6259 processSync(mapper);
6260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6262 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6263
6264 // pencil
6265 processKey(mapper, BTN_TOOL_BRUSH, 0);
6266 processKey(mapper, BTN_TOOL_PENCIL, 1);
6267 processSync(mapper);
6268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6270 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6271
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006272 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006273 processKey(mapper, BTN_TOOL_PENCIL, 0);
6274 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6275 processSync(mapper);
6276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6278 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6279
6280 // mouse
6281 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6282 processKey(mapper, BTN_TOOL_MOUSE, 1);
6283 processSync(mapper);
6284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6286 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6287
6288 // lens
6289 processKey(mapper, BTN_TOOL_MOUSE, 0);
6290 processKey(mapper, BTN_TOOL_LENS, 1);
6291 processSync(mapper);
6292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6294 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6295
6296 // double-tap
6297 processKey(mapper, BTN_TOOL_LENS, 0);
6298 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6299 processSync(mapper);
6300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6301 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6302 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6303
6304 // triple-tap
6305 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6306 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6307 processSync(mapper);
6308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6310 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6311
6312 // quad-tap
6313 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6314 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6315 processSync(mapper);
6316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6318 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6319
6320 // finger
6321 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6322 processKey(mapper, BTN_TOOL_FINGER, 1);
6323 processSync(mapper);
6324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6326 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6327
6328 // stylus trumps finger
6329 processKey(mapper, BTN_TOOL_PEN, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
6334
6335 // eraser trumps stylus
6336 processKey(mapper, BTN_TOOL_RUBBER, 1);
6337 processSync(mapper);
6338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6340 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6341
6342 // mouse trumps eraser
6343 processKey(mapper, BTN_TOOL_MOUSE, 1);
6344 processSync(mapper);
6345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6347 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6348
6349 // back to default tool type
6350 processKey(mapper, BTN_TOOL_MOUSE, 0);
6351 processKey(mapper, BTN_TOOL_RUBBER, 0);
6352 processKey(mapper, BTN_TOOL_PEN, 0);
6353 processKey(mapper, BTN_TOOL_FINGER, 0);
6354 processSync(mapper);
6355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6356 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6357 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6358}
6359
6360TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006361 addConfigurationProperty("touch.deviceType", "touchScreen");
6362 prepareDisplay(DISPLAY_ORIENTATION_0);
6363 prepareButtons();
6364 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006365 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006366 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006367
6368 NotifyMotionArgs motionArgs;
6369
6370 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6371 processKey(mapper, BTN_TOOL_FINGER, 1);
6372 processMove(mapper, 100, 200);
6373 processSync(mapper);
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6375 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6377 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6378
6379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6380 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6381 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6382 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6383
6384 // move a little
6385 processMove(mapper, 150, 250);
6386 processSync(mapper);
6387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6388 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6390 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6391
6392 // down when BTN_TOUCH is pressed, pressure defaults to 1
6393 processKey(mapper, BTN_TOUCH, 1);
6394 processSync(mapper);
6395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6396 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6398 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6399
6400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6401 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6403 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6404
6405 // up when BTN_TOUCH is released, hover restored
6406 processKey(mapper, BTN_TOUCH, 0);
6407 processSync(mapper);
6408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6409 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6411 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6412
6413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6414 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6416 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6417
6418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6419 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6421 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6422
6423 // exit hover when pointer goes away
6424 processKey(mapper, BTN_TOOL_FINGER, 0);
6425 processSync(mapper);
6426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6427 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6428 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6429 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6430}
6431
6432TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006433 addConfigurationProperty("touch.deviceType", "touchScreen");
6434 prepareDisplay(DISPLAY_ORIENTATION_0);
6435 prepareButtons();
6436 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006437 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006438
6439 NotifyMotionArgs motionArgs;
6440
6441 // initially hovering because pressure is 0
6442 processDown(mapper, 100, 200);
6443 processPressure(mapper, 0);
6444 processSync(mapper);
6445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6446 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6448 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6449
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6451 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6453 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6454
6455 // move a little
6456 processMove(mapper, 150, 250);
6457 processSync(mapper);
6458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6459 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6460 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6461 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6462
6463 // down when pressure is non-zero
6464 processPressure(mapper, RAW_PRESSURE_MAX);
6465 processSync(mapper);
6466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6467 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6469 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6470
6471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6472 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6473 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6474 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6475
6476 // up when pressure becomes 0, hover restored
6477 processPressure(mapper, 0);
6478 processSync(mapper);
6479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6480 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6481 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6482 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6483
6484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6485 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6487 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6488
6489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6490 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6491 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6492 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6493
6494 // exit hover when pointer goes away
6495 processUp(mapper);
6496 processSync(mapper);
6497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6498 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6500 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6501}
6502
Prabir Pradhan5632d622021-09-06 07:57:20 -07006503// --- TouchDisplayProjectionTest ---
6504
6505class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6506public:
6507 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6508 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6509 // rotated equivalent of the given un-rotated physical display bounds.
6510 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6511 uint32_t inverseRotationFlags;
6512 auto width = DISPLAY_WIDTH;
6513 auto height = DISPLAY_HEIGHT;
6514 switch (orientation) {
6515 case DISPLAY_ORIENTATION_90:
6516 inverseRotationFlags = ui::Transform::ROT_270;
6517 std::swap(width, height);
6518 break;
6519 case DISPLAY_ORIENTATION_180:
6520 inverseRotationFlags = ui::Transform::ROT_180;
6521 break;
6522 case DISPLAY_ORIENTATION_270:
6523 inverseRotationFlags = ui::Transform::ROT_90;
6524 std::swap(width, height);
6525 break;
6526 case DISPLAY_ORIENTATION_0:
6527 inverseRotationFlags = ui::Transform::ROT_0;
6528 break;
6529 default:
6530 FAIL() << "Invalid orientation: " << orientation;
6531 }
6532
6533 const ui::Transform rotation(inverseRotationFlags, width, height);
6534 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6535
6536 std::optional<DisplayViewport> internalViewport =
6537 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6538 DisplayViewport& v = *internalViewport;
6539 v.displayId = DISPLAY_ID;
6540 v.orientation = orientation;
6541
6542 v.logicalLeft = 0;
6543 v.logicalTop = 0;
6544 v.logicalRight = 100;
6545 v.logicalBottom = 100;
6546
6547 v.physicalLeft = rotatedPhysicalDisplay.left;
6548 v.physicalTop = rotatedPhysicalDisplay.top;
6549 v.physicalRight = rotatedPhysicalDisplay.right;
6550 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6551
6552 v.deviceWidth = width;
6553 v.deviceHeight = height;
6554
6555 v.isActive = true;
6556 v.uniqueId = UNIQUE_ID;
6557 v.type = ViewportType::INTERNAL;
6558 mFakePolicy->updateViewport(v);
6559 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6560 }
6561
6562 void assertReceivedMove(const Point& point) {
6563 NotifyMotionArgs motionArgs;
6564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6566 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6567 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6568 1, 0, 0, 0, 0, 0, 0, 0));
6569 }
6570};
6571
6572TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6573 addConfigurationProperty("touch.deviceType", "touchScreen");
6574 prepareDisplay(DISPLAY_ORIENTATION_0);
6575
6576 prepareButtons();
6577 prepareAxes(POSITION);
6578 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6579
6580 NotifyMotionArgs motionArgs;
6581
6582 // Configure the DisplayViewport such that the logical display maps to a subsection of
6583 // the display panel called the physical display. Here, the physical display is bounded by the
6584 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6585 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6586 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6587 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6588
6589 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6590 DISPLAY_ORIENTATION_270}) {
6591 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6592
6593 // Touches outside the physical display should be ignored, and should not generate any
6594 // events. Ensure touches at the following points that lie outside of the physical display
6595 // area do not generate any events.
6596 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6597 processDown(mapper, toRawX(point.x), toRawY(point.y));
6598 processSync(mapper);
6599 processUp(mapper);
6600 processSync(mapper);
6601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6602 << "Unexpected event generated for touch outside physical display at point: "
6603 << point.x << ", " << point.y;
6604 }
6605 }
6606}
6607
6608TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6609 addConfigurationProperty("touch.deviceType", "touchScreen");
6610 prepareDisplay(DISPLAY_ORIENTATION_0);
6611
6612 prepareButtons();
6613 prepareAxes(POSITION);
6614 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6615
6616 NotifyMotionArgs motionArgs;
6617
6618 // Configure the DisplayViewport such that the logical display maps to a subsection of
6619 // the display panel called the physical display. Here, the physical display is bounded by the
6620 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6621 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6622
6623 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6624 DISPLAY_ORIENTATION_270}) {
6625 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6626
6627 // Touches that start outside the physical display should be ignored until it enters the
6628 // physical display bounds, at which point it should generate a down event. Start a touch at
6629 // the point (5, 100), which is outside the physical display bounds.
6630 static const Point kOutsidePoint{5, 100};
6631 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6632 processSync(mapper);
6633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6634
6635 // Move the touch into the physical display area. This should generate a pointer down.
6636 processMove(mapper, toRawX(11), toRawY(21));
6637 processSync(mapper);
6638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6639 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6640 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6641 ASSERT_NO_FATAL_FAILURE(
6642 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6643
6644 // Move the touch inside the physical display area. This should generate a pointer move.
6645 processMove(mapper, toRawX(69), toRawY(159));
6646 processSync(mapper);
6647 assertReceivedMove({69, 159});
6648
6649 // Move outside the physical display area. Since the pointer is already down, this should
6650 // now continue generating events.
6651 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6652 processSync(mapper);
6653 assertReceivedMove(kOutsidePoint);
6654
6655 // Release. This should generate a pointer up.
6656 processUp(mapper);
6657 processSync(mapper);
6658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6659 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6661 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6662
6663 // Ensure no more events were generated.
6664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6666 }
6667}
6668
Michael Wrightd02c5b62014-02-10 15:10:22 -08006669// --- MultiTouchInputMapperTest ---
6670
6671class MultiTouchInputMapperTest : public TouchInputMapperTest {
6672protected:
6673 void prepareAxes(int axes);
6674
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006675 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6676 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6677 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6678 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6679 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6680 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6681 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6682 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6683 void processId(MultiTouchInputMapper& mapper, int32_t id);
6684 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6685 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6686 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6687 void processMTSync(MultiTouchInputMapper& mapper);
6688 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006689};
6690
6691void MultiTouchInputMapperTest::prepareAxes(int axes) {
6692 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006693 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6694 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006695 }
6696 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006697 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6698 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006699 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006700 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6701 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006702 }
6703 }
6704 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006705 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6706 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006707 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006708 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006709 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006710 }
6711 }
6712 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006713 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6714 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006715 }
6716 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006717 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6718 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006719 }
6720 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006721 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6722 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006723 }
6724 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006725 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6726 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006727 }
6728 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006729 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6730 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006731 }
6732 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006733 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006734 }
6735}
6736
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006737void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6738 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006739 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006741}
6742
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006743void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6744 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006745 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006746}
6747
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006748void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6749 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006750 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006751}
6752
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006753void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006754 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006755}
6756
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006757void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006758 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006759}
6760
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006761void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6762 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006763 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006764}
6765
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006766void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006768}
6769
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006770void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006771 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006772}
6773
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006774void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006776}
6777
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006778void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006780}
6781
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006782void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006783 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006784}
6785
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006786void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6787 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006789}
6790
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006791void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006793}
6794
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006795void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006796 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006797}
6798
Michael Wrightd02c5b62014-02-10 15:10:22 -08006799TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006800 addConfigurationProperty("touch.deviceType", "touchScreen");
6801 prepareDisplay(DISPLAY_ORIENTATION_0);
6802 prepareAxes(POSITION);
6803 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006804 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006805
arthurhungdcef2dc2020-08-11 14:47:50 +08006806 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006807
6808 NotifyMotionArgs motionArgs;
6809
6810 // Two fingers down at once.
6811 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6812 processPosition(mapper, x1, y1);
6813 processMTSync(mapper);
6814 processPosition(mapper, x2, y2);
6815 processMTSync(mapper);
6816 processSync(mapper);
6817
6818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6819 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6820 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6821 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6822 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6823 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6824 ASSERT_EQ(0, motionArgs.flags);
6825 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6826 ASSERT_EQ(0, motionArgs.buttonState);
6827 ASSERT_EQ(0, motionArgs.edgeFlags);
6828 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6829 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6830 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6832 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6833 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6834 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6835 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6836
6837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6838 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6839 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6840 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6841 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006842 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006843 ASSERT_EQ(0, motionArgs.flags);
6844 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6845 ASSERT_EQ(0, motionArgs.buttonState);
6846 ASSERT_EQ(0, motionArgs.edgeFlags);
6847 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6848 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6849 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6850 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6851 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6853 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6855 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6856 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6857 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6858 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6859
6860 // Move.
6861 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6862 processPosition(mapper, x1, y1);
6863 processMTSync(mapper);
6864 processPosition(mapper, x2, y2);
6865 processMTSync(mapper);
6866 processSync(mapper);
6867
6868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6869 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6870 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6871 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6872 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6874 ASSERT_EQ(0, motionArgs.flags);
6875 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6876 ASSERT_EQ(0, motionArgs.buttonState);
6877 ASSERT_EQ(0, motionArgs.edgeFlags);
6878 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6879 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6880 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6881 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6882 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6883 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6884 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6886 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6887 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6888 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6889 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6890
6891 // First finger up.
6892 x2 += 15; y2 -= 20;
6893 processPosition(mapper, x2, y2);
6894 processMTSync(mapper);
6895 processSync(mapper);
6896
6897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6898 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6899 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6900 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6901 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006902 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006903 ASSERT_EQ(0, motionArgs.flags);
6904 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6905 ASSERT_EQ(0, motionArgs.buttonState);
6906 ASSERT_EQ(0, motionArgs.edgeFlags);
6907 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6908 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6909 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6910 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6911 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6912 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6913 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6915 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6916 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6917 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6918 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6919
6920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6921 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6922 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6923 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6924 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6925 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6926 ASSERT_EQ(0, motionArgs.flags);
6927 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6928 ASSERT_EQ(0, motionArgs.buttonState);
6929 ASSERT_EQ(0, motionArgs.edgeFlags);
6930 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6931 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6932 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6934 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6935 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6936 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6937 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6938
6939 // Move.
6940 x2 += 20; y2 -= 25;
6941 processPosition(mapper, x2, y2);
6942 processMTSync(mapper);
6943 processSync(mapper);
6944
6945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6946 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6947 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6948 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6949 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6951 ASSERT_EQ(0, motionArgs.flags);
6952 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6953 ASSERT_EQ(0, motionArgs.buttonState);
6954 ASSERT_EQ(0, motionArgs.edgeFlags);
6955 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6956 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6957 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6959 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6960 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6961 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6962 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6963
6964 // New finger down.
6965 int32_t x3 = 700, y3 = 300;
6966 processPosition(mapper, x2, y2);
6967 processMTSync(mapper);
6968 processPosition(mapper, x3, y3);
6969 processMTSync(mapper);
6970 processSync(mapper);
6971
6972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6973 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6974 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6975 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6976 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006977 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006978 ASSERT_EQ(0, motionArgs.flags);
6979 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6980 ASSERT_EQ(0, motionArgs.buttonState);
6981 ASSERT_EQ(0, motionArgs.edgeFlags);
6982 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6983 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6984 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6985 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6986 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6987 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6988 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6989 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6990 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6991 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6992 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6993 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6994
6995 // Second finger up.
6996 x3 += 30; y3 -= 20;
6997 processPosition(mapper, x3, y3);
6998 processMTSync(mapper);
6999 processSync(mapper);
7000
7001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7002 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7003 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7004 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7005 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007006 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007007 ASSERT_EQ(0, motionArgs.flags);
7008 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7009 ASSERT_EQ(0, motionArgs.buttonState);
7010 ASSERT_EQ(0, motionArgs.edgeFlags);
7011 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7012 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7013 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7014 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7015 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7017 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7018 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7019 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7020 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7021 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7022 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7023
7024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7025 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7026 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7027 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7028 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7029 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7030 ASSERT_EQ(0, motionArgs.flags);
7031 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7032 ASSERT_EQ(0, motionArgs.buttonState);
7033 ASSERT_EQ(0, motionArgs.edgeFlags);
7034 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7035 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7036 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7037 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7038 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7039 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7040 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7041 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7042
7043 // Last finger up.
7044 processMTSync(mapper);
7045 processSync(mapper);
7046
7047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7048 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7049 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7050 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7051 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7052 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7053 ASSERT_EQ(0, motionArgs.flags);
7054 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7055 ASSERT_EQ(0, motionArgs.buttonState);
7056 ASSERT_EQ(0, motionArgs.edgeFlags);
7057 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7058 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7059 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7060 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7061 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7062 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7063 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7064 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7065
7066 // Should not have sent any more keys or motions.
7067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7069}
7070
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007071TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7072 addConfigurationProperty("touch.deviceType", "touchScreen");
7073 prepareDisplay(DISPLAY_ORIENTATION_0);
7074
7075 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7076 /*fuzz*/ 0, /*resolution*/ 10);
7077 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7078 /*fuzz*/ 0, /*resolution*/ 11);
7079 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7080 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7081 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7082 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7083 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7084 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7085 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7086 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7087
7088 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7089
7090 // X and Y axes
7091 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7092 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7093 // Touch major and minor
7094 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7095 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7096 // Tool major and minor
7097 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7098 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7099}
7100
7101TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7102 addConfigurationProperty("touch.deviceType", "touchScreen");
7103 prepareDisplay(DISPLAY_ORIENTATION_0);
7104
7105 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7106 /*fuzz*/ 0, /*resolution*/ 10);
7107 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7108 /*fuzz*/ 0, /*resolution*/ 11);
7109
7110 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7111
7112 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7113
7114 // Touch major and minor
7115 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7116 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7117 // Tool major and minor
7118 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7119 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7120}
7121
Michael Wrightd02c5b62014-02-10 15:10:22 -08007122TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007123 addConfigurationProperty("touch.deviceType", "touchScreen");
7124 prepareDisplay(DISPLAY_ORIENTATION_0);
7125 prepareAxes(POSITION | ID);
7126 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007127 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007128
arthurhungdcef2dc2020-08-11 14:47:50 +08007129 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007130
7131 NotifyMotionArgs motionArgs;
7132
7133 // Two fingers down at once.
7134 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7135 processPosition(mapper, x1, y1);
7136 processId(mapper, 1);
7137 processMTSync(mapper);
7138 processPosition(mapper, x2, y2);
7139 processId(mapper, 2);
7140 processMTSync(mapper);
7141 processSync(mapper);
7142
7143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7144 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7145 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7146 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7147 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7149 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7150
7151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007152 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007153 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7154 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7155 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7156 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7157 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7158 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7159 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7160 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7161 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7162
7163 // Move.
7164 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7165 processPosition(mapper, x1, y1);
7166 processId(mapper, 1);
7167 processMTSync(mapper);
7168 processPosition(mapper, x2, y2);
7169 processId(mapper, 2);
7170 processMTSync(mapper);
7171 processSync(mapper);
7172
7173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7175 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7176 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7177 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7178 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7179 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7181 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7182 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7183 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7184
7185 // First finger up.
7186 x2 += 15; y2 -= 20;
7187 processPosition(mapper, x2, y2);
7188 processId(mapper, 2);
7189 processMTSync(mapper);
7190 processSync(mapper);
7191
7192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007193 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007194 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7195 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7196 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7197 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7198 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7199 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7200 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7202 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7203
7204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7205 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7206 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7207 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7208 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7210 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7211
7212 // Move.
7213 x2 += 20; y2 -= 25;
7214 processPosition(mapper, x2, y2);
7215 processId(mapper, 2);
7216 processMTSync(mapper);
7217 processSync(mapper);
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 // New finger down.
7228 int32_t x3 = 700, y3 = 300;
7229 processPosition(mapper, x2, y2);
7230 processId(mapper, 2);
7231 processMTSync(mapper);
7232 processPosition(mapper, x3, y3);
7233 processId(mapper, 3);
7234 processMTSync(mapper);
7235 processSync(mapper);
7236
7237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007238 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007239 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7240 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7241 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7242 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7243 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7245 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7246 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7247 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7248
7249 // Second finger up.
7250 x3 += 30; y3 -= 20;
7251 processPosition(mapper, x3, y3);
7252 processId(mapper, 3);
7253 processMTSync(mapper);
7254 processSync(mapper);
7255
7256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007257 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007258 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7259 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7260 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7261 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7262 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7264 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7265 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7266 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7267
7268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7270 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7271 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7272 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7273 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7274 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7275
7276 // Last finger up.
7277 processMTSync(mapper);
7278 processSync(mapper);
7279
7280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7281 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7282 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7283 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7284 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7286 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7287
7288 // Should not have sent any more keys or motions.
7289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7291}
7292
7293TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007294 addConfigurationProperty("touch.deviceType", "touchScreen");
7295 prepareDisplay(DISPLAY_ORIENTATION_0);
7296 prepareAxes(POSITION | ID | SLOT);
7297 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007298 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007299
arthurhungdcef2dc2020-08-11 14:47:50 +08007300 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007301
7302 NotifyMotionArgs motionArgs;
7303
7304 // Two fingers down at once.
7305 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7306 processPosition(mapper, x1, y1);
7307 processId(mapper, 1);
7308 processSlot(mapper, 1);
7309 processPosition(mapper, x2, y2);
7310 processId(mapper, 2);
7311 processSync(mapper);
7312
7313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7314 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7315 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7316 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7317 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7319 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7320
7321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007322 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007323 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7324 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7325 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7326 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7327 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7328 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7329 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7331 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7332
7333 // Move.
7334 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7335 processSlot(mapper, 0);
7336 processPosition(mapper, x1, y1);
7337 processSlot(mapper, 1);
7338 processPosition(mapper, x2, y2);
7339 processSync(mapper);
7340
7341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7343 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7344 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7345 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7346 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7347 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7349 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7350 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7351 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7352
7353 // First finger up.
7354 x2 += 15; y2 -= 20;
7355 processSlot(mapper, 0);
7356 processId(mapper, -1);
7357 processSlot(mapper, 1);
7358 processPosition(mapper, x2, y2);
7359 processSync(mapper);
7360
7361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007362 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007363 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7364 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7365 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7366 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7367 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7369 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7371 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7372
7373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7375 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7376 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7377 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7378 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7379 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7380
7381 // Move.
7382 x2 += 20; y2 -= 25;
7383 processPosition(mapper, x2, y2);
7384 processSync(mapper);
7385
7386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7387 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7388 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7389 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7390 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7392 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7393
7394 // New finger down.
7395 int32_t x3 = 700, y3 = 300;
7396 processPosition(mapper, x2, y2);
7397 processSlot(mapper, 0);
7398 processId(mapper, 3);
7399 processPosition(mapper, x3, y3);
7400 processSync(mapper);
7401
7402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007403 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007404 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7405 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7406 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7407 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7408 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7410 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7412 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7413
7414 // Second finger up.
7415 x3 += 30; y3 -= 20;
7416 processSlot(mapper, 1);
7417 processId(mapper, -1);
7418 processSlot(mapper, 0);
7419 processPosition(mapper, x3, y3);
7420 processSync(mapper);
7421
7422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007423 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007424 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7425 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7426 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7427 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7428 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7429 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7430 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7432 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7433
7434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7435 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7436 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7437 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7438 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7440 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7441
7442 // Last finger up.
7443 processId(mapper, -1);
7444 processSync(mapper);
7445
7446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7447 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7448 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7449 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7450 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7452 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7453
7454 // Should not have sent any more keys or motions.
7455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7457}
7458
7459TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007460 addConfigurationProperty("touch.deviceType", "touchScreen");
7461 prepareDisplay(DISPLAY_ORIENTATION_0);
7462 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007463 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007464
7465 // These calculations are based on the input device calibration documentation.
7466 int32_t rawX = 100;
7467 int32_t rawY = 200;
7468 int32_t rawTouchMajor = 7;
7469 int32_t rawTouchMinor = 6;
7470 int32_t rawToolMajor = 9;
7471 int32_t rawToolMinor = 8;
7472 int32_t rawPressure = 11;
7473 int32_t rawDistance = 0;
7474 int32_t rawOrientation = 3;
7475 int32_t id = 5;
7476
7477 float x = toDisplayX(rawX);
7478 float y = toDisplayY(rawY);
7479 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7480 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7481 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7482 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7483 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7484 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7485 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7486 float distance = float(rawDistance);
7487
7488 processPosition(mapper, rawX, rawY);
7489 processTouchMajor(mapper, rawTouchMajor);
7490 processTouchMinor(mapper, rawTouchMinor);
7491 processToolMajor(mapper, rawToolMajor);
7492 processToolMinor(mapper, rawToolMinor);
7493 processPressure(mapper, rawPressure);
7494 processOrientation(mapper, rawOrientation);
7495 processDistance(mapper, rawDistance);
7496 processId(mapper, id);
7497 processMTSync(mapper);
7498 processSync(mapper);
7499
7500 NotifyMotionArgs args;
7501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7502 ASSERT_EQ(0, args.pointerProperties[0].id);
7503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7504 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7505 orientation, distance));
7506}
7507
7508TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007509 addConfigurationProperty("touch.deviceType", "touchScreen");
7510 prepareDisplay(DISPLAY_ORIENTATION_0);
7511 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7512 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007513 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007514
7515 // These calculations are based on the input device calibration documentation.
7516 int32_t rawX = 100;
7517 int32_t rawY = 200;
7518 int32_t rawTouchMajor = 140;
7519 int32_t rawTouchMinor = 120;
7520 int32_t rawToolMajor = 180;
7521 int32_t rawToolMinor = 160;
7522
7523 float x = toDisplayX(rawX);
7524 float y = toDisplayY(rawY);
7525 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7526 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7527 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7528 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7529 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7530
7531 processPosition(mapper, rawX, rawY);
7532 processTouchMajor(mapper, rawTouchMajor);
7533 processTouchMinor(mapper, rawTouchMinor);
7534 processToolMajor(mapper, rawToolMajor);
7535 processToolMinor(mapper, rawToolMinor);
7536 processMTSync(mapper);
7537 processSync(mapper);
7538
7539 NotifyMotionArgs args;
7540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7542 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7543}
7544
7545TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007546 addConfigurationProperty("touch.deviceType", "touchScreen");
7547 prepareDisplay(DISPLAY_ORIENTATION_0);
7548 prepareAxes(POSITION | TOUCH | TOOL);
7549 addConfigurationProperty("touch.size.calibration", "diameter");
7550 addConfigurationProperty("touch.size.scale", "10");
7551 addConfigurationProperty("touch.size.bias", "160");
7552 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007553 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007554
7555 // These calculations are based on the input device calibration documentation.
7556 // Note: We only provide a single common touch/tool value because the device is assumed
7557 // not to emit separate values for each pointer (isSummed = 1).
7558 int32_t rawX = 100;
7559 int32_t rawY = 200;
7560 int32_t rawX2 = 150;
7561 int32_t rawY2 = 250;
7562 int32_t rawTouchMajor = 5;
7563 int32_t rawToolMajor = 8;
7564
7565 float x = toDisplayX(rawX);
7566 float y = toDisplayY(rawY);
7567 float x2 = toDisplayX(rawX2);
7568 float y2 = toDisplayY(rawY2);
7569 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7570 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7571 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7572
7573 processPosition(mapper, rawX, rawY);
7574 processTouchMajor(mapper, rawTouchMajor);
7575 processToolMajor(mapper, rawToolMajor);
7576 processMTSync(mapper);
7577 processPosition(mapper, rawX2, rawY2);
7578 processTouchMajor(mapper, rawTouchMajor);
7579 processToolMajor(mapper, rawToolMajor);
7580 processMTSync(mapper);
7581 processSync(mapper);
7582
7583 NotifyMotionArgs args;
7584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7585 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7586
7587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007588 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007589 ASSERT_EQ(size_t(2), args.pointerCount);
7590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7591 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7593 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7594}
7595
7596TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007597 addConfigurationProperty("touch.deviceType", "touchScreen");
7598 prepareDisplay(DISPLAY_ORIENTATION_0);
7599 prepareAxes(POSITION | TOUCH | TOOL);
7600 addConfigurationProperty("touch.size.calibration", "area");
7601 addConfigurationProperty("touch.size.scale", "43");
7602 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007603 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007604
7605 // These calculations are based on the input device calibration documentation.
7606 int32_t rawX = 100;
7607 int32_t rawY = 200;
7608 int32_t rawTouchMajor = 5;
7609 int32_t rawToolMajor = 8;
7610
7611 float x = toDisplayX(rawX);
7612 float y = toDisplayY(rawY);
7613 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7614 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7615 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7616
7617 processPosition(mapper, rawX, rawY);
7618 processTouchMajor(mapper, rawTouchMajor);
7619 processToolMajor(mapper, rawToolMajor);
7620 processMTSync(mapper);
7621 processSync(mapper);
7622
7623 NotifyMotionArgs args;
7624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7626 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7627}
7628
7629TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007630 addConfigurationProperty("touch.deviceType", "touchScreen");
7631 prepareDisplay(DISPLAY_ORIENTATION_0);
7632 prepareAxes(POSITION | PRESSURE);
7633 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7634 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007635 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007636
Michael Wrightaa449c92017-12-13 21:21:43 +00007637 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007638 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007639 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7640 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7641 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7642
Michael Wrightd02c5b62014-02-10 15:10:22 -08007643 // These calculations are based on the input device calibration documentation.
7644 int32_t rawX = 100;
7645 int32_t rawY = 200;
7646 int32_t rawPressure = 60;
7647
7648 float x = toDisplayX(rawX);
7649 float y = toDisplayY(rawY);
7650 float pressure = float(rawPressure) * 0.01f;
7651
7652 processPosition(mapper, rawX, rawY);
7653 processPressure(mapper, rawPressure);
7654 processMTSync(mapper);
7655 processSync(mapper);
7656
7657 NotifyMotionArgs args;
7658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7660 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7661}
7662
7663TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007664 addConfigurationProperty("touch.deviceType", "touchScreen");
7665 prepareDisplay(DISPLAY_ORIENTATION_0);
7666 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007667 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007668
7669 NotifyMotionArgs motionArgs;
7670 NotifyKeyArgs keyArgs;
7671
7672 processId(mapper, 1);
7673 processPosition(mapper, 100, 200);
7674 processSync(mapper);
7675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7676 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7677 ASSERT_EQ(0, motionArgs.buttonState);
7678
7679 // press BTN_LEFT, release BTN_LEFT
7680 processKey(mapper, BTN_LEFT, 1);
7681 processSync(mapper);
7682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7683 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7684 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7685
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7687 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7688 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7689
Michael Wrightd02c5b62014-02-10 15:10:22 -08007690 processKey(mapper, BTN_LEFT, 0);
7691 processSync(mapper);
7692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007693 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007694 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007695
7696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007697 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007698 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007699
7700 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7701 processKey(mapper, BTN_RIGHT, 1);
7702 processKey(mapper, BTN_MIDDLE, 1);
7703 processSync(mapper);
7704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7705 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7706 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7707 motionArgs.buttonState);
7708
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7710 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7711 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7712
7713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7714 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7715 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7716 motionArgs.buttonState);
7717
Michael Wrightd02c5b62014-02-10 15:10:22 -08007718 processKey(mapper, BTN_RIGHT, 0);
7719 processSync(mapper);
7720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007721 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007722 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007723
7724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007726 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007727
7728 processKey(mapper, BTN_MIDDLE, 0);
7729 processSync(mapper);
7730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007731 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007732 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007733
7734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007736 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007737
7738 // press BTN_BACK, release BTN_BACK
7739 processKey(mapper, BTN_BACK, 1);
7740 processSync(mapper);
7741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7742 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7743 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007744
Michael Wrightd02c5b62014-02-10 15:10:22 -08007745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007747 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7748
7749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7750 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7751 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007752
7753 processKey(mapper, BTN_BACK, 0);
7754 processSync(mapper);
7755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007756 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007757 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007758
7759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007761 ASSERT_EQ(0, motionArgs.buttonState);
7762
Michael Wrightd02c5b62014-02-10 15:10:22 -08007763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7764 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7765 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7766
7767 // press BTN_SIDE, release BTN_SIDE
7768 processKey(mapper, BTN_SIDE, 1);
7769 processSync(mapper);
7770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7771 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7772 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007773
Michael Wrightd02c5b62014-02-10 15:10:22 -08007774 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(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7777
7778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7779 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7780 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007781
7782 processKey(mapper, BTN_SIDE, 0);
7783 processSync(mapper);
7784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007785 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007786 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007787
7788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007790 ASSERT_EQ(0, motionArgs.buttonState);
7791
Michael Wrightd02c5b62014-02-10 15:10:22 -08007792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7793 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7794 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7795
7796 // press BTN_FORWARD, release BTN_FORWARD
7797 processKey(mapper, BTN_FORWARD, 1);
7798 processSync(mapper);
7799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7800 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7801 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007802
Michael Wrightd02c5b62014-02-10 15:10:22 -08007803 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(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7806
7807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7808 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7809 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007810
7811 processKey(mapper, BTN_FORWARD, 0);
7812 processSync(mapper);
7813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007814 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007815 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007816
7817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007818 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007819 ASSERT_EQ(0, motionArgs.buttonState);
7820
Michael Wrightd02c5b62014-02-10 15:10:22 -08007821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7822 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7823 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7824
7825 // press BTN_EXTRA, release BTN_EXTRA
7826 processKey(mapper, BTN_EXTRA, 1);
7827 processSync(mapper);
7828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7829 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7830 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007831
Michael Wrightd02c5b62014-02-10 15:10:22 -08007832 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(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7835
7836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7837 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7838 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007839
7840 processKey(mapper, BTN_EXTRA, 0);
7841 processSync(mapper);
7842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007843 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007844 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007845
7846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007847 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007848 ASSERT_EQ(0, motionArgs.buttonState);
7849
Michael Wrightd02c5b62014-02-10 15:10:22 -08007850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7851 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7852 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7853
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7855
Michael Wrightd02c5b62014-02-10 15:10:22 -08007856 // press BTN_STYLUS, release BTN_STYLUS
7857 processKey(mapper, BTN_STYLUS, 1);
7858 processSync(mapper);
7859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7860 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007861 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7862
7863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7864 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7865 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007866
7867 processKey(mapper, BTN_STYLUS, 0);
7868 processSync(mapper);
7869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007870 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007871 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007872
7873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007875 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007876
7877 // press BTN_STYLUS2, release BTN_STYLUS2
7878 processKey(mapper, BTN_STYLUS2, 1);
7879 processSync(mapper);
7880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7881 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007882 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7883
7884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7885 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7886 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007887
7888 processKey(mapper, BTN_STYLUS2, 0);
7889 processSync(mapper);
7890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007891 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007892 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007893
7894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007895 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007896 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007897
7898 // release touch
7899 processId(mapper, -1);
7900 processSync(mapper);
7901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7902 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7903 ASSERT_EQ(0, motionArgs.buttonState);
7904}
7905
7906TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007907 addConfigurationProperty("touch.deviceType", "touchScreen");
7908 prepareDisplay(DISPLAY_ORIENTATION_0);
7909 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007910 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007911
7912 NotifyMotionArgs motionArgs;
7913
7914 // default tool type is finger
7915 processId(mapper, 1);
7916 processPosition(mapper, 100, 200);
7917 processSync(mapper);
7918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7919 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7920 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7921
7922 // eraser
7923 processKey(mapper, BTN_TOOL_RUBBER, 1);
7924 processSync(mapper);
7925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7926 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7927 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7928
7929 // stylus
7930 processKey(mapper, BTN_TOOL_RUBBER, 0);
7931 processKey(mapper, BTN_TOOL_PEN, 1);
7932 processSync(mapper);
7933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7934 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7935 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7936
7937 // brush
7938 processKey(mapper, BTN_TOOL_PEN, 0);
7939 processKey(mapper, BTN_TOOL_BRUSH, 1);
7940 processSync(mapper);
7941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7942 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7943 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7944
7945 // pencil
7946 processKey(mapper, BTN_TOOL_BRUSH, 0);
7947 processKey(mapper, BTN_TOOL_PENCIL, 1);
7948 processSync(mapper);
7949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7951 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7952
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007953 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007954 processKey(mapper, BTN_TOOL_PENCIL, 0);
7955 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7956 processSync(mapper);
7957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7958 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7959 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7960
7961 // mouse
7962 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7963 processKey(mapper, BTN_TOOL_MOUSE, 1);
7964 processSync(mapper);
7965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7967 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7968
7969 // lens
7970 processKey(mapper, BTN_TOOL_MOUSE, 0);
7971 processKey(mapper, BTN_TOOL_LENS, 1);
7972 processSync(mapper);
7973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7974 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7975 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7976
7977 // double-tap
7978 processKey(mapper, BTN_TOOL_LENS, 0);
7979 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7980 processSync(mapper);
7981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7982 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7983 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7984
7985 // triple-tap
7986 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7987 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7988 processSync(mapper);
7989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7990 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7991 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7992
7993 // quad-tap
7994 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7995 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7996 processSync(mapper);
7997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7998 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7999 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8000
8001 // finger
8002 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8003 processKey(mapper, BTN_TOOL_FINGER, 1);
8004 processSync(mapper);
8005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8007 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8008
8009 // stylus trumps finger
8010 processKey(mapper, BTN_TOOL_PEN, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
8015
8016 // eraser trumps stylus
8017 processKey(mapper, BTN_TOOL_RUBBER, 1);
8018 processSync(mapper);
8019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8021 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8022
8023 // mouse trumps eraser
8024 processKey(mapper, BTN_TOOL_MOUSE, 1);
8025 processSync(mapper);
8026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8027 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8028 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8029
8030 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8031 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8032 processSync(mapper);
8033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8034 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8035 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8036
8037 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8038 processToolType(mapper, MT_TOOL_PEN);
8039 processSync(mapper);
8040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8041 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8042 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8043
8044 // back to default tool type
8045 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8046 processKey(mapper, BTN_TOOL_MOUSE, 0);
8047 processKey(mapper, BTN_TOOL_RUBBER, 0);
8048 processKey(mapper, BTN_TOOL_PEN, 0);
8049 processKey(mapper, BTN_TOOL_FINGER, 0);
8050 processSync(mapper);
8051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8052 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8053 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8054}
8055
8056TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008057 addConfigurationProperty("touch.deviceType", "touchScreen");
8058 prepareDisplay(DISPLAY_ORIENTATION_0);
8059 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008060 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008061 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008062
8063 NotifyMotionArgs motionArgs;
8064
8065 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8066 processId(mapper, 1);
8067 processPosition(mapper, 100, 200);
8068 processSync(mapper);
8069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8070 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8071 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8072 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8073
8074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8075 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8076 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8077 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8078
8079 // move a little
8080 processPosition(mapper, 150, 250);
8081 processSync(mapper);
8082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8083 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8085 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8086
8087 // down when BTN_TOUCH is pressed, pressure defaults to 1
8088 processKey(mapper, BTN_TOUCH, 1);
8089 processSync(mapper);
8090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8091 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8093 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8094
8095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8096 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8097 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8098 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8099
8100 // up when BTN_TOUCH is released, hover restored
8101 processKey(mapper, BTN_TOUCH, 0);
8102 processSync(mapper);
8103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8104 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8105 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8106 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8107
8108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8109 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8110 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8111 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8112
8113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8114 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8115 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8116 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8117
8118 // exit hover when pointer goes away
8119 processId(mapper, -1);
8120 processSync(mapper);
8121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8122 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8123 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8124 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8125}
8126
8127TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008128 addConfigurationProperty("touch.deviceType", "touchScreen");
8129 prepareDisplay(DISPLAY_ORIENTATION_0);
8130 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008131 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008132
8133 NotifyMotionArgs motionArgs;
8134
8135 // initially hovering because pressure is 0
8136 processId(mapper, 1);
8137 processPosition(mapper, 100, 200);
8138 processPressure(mapper, 0);
8139 processSync(mapper);
8140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8141 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8142 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8143 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8144
8145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8146 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8147 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8148 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8149
8150 // move a little
8151 processPosition(mapper, 150, 250);
8152 processSync(mapper);
8153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8154 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8155 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8156 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8157
8158 // down when pressure becomes non-zero
8159 processPressure(mapper, RAW_PRESSURE_MAX);
8160 processSync(mapper);
8161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8162 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8163 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8164 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8165
8166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8167 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8169 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8170
8171 // up when pressure becomes 0, hover restored
8172 processPressure(mapper, 0);
8173 processSync(mapper);
8174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8175 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8176 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8177 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8178
8179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8180 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8182 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8183
8184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8185 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8186 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8187 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8188
8189 // exit hover when pointer goes away
8190 processId(mapper, -1);
8191 processSync(mapper);
8192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8193 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8194 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8195 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8196}
8197
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008198/**
8199 * Set the input device port <--> display port associations, and check that the
8200 * events are routed to the display that matches the display port.
8201 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8202 */
8203TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008204 const std::string usb2 = "USB2";
8205 const uint8_t hdmi1 = 0;
8206 const uint8_t hdmi2 = 1;
8207 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008208 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008209
8210 addConfigurationProperty("touch.deviceType", "touchScreen");
8211 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008212 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008213
8214 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8215 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8216
8217 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8218 // for this input device is specified, and the matching viewport is not present,
8219 // the input device should be disabled (at the mapper level).
8220
8221 // Add viewport for display 2 on hdmi2
8222 prepareSecondaryDisplay(type, hdmi2);
8223 // Send a touch event
8224 processPosition(mapper, 100, 100);
8225 processSync(mapper);
8226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8227
8228 // Add viewport for display 1 on hdmi1
8229 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8230 // Send a touch event again
8231 processPosition(mapper, 100, 100);
8232 processSync(mapper);
8233
8234 NotifyMotionArgs args;
8235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8236 ASSERT_EQ(DISPLAY_ID, args.displayId);
8237}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008238
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008239TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008240 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008241 std::shared_ptr<FakePointerController> fakePointerController =
8242 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008243 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008244 fakePointerController->setPosition(100, 200);
8245 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008246 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008247
Garfield Tan888a6a42020-01-09 11:39:16 -08008248 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008249 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008250
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008251 prepareDisplay(DISPLAY_ORIENTATION_0);
8252 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008253 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008254
8255 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008256 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008257
8258 NotifyMotionArgs motionArgs;
8259 processPosition(mapper, 100, 100);
8260 processSync(mapper);
8261
8262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8263 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8264 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8265}
8266
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008267/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008268 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8269 */
8270TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8271 addConfigurationProperty("touch.deviceType", "touchScreen");
8272 prepareAxes(POSITION);
8273 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8274
8275 prepareDisplay(DISPLAY_ORIENTATION_0);
8276 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8277 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8278 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8279 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8280
8281 NotifyMotionArgs args;
8282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8283 ASSERT_EQ(26, args.readTime);
8284
8285 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8286 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8287 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8288
8289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8290 ASSERT_EQ(33, args.readTime);
8291}
8292
8293/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008294 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8295 * events should not be delivered to the listener.
8296 */
8297TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8298 addConfigurationProperty("touch.deviceType", "touchScreen");
8299 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8300 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8301 ViewportType::INTERNAL);
8302 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8303 prepareAxes(POSITION);
8304 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8305
8306 NotifyMotionArgs motionArgs;
8307 processPosition(mapper, 100, 100);
8308 processSync(mapper);
8309
8310 mFakeListener->assertNotifyMotionWasNotCalled();
8311}
8312
Garfield Tanc734e4f2021-01-15 20:01:39 -08008313TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8314 addConfigurationProperty("touch.deviceType", "touchScreen");
8315 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8316 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8317 ViewportType::INTERNAL);
8318 std::optional<DisplayViewport> optionalDisplayViewport =
8319 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8320 ASSERT_TRUE(optionalDisplayViewport.has_value());
8321 DisplayViewport displayViewport = *optionalDisplayViewport;
8322
8323 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8324 prepareAxes(POSITION);
8325 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8326
8327 // Finger down
8328 int32_t x = 100, y = 100;
8329 processPosition(mapper, x, y);
8330 processSync(mapper);
8331
8332 NotifyMotionArgs motionArgs;
8333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8334 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8335
8336 // Deactivate display viewport
8337 displayViewport.isActive = false;
8338 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8339 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8340
8341 // Finger move
8342 x += 10, y += 10;
8343 processPosition(mapper, x, y);
8344 processSync(mapper);
8345
8346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8347 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8348
8349 // Reactivate display viewport
8350 displayViewport.isActive = true;
8351 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8352 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8353
8354 // Finger move again
8355 x += 10, y += 10;
8356 processPosition(mapper, x, y);
8357 processSync(mapper);
8358
8359 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8360 // no pointer on the touch device.
8361 mFakeListener->assertNotifyMotionWasNotCalled();
8362}
8363
Arthur Hung7c645402019-01-25 17:45:42 +08008364TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8365 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008366 prepareAxes(POSITION | ID | SLOT);
8367 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008368 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008369
8370 // Create the second touch screen device, and enable multi fingers.
8371 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008372 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008373 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008374 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008375 std::shared_ptr<InputDevice> device2 =
8376 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
8377 Flags<InputDeviceClass>(0));
8378
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008379 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8380 0 /*flat*/, 0 /*fuzz*/);
8381 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8382 0 /*flat*/, 0 /*fuzz*/);
8383 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8384 0 /*flat*/, 0 /*fuzz*/);
8385 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8386 0 /*flat*/, 0 /*fuzz*/);
8387 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8388 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8389 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008390
8391 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008392 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008393 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8394 device2->reset(ARBITRARY_TIME);
8395
8396 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008397 std::shared_ptr<FakePointerController> fakePointerController =
8398 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008399 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008400
8401 // Setup policy for associated displays and show touches.
8402 const uint8_t hdmi1 = 0;
8403 const uint8_t hdmi2 = 1;
8404 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8405 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8406 mFakePolicy->setShowTouches(true);
8407
8408 // Create displays.
8409 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008410 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008411
8412 // Default device will reconfigure above, need additional reconfiguration for another device.
8413 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008414 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08008415
8416 // Two fingers down at default display.
8417 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8418 processPosition(mapper, x1, y1);
8419 processId(mapper, 1);
8420 processSlot(mapper, 1);
8421 processPosition(mapper, x2, y2);
8422 processId(mapper, 2);
8423 processSync(mapper);
8424
8425 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8426 fakePointerController->getSpots().find(DISPLAY_ID);
8427 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8428 ASSERT_EQ(size_t(2), iter->second.size());
8429
8430 // Two fingers down at second display.
8431 processPosition(mapper2, x1, y1);
8432 processId(mapper2, 1);
8433 processSlot(mapper2, 1);
8434 processPosition(mapper2, x2, y2);
8435 processId(mapper2, 2);
8436 processSync(mapper2);
8437
8438 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8439 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8440 ASSERT_EQ(size_t(2), iter->second.size());
8441}
8442
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008443TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008444 prepareAxes(POSITION);
8445 addConfigurationProperty("touch.deviceType", "touchScreen");
8446 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008447 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008448
8449 NotifyMotionArgs motionArgs;
8450 // Unrotated video frame
8451 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8452 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008453 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008454 processPosition(mapper, 100, 200);
8455 processSync(mapper);
8456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8457 ASSERT_EQ(frames, motionArgs.videoFrames);
8458
8459 // Subsequent touch events should not have any videoframes
8460 // This is implemented separately in FakeEventHub,
8461 // but that should match the behaviour of TouchVideoDevice.
8462 processPosition(mapper, 200, 200);
8463 processSync(mapper);
8464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8465 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8466}
8467
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008468TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008469 prepareAxes(POSITION);
8470 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008471 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008472 // Unrotated video frame
8473 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8474 NotifyMotionArgs motionArgs;
8475
8476 // Test all 4 orientations
8477 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008478 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8479 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8480 clearViewports();
8481 prepareDisplay(orientation);
8482 std::vector<TouchVideoFrame> frames{frame};
8483 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8484 processPosition(mapper, 100, 200);
8485 processSync(mapper);
8486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8487 ASSERT_EQ(frames, motionArgs.videoFrames);
8488 }
8489}
8490
8491TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8492 prepareAxes(POSITION);
8493 addConfigurationProperty("touch.deviceType", "touchScreen");
8494 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8495 // orientation-aware are affected by display rotation.
8496 addConfigurationProperty("touch.orientationAware", "0");
8497 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8498 // Unrotated video frame
8499 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8500 NotifyMotionArgs motionArgs;
8501
8502 // Test all 4 orientations
8503 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008504 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8505 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8506 clearViewports();
8507 prepareDisplay(orientation);
8508 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008509 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008510 processPosition(mapper, 100, 200);
8511 processSync(mapper);
8512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008513 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8514 // compared to the display. This is so that when the window transform (which contains the
8515 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8516 // window's coordinate space.
8517 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008518 ASSERT_EQ(frames, motionArgs.videoFrames);
8519 }
8520}
8521
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008522TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008523 prepareAxes(POSITION);
8524 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008525 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008526 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8527 // so mix these.
8528 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8529 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8530 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8531 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8532 NotifyMotionArgs motionArgs;
8533
8534 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008535 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008536 processPosition(mapper, 100, 200);
8537 processSync(mapper);
8538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008539 ASSERT_EQ(frames, motionArgs.videoFrames);
8540}
8541
8542TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8543 prepareAxes(POSITION);
8544 addConfigurationProperty("touch.deviceType", "touchScreen");
8545 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8546 // orientation-aware are affected by display rotation.
8547 addConfigurationProperty("touch.orientationAware", "0");
8548 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8549 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8550 // so mix these.
8551 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8552 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8553 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8554 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8555 NotifyMotionArgs motionArgs;
8556
8557 prepareDisplay(DISPLAY_ORIENTATION_90);
8558 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8559 processPosition(mapper, 100, 200);
8560 processSync(mapper);
8561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8562 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8563 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8564 // compared to the display. This is so that when the window transform (which contains the
8565 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8566 // window's coordinate space.
8567 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8568 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008569 ASSERT_EQ(frames, motionArgs.videoFrames);
8570}
8571
Arthur Hung9da14732019-09-02 16:16:58 +08008572/**
8573 * If we had defined port associations, but the viewport is not ready, the touch device would be
8574 * expected to be disabled, and it should be enabled after the viewport has found.
8575 */
8576TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008577 constexpr uint8_t hdmi2 = 1;
8578 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008579 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008580
8581 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8582
8583 addConfigurationProperty("touch.deviceType", "touchScreen");
8584 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008585 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008586
8587 ASSERT_EQ(mDevice->isEnabled(), false);
8588
8589 // Add display on hdmi2, the device should be enabled and can receive touch event.
8590 prepareSecondaryDisplay(type, hdmi2);
8591 ASSERT_EQ(mDevice->isEnabled(), true);
8592
8593 // Send a touch event.
8594 processPosition(mapper, 100, 100);
8595 processSync(mapper);
8596
8597 NotifyMotionArgs args;
8598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8599 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8600}
8601
Arthur Hung421eb1c2020-01-16 00:09:42 +08008602TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008603 addConfigurationProperty("touch.deviceType", "touchScreen");
8604 prepareDisplay(DISPLAY_ORIENTATION_0);
8605 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008606 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008607
8608 NotifyMotionArgs motionArgs;
8609
8610 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8611 // finger down
8612 processId(mapper, 1);
8613 processPosition(mapper, x1, y1);
8614 processSync(mapper);
8615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8616 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8617 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8618
8619 // finger move
8620 processId(mapper, 1);
8621 processPosition(mapper, x2, y2);
8622 processSync(mapper);
8623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8625 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8626
8627 // finger up.
8628 processId(mapper, -1);
8629 processSync(mapper);
8630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8631 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8633
8634 // new finger down
8635 processId(mapper, 1);
8636 processPosition(mapper, x3, y3);
8637 processSync(mapper);
8638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8639 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8640 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8641}
8642
8643/**
arthurhungcc7f9802020-04-30 17:55:40 +08008644 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8645 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008646 */
arthurhungcc7f9802020-04-30 17:55:40 +08008647TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008648 addConfigurationProperty("touch.deviceType", "touchScreen");
8649 prepareDisplay(DISPLAY_ORIENTATION_0);
8650 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008651 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008652
8653 NotifyMotionArgs motionArgs;
8654
8655 // default tool type is finger
8656 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008657 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008658 processPosition(mapper, x1, y1);
8659 processSync(mapper);
8660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8661 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8662 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8663
8664 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8665 processToolType(mapper, MT_TOOL_PALM);
8666 processSync(mapper);
8667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8668 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8669
8670 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008671 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008672 processPosition(mapper, x2, y2);
8673 processSync(mapper);
8674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8675
8676 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008677 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008678 processSync(mapper);
8679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8680
8681 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008682 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008683 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008684 processPosition(mapper, x3, y3);
8685 processSync(mapper);
8686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8687 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8688 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8689}
8690
arthurhungbf89a482020-04-17 17:37:55 +08008691/**
arthurhungcc7f9802020-04-30 17:55:40 +08008692 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8693 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008694 */
arthurhungcc7f9802020-04-30 17:55:40 +08008695TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008696 addConfigurationProperty("touch.deviceType", "touchScreen");
8697 prepareDisplay(DISPLAY_ORIENTATION_0);
8698 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8699 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8700
8701 NotifyMotionArgs motionArgs;
8702
8703 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008704 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8705 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008706 processPosition(mapper, x1, y1);
8707 processSync(mapper);
8708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8709 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8711
8712 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008713 processSlot(mapper, SECOND_SLOT);
8714 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008715 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008716 processSync(mapper);
8717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008718 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008719 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8720
8721 // If the tool type of the first finger changes to MT_TOOL_PALM,
8722 // we expect to receive ACTION_POINTER_UP with cancel flag.
8723 processSlot(mapper, FIRST_SLOT);
8724 processId(mapper, FIRST_TRACKING_ID);
8725 processToolType(mapper, MT_TOOL_PALM);
8726 processSync(mapper);
8727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008728 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008729 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8730
8731 // The following MOVE events of second finger should be processed.
8732 processSlot(mapper, SECOND_SLOT);
8733 processId(mapper, SECOND_TRACKING_ID);
8734 processPosition(mapper, x2 + 1, y2 + 1);
8735 processSync(mapper);
8736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8737 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8738 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8739
8740 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8741 // it. Second finger receive move.
8742 processSlot(mapper, FIRST_SLOT);
8743 processId(mapper, INVALID_TRACKING_ID);
8744 processSync(mapper);
8745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8747 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8748
8749 // Second finger keeps moving.
8750 processSlot(mapper, SECOND_SLOT);
8751 processId(mapper, SECOND_TRACKING_ID);
8752 processPosition(mapper, x2 + 2, y2 + 2);
8753 processSync(mapper);
8754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8755 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8756 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8757
8758 // Second finger up.
8759 processId(mapper, INVALID_TRACKING_ID);
8760 processSync(mapper);
8761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8762 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8763 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8764}
8765
8766/**
8767 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8768 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8769 */
8770TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8771 addConfigurationProperty("touch.deviceType", "touchScreen");
8772 prepareDisplay(DISPLAY_ORIENTATION_0);
8773 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8774 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8775
8776 NotifyMotionArgs motionArgs;
8777
8778 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8779 // First finger down.
8780 processId(mapper, FIRST_TRACKING_ID);
8781 processPosition(mapper, x1, y1);
8782 processSync(mapper);
8783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8784 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8785 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8786
8787 // Second finger down.
8788 processSlot(mapper, SECOND_SLOT);
8789 processId(mapper, SECOND_TRACKING_ID);
8790 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008791 processSync(mapper);
8792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008793 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08008794 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8795
arthurhungcc7f9802020-04-30 17:55:40 +08008796 // If the tool type of the first finger changes to MT_TOOL_PALM,
8797 // we expect to receive ACTION_POINTER_UP with cancel flag.
8798 processSlot(mapper, FIRST_SLOT);
8799 processId(mapper, FIRST_TRACKING_ID);
8800 processToolType(mapper, MT_TOOL_PALM);
8801 processSync(mapper);
8802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008803 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008804 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8805
8806 // Second finger keeps moving.
8807 processSlot(mapper, SECOND_SLOT);
8808 processId(mapper, SECOND_TRACKING_ID);
8809 processPosition(mapper, x2 + 1, y2 + 1);
8810 processSync(mapper);
8811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8812 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8813
8814 // second finger becomes palm, receive cancel due to only 1 finger is active.
8815 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008816 processToolType(mapper, MT_TOOL_PALM);
8817 processSync(mapper);
8818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8819 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8820
arthurhungcc7f9802020-04-30 17:55:40 +08008821 // third finger down.
8822 processSlot(mapper, THIRD_SLOT);
8823 processId(mapper, THIRD_TRACKING_ID);
8824 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008825 processPosition(mapper, x3, y3);
8826 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8828 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8829 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008830 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8831
8832 // third finger move
8833 processId(mapper, THIRD_TRACKING_ID);
8834 processPosition(mapper, x3 + 1, y3 + 1);
8835 processSync(mapper);
8836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8837 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8838
8839 // first finger up, third finger receive move.
8840 processSlot(mapper, FIRST_SLOT);
8841 processId(mapper, INVALID_TRACKING_ID);
8842 processSync(mapper);
8843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8844 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8845 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8846
8847 // second finger up, third finger receive move.
8848 processSlot(mapper, SECOND_SLOT);
8849 processId(mapper, INVALID_TRACKING_ID);
8850 processSync(mapper);
8851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8852 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8853 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8854
8855 // third finger up.
8856 processSlot(mapper, THIRD_SLOT);
8857 processId(mapper, INVALID_TRACKING_ID);
8858 processSync(mapper);
8859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8860 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8861 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8862}
8863
8864/**
8865 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8866 * and the active finger could still be allowed to receive the events
8867 */
8868TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8869 addConfigurationProperty("touch.deviceType", "touchScreen");
8870 prepareDisplay(DISPLAY_ORIENTATION_0);
8871 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8872 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8873
8874 NotifyMotionArgs motionArgs;
8875
8876 // default tool type is finger
8877 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8878 processId(mapper, FIRST_TRACKING_ID);
8879 processPosition(mapper, x1, y1);
8880 processSync(mapper);
8881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8882 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8883 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8884
8885 // Second finger down.
8886 processSlot(mapper, SECOND_SLOT);
8887 processId(mapper, SECOND_TRACKING_ID);
8888 processPosition(mapper, x2, y2);
8889 processSync(mapper);
8890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008891 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008892 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8893
8894 // If the tool type of the second finger changes to MT_TOOL_PALM,
8895 // we expect to receive ACTION_POINTER_UP with cancel flag.
8896 processId(mapper, SECOND_TRACKING_ID);
8897 processToolType(mapper, MT_TOOL_PALM);
8898 processSync(mapper);
8899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008900 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008901 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8902
8903 // The following MOVE event should be processed.
8904 processSlot(mapper, FIRST_SLOT);
8905 processId(mapper, FIRST_TRACKING_ID);
8906 processPosition(mapper, x1 + 1, y1 + 1);
8907 processSync(mapper);
8908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8909 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8910 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8911
8912 // second finger up.
8913 processSlot(mapper, SECOND_SLOT);
8914 processId(mapper, INVALID_TRACKING_ID);
8915 processSync(mapper);
8916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8917 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8918
8919 // first finger keep moving
8920 processSlot(mapper, FIRST_SLOT);
8921 processId(mapper, FIRST_TRACKING_ID);
8922 processPosition(mapper, x1 + 2, y1 + 2);
8923 processSync(mapper);
8924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8925 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8926
8927 // first finger up.
8928 processId(mapper, INVALID_TRACKING_ID);
8929 processSync(mapper);
8930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8931 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8932 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008933}
8934
Arthur Hung9ad18942021-06-19 02:04:46 +00008935/**
8936 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
8937 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
8938 * cause slot be valid again.
8939 */
8940TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
8941 addConfigurationProperty("touch.deviceType", "touchScreen");
8942 prepareDisplay(DISPLAY_ORIENTATION_0);
8943 prepareAxes(POSITION | ID | SLOT | PRESSURE);
8944 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8945
8946 NotifyMotionArgs motionArgs;
8947
8948 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
8949 // First finger down.
8950 processId(mapper, FIRST_TRACKING_ID);
8951 processPosition(mapper, x1, y1);
8952 processPressure(mapper, RAW_PRESSURE_MAX);
8953 processSync(mapper);
8954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8955 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8956 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8957
8958 // First finger move.
8959 processId(mapper, FIRST_TRACKING_ID);
8960 processPosition(mapper, x1 + 1, y1 + 1);
8961 processPressure(mapper, RAW_PRESSURE_MAX);
8962 processSync(mapper);
8963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8964 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8965 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8966
8967 // Second finger down.
8968 processSlot(mapper, SECOND_SLOT);
8969 processId(mapper, SECOND_TRACKING_ID);
8970 processPosition(mapper, x2, y2);
8971 processPressure(mapper, RAW_PRESSURE_MAX);
8972 processSync(mapper);
8973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008974 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00008975 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8976
8977 // second finger up with some unexpected data.
8978 processSlot(mapper, SECOND_SLOT);
8979 processId(mapper, INVALID_TRACKING_ID);
8980 processPosition(mapper, x2, y2);
8981 processSync(mapper);
8982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008983 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00008984 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8985
8986 // first finger up with some unexpected data.
8987 processSlot(mapper, FIRST_SLOT);
8988 processId(mapper, INVALID_TRACKING_ID);
8989 processPosition(mapper, x2, y2);
8990 processPressure(mapper, RAW_PRESSURE_MAX);
8991 processSync(mapper);
8992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8993 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8994 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8995}
8996
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008997// --- MultiTouchInputMapperTest_ExternalDevice ---
8998
8999class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9000protected:
Chris Yea52ade12020-08-27 16:49:20 -07009001 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009002};
9003
9004/**
9005 * Expect fallback to internal viewport if device is external and external viewport is not present.
9006 */
9007TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9008 prepareAxes(POSITION);
9009 addConfigurationProperty("touch.deviceType", "touchScreen");
9010 prepareDisplay(DISPLAY_ORIENTATION_0);
9011 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9012
9013 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9014
9015 NotifyMotionArgs motionArgs;
9016
9017 // Expect the event to be sent to the internal viewport,
9018 // because an external viewport is not present.
9019 processPosition(mapper, 100, 100);
9020 processSync(mapper);
9021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9022 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9023
9024 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009025 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009026 processPosition(mapper, 100, 100);
9027 processSync(mapper);
9028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9029 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9030}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009031
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009032TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9033 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9034 std::shared_ptr<FakePointerController> fakePointerController =
9035 std::make_shared<FakePointerController>();
9036 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9037 fakePointerController->setPosition(0, 0);
9038 fakePointerController->setButtonState(0);
9039
9040 // prepare device and capture
9041 prepareDisplay(DISPLAY_ORIENTATION_0);
9042 prepareAxes(POSITION | ID | SLOT);
9043 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9044 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9045 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009046 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009047 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9048
9049 // captured touchpad should be a touchpad source
9050 NotifyDeviceResetArgs resetArgs;
9051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9052 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9053
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009054 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009055
9056 const InputDeviceInfo::MotionRange* relRangeX =
9057 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9058 ASSERT_NE(relRangeX, nullptr);
9059 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9060 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9061 const InputDeviceInfo::MotionRange* relRangeY =
9062 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9063 ASSERT_NE(relRangeY, nullptr);
9064 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9065 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9066
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009067 // run captured pointer tests - note that this is unscaled, so input listener events should be
9068 // identical to what the hardware sends (accounting for any
9069 // calibration).
9070 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009071 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009072 processId(mapper, 1);
9073 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9074 processKey(mapper, BTN_TOUCH, 1);
9075 processSync(mapper);
9076
9077 // expect coord[0] to contain initial location of touch 0
9078 NotifyMotionArgs args;
9079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9080 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9081 ASSERT_EQ(1U, args.pointerCount);
9082 ASSERT_EQ(0, args.pointerProperties[0].id);
9083 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9084 ASSERT_NO_FATAL_FAILURE(
9085 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9086
9087 // FINGER 1 DOWN
9088 processSlot(mapper, 1);
9089 processId(mapper, 2);
9090 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9091 processSync(mapper);
9092
9093 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009095 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009096 ASSERT_EQ(2U, args.pointerCount);
9097 ASSERT_EQ(0, args.pointerProperties[0].id);
9098 ASSERT_EQ(1, args.pointerProperties[1].id);
9099 ASSERT_NO_FATAL_FAILURE(
9100 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9101 ASSERT_NO_FATAL_FAILURE(
9102 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9103
9104 // FINGER 1 MOVE
9105 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9106 processSync(mapper);
9107
9108 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9109 // from move
9110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9112 ASSERT_NO_FATAL_FAILURE(
9113 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9114 ASSERT_NO_FATAL_FAILURE(
9115 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9116
9117 // FINGER 0 MOVE
9118 processSlot(mapper, 0);
9119 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9120 processSync(mapper);
9121
9122 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9124 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9125 ASSERT_NO_FATAL_FAILURE(
9126 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9127 ASSERT_NO_FATAL_FAILURE(
9128 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9129
9130 // BUTTON DOWN
9131 processKey(mapper, BTN_LEFT, 1);
9132 processSync(mapper);
9133
9134 // touchinputmapper design sends a move before button press
9135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9136 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9138 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9139
9140 // BUTTON UP
9141 processKey(mapper, BTN_LEFT, 0);
9142 processSync(mapper);
9143
9144 // touchinputmapper design sends a move after button release
9145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9146 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9148 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9149
9150 // FINGER 0 UP
9151 processId(mapper, -1);
9152 processSync(mapper);
9153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9154 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9155
9156 // FINGER 1 MOVE
9157 processSlot(mapper, 1);
9158 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9159 processSync(mapper);
9160
9161 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9164 ASSERT_EQ(1U, args.pointerCount);
9165 ASSERT_EQ(1, args.pointerProperties[0].id);
9166 ASSERT_NO_FATAL_FAILURE(
9167 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9168
9169 // FINGER 1 UP
9170 processId(mapper, -1);
9171 processKey(mapper, BTN_TOUCH, 0);
9172 processSync(mapper);
9173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9174 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9175
9176 // non captured touchpad should be a mouse source
9177 mFakePolicy->setPointerCapture(false);
9178 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9180 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9181}
9182
9183TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9184 std::shared_ptr<FakePointerController> fakePointerController =
9185 std::make_shared<FakePointerController>();
9186 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9187 fakePointerController->setPosition(0, 0);
9188 fakePointerController->setButtonState(0);
9189
9190 // prepare device and capture
9191 prepareDisplay(DISPLAY_ORIENTATION_0);
9192 prepareAxes(POSITION | ID | SLOT);
9193 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9194 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009195 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009196 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9197 // run uncaptured pointer tests - pushes out generic events
9198 // FINGER 0 DOWN
9199 processId(mapper, 3);
9200 processPosition(mapper, 100, 100);
9201 processKey(mapper, BTN_TOUCH, 1);
9202 processSync(mapper);
9203
9204 // start at (100,100), cursor should be at (0,0) * scale
9205 NotifyMotionArgs args;
9206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9207 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9208 ASSERT_NO_FATAL_FAILURE(
9209 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9210
9211 // FINGER 0 MOVE
9212 processPosition(mapper, 200, 200);
9213 processSync(mapper);
9214
9215 // compute scaling to help with touch position checking
9216 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9217 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9218 float scale =
9219 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9220
9221 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9223 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9225 0, 0, 0, 0, 0, 0, 0));
9226}
9227
9228TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9229 std::shared_ptr<FakePointerController> fakePointerController =
9230 std::make_shared<FakePointerController>();
9231
9232 prepareDisplay(DISPLAY_ORIENTATION_0);
9233 prepareAxes(POSITION | ID | SLOT);
9234 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009235 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009236 mFakePolicy->setPointerCapture(false);
9237 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9238
9239 // uncaptured touchpad should be a pointer device
9240 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9241
9242 // captured touchpad should be a touchpad device
9243 mFakePolicy->setPointerCapture(true);
9244 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9245 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9246}
9247
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009248// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009249
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009250class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009251protected:
9252 static const char* DEVICE_NAME;
9253 static const char* DEVICE_LOCATION;
9254 static const int32_t DEVICE_ID;
9255 static const int32_t DEVICE_GENERATION;
9256 static const int32_t DEVICE_CONTROLLER_NUMBER;
9257 static const Flags<InputDeviceClass> DEVICE_CLASSES;
9258 static const int32_t EVENTHUB_ID;
9259
9260 std::shared_ptr<FakeEventHub> mFakeEventHub;
9261 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009262 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009263 std::unique_ptr<InstrumentedInputReader> mReader;
9264 std::shared_ptr<InputDevice> mDevice;
9265
9266 virtual void SetUp(Flags<InputDeviceClass> classes) {
9267 mFakeEventHub = std::make_unique<FakeEventHub>();
9268 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009269 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009270 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009271 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009272 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9273 }
9274
9275 void SetUp() override { SetUp(DEVICE_CLASSES); }
9276
9277 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009278 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009279 mFakePolicy.clear();
9280 }
9281
9282 void configureDevice(uint32_t changes) {
9283 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9284 mReader->requestRefreshConfiguration(changes);
9285 mReader->loopOnce();
9286 }
9287 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9288 }
9289
9290 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9291 const std::string& location, int32_t eventHubId,
9292 Flags<InputDeviceClass> classes) {
9293 InputDeviceIdentifier identifier;
9294 identifier.name = name;
9295 identifier.location = location;
9296 std::shared_ptr<InputDevice> device =
9297 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9298 identifier);
9299 mReader->pushNextDevice(device);
9300 mFakeEventHub->addDevice(eventHubId, name, classes);
9301 mReader->loopOnce();
9302 return device;
9303 }
9304
9305 template <class T, typename... Args>
9306 T& addControllerAndConfigure(Args... args) {
9307 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9308
9309 return controller;
9310 }
9311};
9312
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009313const char* PeripheralControllerTest::DEVICE_NAME = "device";
9314const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9315const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9316const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9317const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
9318const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08009319 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009320const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009321
9322// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009323class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009324protected:
9325 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009326 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009327 }
9328};
9329
9330TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009331 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009332
9333 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9334 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9335}
9336
9337TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009338 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009339
9340 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9341 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9342}
9343
9344// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009345class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009346protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009347 void SetUp() override {
9348 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9349 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009350};
9351
Chris Ye85758332021-05-16 23:05:17 -07009352TEST_F(LightControllerTest, MonoLight) {
9353 RawLightInfo infoMono = {.id = 1,
9354 .name = "Mono",
9355 .maxBrightness = 255,
9356 .flags = InputLightClass::BRIGHTNESS,
9357 .path = ""};
9358 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009359
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009360 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009361 InputDeviceInfo info;
9362 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009363 std::vector<InputDeviceLightInfo> lights = info.getLights();
9364 ASSERT_EQ(1U, lights.size());
9365 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009366
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009367 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9368 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009369}
9370
9371TEST_F(LightControllerTest, RGBLight) {
9372 RawLightInfo infoRed = {.id = 1,
9373 .name = "red",
9374 .maxBrightness = 255,
9375 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9376 .path = ""};
9377 RawLightInfo infoGreen = {.id = 2,
9378 .name = "green",
9379 .maxBrightness = 255,
9380 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9381 .path = ""};
9382 RawLightInfo infoBlue = {.id = 3,
9383 .name = "blue",
9384 .maxBrightness = 255,
9385 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9386 .path = ""};
9387 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9388 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9389 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9390
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009391 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009392 InputDeviceInfo info;
9393 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009394 std::vector<InputDeviceLightInfo> lights = info.getLights();
9395 ASSERT_EQ(1U, lights.size());
9396 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009397
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009398 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9399 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009400}
9401
9402TEST_F(LightControllerTest, MultiColorRGBLight) {
9403 RawLightInfo infoColor = {.id = 1,
9404 .name = "red",
9405 .maxBrightness = 255,
9406 .flags = InputLightClass::BRIGHTNESS |
9407 InputLightClass::MULTI_INTENSITY |
9408 InputLightClass::MULTI_INDEX,
9409 .path = ""};
9410
9411 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9412
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009413 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009414 InputDeviceInfo info;
9415 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009416 std::vector<InputDeviceLightInfo> lights = info.getLights();
9417 ASSERT_EQ(1U, lights.size());
9418 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009419
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009420 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9421 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009422}
9423
9424TEST_F(LightControllerTest, PlayerIdLight) {
9425 RawLightInfo info1 = {.id = 1,
9426 .name = "player1",
9427 .maxBrightness = 255,
9428 .flags = InputLightClass::BRIGHTNESS,
9429 .path = ""};
9430 RawLightInfo info2 = {.id = 2,
9431 .name = "player2",
9432 .maxBrightness = 255,
9433 .flags = InputLightClass::BRIGHTNESS,
9434 .path = ""};
9435 RawLightInfo info3 = {.id = 3,
9436 .name = "player3",
9437 .maxBrightness = 255,
9438 .flags = InputLightClass::BRIGHTNESS,
9439 .path = ""};
9440 RawLightInfo info4 = {.id = 4,
9441 .name = "player4",
9442 .maxBrightness = 255,
9443 .flags = InputLightClass::BRIGHTNESS,
9444 .path = ""};
9445 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9446 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9447 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9448 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9449
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009450 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009451 InputDeviceInfo info;
9452 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009453 std::vector<InputDeviceLightInfo> lights = info.getLights();
9454 ASSERT_EQ(1U, lights.size());
9455 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009456
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009457 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9458 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9459 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009460}
9461
Michael Wrightd02c5b62014-02-10 15:10:22 -08009462} // namespace android