blob: 068e03c4fdbbb266a5add23a27c5bd90114caf8e [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Prabir Pradhan2770d242019-09-02 18:07:11 -070017#include <CursorInputMapper.h>
18#include <InputDevice.h>
19#include <InputMapper.h>
20#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080021#include <InputReaderBase.h>
22#include <InputReaderFactory.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070023#include <KeyboardInputMapper.h>
24#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070025#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070026#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <SingleTouchInputMapper.h>
28#include <SwitchInputMapper.h>
29#include <TestInputListener.h>
30#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080031#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000032#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070033#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080034#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050035#include <gui/constants.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080036#include <inttypes.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080037#include <math.h>
38
Michael Wright17db18e2020-06-26 20:51:44 +010039#include <memory>
Chris Ye3fdbfef2021-01-06 18:45:18 -080040#include <regex>
Michael Wrightdde67b82020-10-27 16:09:22 +000041#include "input/DisplayViewport.h"
42#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010043
Michael Wrightd02c5b62014-02-10 15:10:22 -080044namespace android {
45
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070046using std::chrono_literals::operator""ms;
Chris Ye1b0c7342020-07-28 21:57:03 -070047using namespace android::flag_operators;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070048
49// Timeout for waiting for an expected event
50static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
51
Michael Wrightd02c5b62014-02-10 15:10:22 -080052// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000053static constexpr nsecs_t ARBITRARY_TIME = 1234;
54static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080055
56// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080057static constexpr int32_t DISPLAY_ID = 0;
58static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
59static constexpr int32_t DISPLAY_WIDTH = 480;
60static constexpr int32_t DISPLAY_HEIGHT = 800;
61static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
62static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
63static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070064static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070065static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080066
arthurhungcc7f9802020-04-30 17:55:40 +080067static constexpr int32_t FIRST_SLOT = 0;
68static constexpr int32_t SECOND_SLOT = 1;
69static constexpr int32_t THIRD_SLOT = 2;
70static constexpr int32_t INVALID_TRACKING_ID = -1;
71static constexpr int32_t FIRST_TRACKING_ID = 0;
72static constexpr int32_t SECOND_TRACKING_ID = 1;
73static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080074static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080075static constexpr int32_t BATTERY_STATUS = 4;
76static constexpr int32_t BATTERY_CAPACITY = 66;
Chris Ye3fdbfef2021-01-06 18:45:18 -080077static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
78static constexpr int32_t LIGHT_COLOR = 0x7F448866;
79static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080080
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080081static constexpr int32_t ACTION_POINTER_0_DOWN =
82 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
83static constexpr int32_t ACTION_POINTER_0_UP =
84 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
85static constexpr int32_t ACTION_POINTER_1_DOWN =
86 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
87static constexpr int32_t ACTION_POINTER_1_UP =
88 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
89
Michael Wrightd02c5b62014-02-10 15:10:22 -080090// Error tolerance for floating point assertions.
91static const float EPSILON = 0.001f;
92
93template<typename T>
94static inline T min(T a, T b) {
95 return a < b ? a : b;
96}
97
98static inline float avg(float x, float y) {
99 return (x + y) / 2;
100}
101
Chris Ye3fdbfef2021-01-06 18:45:18 -0800102// Mapping for light color name and the light color
103const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
104 {"green", LightColor::GREEN},
105 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800106
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700107static int32_t getInverseRotation(int32_t orientation) {
108 switch (orientation) {
109 case DISPLAY_ORIENTATION_90:
110 return DISPLAY_ORIENTATION_270;
111 case DISPLAY_ORIENTATION_270:
112 return DISPLAY_ORIENTATION_90;
113 default:
114 return orientation;
115 }
116}
117
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800118static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
119 InputDeviceInfo info;
120 mapper.populateDeviceInfo(&info);
121
122 const InputDeviceInfo::MotionRange* motionRange =
123 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
124 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
125}
126
127static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
128 InputDeviceInfo info;
129 mapper.populateDeviceInfo(&info);
130
131 const InputDeviceInfo::MotionRange* motionRange =
132 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
133 ASSERT_EQ(nullptr, motionRange);
134}
135
Michael Wrightd02c5b62014-02-10 15:10:22 -0800136// --- FakePointerController ---
137
138class FakePointerController : public PointerControllerInterface {
139 bool mHaveBounds;
140 float mMinX, mMinY, mMaxX, mMaxY;
141 float mX, mY;
142 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800143 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800144
Michael Wrightd02c5b62014-02-10 15:10:22 -0800145public:
146 FakePointerController() :
147 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800148 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800149 }
150
Michael Wright17db18e2020-06-26 20:51:44 +0100151 virtual ~FakePointerController() {}
152
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153 void setBounds(float minX, float minY, float maxX, float maxY) {
154 mHaveBounds = true;
155 mMinX = minX;
156 mMinY = minY;
157 mMaxX = maxX;
158 mMaxY = maxY;
159 }
160
Chris Yea52ade12020-08-27 16:49:20 -0700161 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800162 mX = x;
163 mY = y;
164 }
165
Chris Yea52ade12020-08-27 16:49:20 -0700166 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800167
Chris Yea52ade12020-08-27 16:49:20 -0700168 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169
Chris Yea52ade12020-08-27 16:49:20 -0700170 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171 *outX = mX;
172 *outY = mY;
173 }
174
Chris Yea52ade12020-08-27 16:49:20 -0700175 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800176
Chris Yea52ade12020-08-27 16:49:20 -0700177 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800178 mDisplayId = viewport.displayId;
179 }
180
Arthur Hung7c645402019-01-25 17:45:42 +0800181 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
182 return mSpotsByDisplay;
183 }
184
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185private:
Chris Yea52ade12020-08-27 16:49:20 -0700186 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800187 *outMinX = mMinX;
188 *outMinY = mMinY;
189 *outMaxX = mMaxX;
190 *outMaxY = mMaxY;
191 return mHaveBounds;
192 }
193
Chris Yea52ade12020-08-27 16:49:20 -0700194 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800195 mX += deltaX;
196 if (mX < mMinX) mX = mMinX;
197 if (mX > mMaxX) mX = mMaxX;
198 mY += deltaY;
199 if (mY < mMinY) mY = mMinY;
200 if (mY > mMaxY) mY = mMaxY;
201 }
202
Chris Yea52ade12020-08-27 16:49:20 -0700203 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204
Chris Yea52ade12020-08-27 16:49:20 -0700205 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206
Chris Yea52ade12020-08-27 16:49:20 -0700207 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800208
Chris Yea52ade12020-08-27 16:49:20 -0700209 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
210 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800211 std::vector<int32_t> newSpots;
212 // Add spots for fingers that are down.
213 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
214 uint32_t id = idBits.clearFirstMarkedBit();
215 newSpots.push_back(id);
216 }
217
218 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800219 }
220
Chris Yea52ade12020-08-27 16:49:20 -0700221 void clearSpots() override {}
Arthur Hung7c645402019-01-25 17:45:42 +0800222
223 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800224};
225
226
227// --- FakeInputReaderPolicy ---
228
229class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700230 std::mutex mLock;
231 std::condition_variable mDevicesChangedCondition;
232
Michael Wrightd02c5b62014-02-10 15:10:22 -0800233 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000234 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700235 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
236 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100237 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700238 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800239
240protected:
Chris Yea52ade12020-08-27 16:49:20 -0700241 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800242
243public:
244 FakeInputReaderPolicy() {
245 }
246
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700247 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800248 waitForInputDevices([](bool devicesChanged) {
249 if (!devicesChanged) {
250 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
251 }
252 });
253 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700254
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800255 void assertInputDevicesNotChanged() {
256 waitForInputDevices([](bool devicesChanged) {
257 if (devicesChanged) {
258 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
259 }
260 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700261 }
262
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700263 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100264 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100265 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700266 }
267
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700268 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
269 return mConfig.getDisplayViewportByUniqueId(uniqueId);
270 }
271 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
272 return mConfig.getDisplayViewportByType(type);
273 }
274
275 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
276 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700277 }
278
Prabir Pradhan5632d622021-09-06 07:57:20 -0700279 void addDisplayViewport(DisplayViewport viewport) {
280 mViewports.push_back(std::move(viewport));
281 mConfig.setDisplayViewports(mViewports);
282 }
283
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700284 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000285 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700286 std::optional<uint8_t> physicalPort, ViewportType type) {
287 const bool isRotated =
288 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
289 DisplayViewport v;
290 v.displayId = displayId;
291 v.orientation = orientation;
292 v.logicalLeft = 0;
293 v.logicalTop = 0;
294 v.logicalRight = isRotated ? height : width;
295 v.logicalBottom = isRotated ? width : height;
296 v.physicalLeft = 0;
297 v.physicalTop = 0;
298 v.physicalRight = isRotated ? height : width;
299 v.physicalBottom = isRotated ? width : height;
300 v.deviceWidth = isRotated ? height : width;
301 v.deviceHeight = isRotated ? width : height;
302 v.isActive = isActive;
303 v.uniqueId = uniqueId;
304 v.physicalPort = physicalPort;
305 v.type = type;
306
307 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800308 }
309
Arthur Hung6cd19a42019-08-30 19:04:12 +0800310 bool updateViewport(const DisplayViewport& viewport) {
311 size_t count = mViewports.size();
312 for (size_t i = 0; i < count; i++) {
313 const DisplayViewport& currentViewport = mViewports[i];
314 if (currentViewport.displayId == viewport.displayId) {
315 mViewports[i] = viewport;
316 mConfig.setDisplayViewports(mViewports);
317 return true;
318 }
319 }
320 // no viewport found.
321 return false;
322 }
323
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100324 void addExcludedDeviceName(const std::string& deviceName) {
325 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800326 }
327
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700328 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
329 mConfig.portAssociations.insert({inputPort, displayPort});
330 }
331
Christine Franks1ba71cc2021-04-07 14:37:42 -0700332 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
333 const std::string& displayUniqueId) {
334 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
335 }
336
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000337 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700338
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000339 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700340
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000341 void setPointerController(std::shared_ptr<FakePointerController> controller) {
342 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800343 }
344
345 const InputReaderConfiguration* getReaderConfiguration() const {
346 return &mConfig;
347 }
348
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800349 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800350 return mInputDevices;
351 }
352
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100353 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700354 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700355 return transform;
356 }
357
358 void setTouchAffineTransformation(const TouchAffineTransformation t) {
359 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800360 }
361
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000362 PointerCaptureRequest setPointerCapture(bool enabled) {
363 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
364 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800365 }
366
Arthur Hung7c645402019-01-25 17:45:42 +0800367 void setShowTouches(bool enabled) {
368 mConfig.showTouches = enabled;
369 }
370
Garfield Tan888a6a42020-01-09 11:39:16 -0800371 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
372 mConfig.defaultPointerDisplayId = pointerDisplayId;
373 }
374
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800375 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
376
Michael Wrightd02c5b62014-02-10 15:10:22 -0800377private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000378 uint32_t mNextPointerCaptureSequenceNumber = 0;
379
Chris Yea52ade12020-08-27 16:49:20 -0700380 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800381 *outConfig = mConfig;
382 }
383
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000384 std::shared_ptr<PointerControllerInterface> obtainPointerController(
385 int32_t /*deviceId*/) override {
386 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387 }
388
Chris Yea52ade12020-08-27 16:49:20 -0700389 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700390 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800391 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700392 mInputDevicesChanged = true;
393 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394 }
395
Chris Yea52ade12020-08-27 16:49:20 -0700396 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
397 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700398 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800399 }
400
Chris Yea52ade12020-08-27 16:49:20 -0700401 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800402
403 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
404 std::unique_lock<std::mutex> lock(mLock);
405 base::ScopedLockAssertion assumeLocked(mLock);
406
407 const bool devicesChanged =
408 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
409 return mInputDevicesChanged;
410 });
411 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
412 mInputDevicesChanged = false;
413 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800414};
415
Michael Wrightd02c5b62014-02-10 15:10:22 -0800416// --- FakeEventHub ---
417
418class FakeEventHub : public EventHubInterface {
419 struct KeyInfo {
420 int32_t keyCode;
421 uint32_t flags;
422 };
423
Chris Yef59a2f42020-10-16 12:55:26 -0700424 struct SensorInfo {
425 InputDeviceSensorType sensorType;
426 int32_t sensorDataIndex;
427 };
428
Michael Wrightd02c5b62014-02-10 15:10:22 -0800429 struct Device {
430 InputDeviceIdentifier identifier;
Chris Ye1b0c7342020-07-28 21:57:03 -0700431 Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800432 PropertyMap configuration;
433 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
434 KeyedVector<int, bool> relativeAxes;
435 KeyedVector<int32_t, int32_t> keyCodeStates;
436 KeyedVector<int32_t, int32_t> scanCodeStates;
437 KeyedVector<int32_t, int32_t> switchStates;
438 KeyedVector<int32_t, int32_t> absoluteAxisValue;
439 KeyedVector<int32_t, KeyInfo> keysByScanCode;
440 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
441 KeyedVector<int32_t, bool> leds;
Chris Yef59a2f42020-10-16 12:55:26 -0700442 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
443 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800444 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700445 bool enabled;
446
447 status_t enable() {
448 enabled = true;
449 return OK;
450 }
451
452 status_t disable() {
453 enabled = false;
454 return OK;
455 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800456
Chris Ye1b0c7342020-07-28 21:57:03 -0700457 explicit Device(Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800458 };
459
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700460 std::mutex mLock;
461 std::condition_variable mEventsCondition;
462
Michael Wrightd02c5b62014-02-10 15:10:22 -0800463 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100464 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000465 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600466 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000467 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800468 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
469 // Simulates a device light brightness, from light id to light brightness.
470 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
471 // Simulates a device light intensities, from light id to light intensities map.
472 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
473 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800474
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700475public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800476 virtual ~FakeEventHub() {
477 for (size_t i = 0; i < mDevices.size(); i++) {
478 delete mDevices.valueAt(i);
479 }
480 }
481
Michael Wrightd02c5b62014-02-10 15:10:22 -0800482 FakeEventHub() { }
483
Chris Ye1b0c7342020-07-28 21:57:03 -0700484 void addDevice(int32_t deviceId, const std::string& name, Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800485 Device* device = new Device(classes);
486 device->identifier.name = name;
487 mDevices.add(deviceId, device);
488
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000489 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490 }
491
492 void removeDevice(int32_t deviceId) {
493 delete mDevices.valueFor(deviceId);
494 mDevices.removeItem(deviceId);
495
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000496 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800497 }
498
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700499 bool isDeviceEnabled(int32_t deviceId) {
500 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700501 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700502 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
503 return false;
504 }
505 return device->enabled;
506 }
507
508 status_t enableDevice(int32_t deviceId) {
509 status_t result;
510 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700511 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700512 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
513 return BAD_VALUE;
514 }
515 if (device->enabled) {
516 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
517 return OK;
518 }
519 result = device->enable();
520 return result;
521 }
522
523 status_t disableDevice(int32_t deviceId) {
524 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700525 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700526 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
527 return BAD_VALUE;
528 }
529 if (!device->enabled) {
530 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
531 return OK;
532 }
533 return device->disable();
534 }
535
Michael Wrightd02c5b62014-02-10 15:10:22 -0800536 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000537 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800538 }
539
540 void addConfigurationProperty(int32_t deviceId, const String8& key, const String8& value) {
541 Device* device = getDevice(deviceId);
542 device->configuration.addProperty(key, value);
543 }
544
545 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
546 Device* device = getDevice(deviceId);
547 device->configuration.addAll(configuration);
548 }
549
550 void addAbsoluteAxis(int32_t deviceId, int axis,
551 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
552 Device* device = getDevice(deviceId);
553
554 RawAbsoluteAxisInfo info;
555 info.valid = true;
556 info.minValue = minValue;
557 info.maxValue = maxValue;
558 info.flat = flat;
559 info.fuzz = fuzz;
560 info.resolution = resolution;
561 device->absoluteAxes.add(axis, info);
562 }
563
564 void addRelativeAxis(int32_t deviceId, int32_t axis) {
565 Device* device = getDevice(deviceId);
566 device->relativeAxes.add(axis, true);
567 }
568
569 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
570 Device* device = getDevice(deviceId);
571 device->keyCodeStates.replaceValueFor(keyCode, state);
572 }
573
574 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
575 Device* device = getDevice(deviceId);
576 device->scanCodeStates.replaceValueFor(scanCode, state);
577 }
578
579 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
580 Device* device = getDevice(deviceId);
581 device->switchStates.replaceValueFor(switchCode, state);
582 }
583
584 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
585 Device* device = getDevice(deviceId);
586 device->absoluteAxisValue.replaceValueFor(axis, value);
587 }
588
589 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
590 int32_t keyCode, uint32_t flags) {
591 Device* device = getDevice(deviceId);
592 KeyInfo info;
593 info.keyCode = keyCode;
594 info.flags = flags;
595 if (scanCode) {
596 device->keysByScanCode.add(scanCode, info);
597 }
598 if (usageCode) {
599 device->keysByUsageCode.add(usageCode, info);
600 }
601 }
602
603 void addLed(int32_t deviceId, int32_t led, bool initialState) {
604 Device* device = getDevice(deviceId);
605 device->leds.add(led, initialState);
606 }
607
Chris Yef59a2f42020-10-16 12:55:26 -0700608 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
609 int32_t sensorDataIndex) {
610 Device* device = getDevice(deviceId);
611 SensorInfo info;
612 info.sensorType = sensorType;
613 info.sensorDataIndex = sensorDataIndex;
614 device->sensorsByAbsCode.emplace(absCode, info);
615 }
616
617 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
618 Device* device = getDevice(deviceId);
619 typename BitArray<MSC_MAX>::Buffer buffer;
620 buffer[mscEvent / 32] = 1 << mscEvent % 32;
621 device->mscBitmask.loadFromBuffer(buffer);
622 }
623
Chris Ye3fdbfef2021-01-06 18:45:18 -0800624 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
625 mRawLightInfos.emplace(rawId, std::move(info));
626 }
627
628 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
629 mLightBrightness.emplace(rawId, brightness);
630 }
631
632 void fakeLightIntensities(int32_t rawId,
633 const std::unordered_map<LightColor, int32_t> intensities) {
634 mLightIntensities.emplace(rawId, std::move(intensities));
635 }
636
Michael Wrightd02c5b62014-02-10 15:10:22 -0800637 bool getLedState(int32_t deviceId, int32_t led) {
638 Device* device = getDevice(deviceId);
639 return device->leds.valueFor(led);
640 }
641
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100642 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800643 return mExcludedDevices;
644 }
645
646 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
647 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800648 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800649 }
650
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000651 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
652 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700653 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800654 RawEvent event;
655 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000656 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800657 event.deviceId = deviceId;
658 event.type = type;
659 event.code = code;
660 event.value = value;
661 mEvents.push_back(event);
662
663 if (type == EV_ABS) {
664 setAbsoluteAxisValue(deviceId, code, value);
665 }
666 }
667
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600668 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
669 std::vector<TouchVideoFrame>> videoFrames) {
670 mVideoFrames = std::move(videoFrames);
671 }
672
Michael Wrightd02c5b62014-02-10 15:10:22 -0800673 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700674 std::unique_lock<std::mutex> lock(mLock);
675 base::ScopedLockAssertion assumeLocked(mLock);
676 const bool queueIsEmpty =
677 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
678 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
679 if (!queueIsEmpty) {
680 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
681 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800682 }
683
684private:
685 Device* getDevice(int32_t deviceId) const {
686 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100687 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800688 }
689
Chris Yea52ade12020-08-27 16:49:20 -0700690 Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800691 Device* device = getDevice(deviceId);
Chris Ye1b0c7342020-07-28 21:57:03 -0700692 return device ? device->classes : Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800693 }
694
Chris Yea52ade12020-08-27 16:49:20 -0700695 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800696 Device* device = getDevice(deviceId);
697 return device ? device->identifier : InputDeviceIdentifier();
698 }
699
Chris Yea52ade12020-08-27 16:49:20 -0700700 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800701
Chris Yea52ade12020-08-27 16:49:20 -0700702 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703 Device* device = getDevice(deviceId);
704 if (device) {
705 *outConfiguration = device->configuration;
706 }
707 }
708
Chris Yea52ade12020-08-27 16:49:20 -0700709 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
710 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800711 Device* device = getDevice(deviceId);
Arthur Hung9da14732019-09-02 16:16:58 +0800712 if (device && device->enabled) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800713 ssize_t index = device->absoluteAxes.indexOfKey(axis);
714 if (index >= 0) {
715 *outAxisInfo = device->absoluteAxes.valueAt(index);
716 return OK;
717 }
718 }
719 outAxisInfo->clear();
720 return -1;
721 }
722
Chris Yea52ade12020-08-27 16:49:20 -0700723 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800724 Device* device = getDevice(deviceId);
725 if (device) {
726 return device->relativeAxes.indexOfKey(axis) >= 0;
727 }
728 return false;
729 }
730
Chris Yea52ade12020-08-27 16:49:20 -0700731 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800732
Chris Yef59a2f42020-10-16 12:55:26 -0700733 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
734 Device* device = getDevice(deviceId);
735 if (device) {
736 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
737 }
738 return false;
739 }
740
Chris Yea52ade12020-08-27 16:49:20 -0700741 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
742 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800743 Device* device = getDevice(deviceId);
744 if (device) {
745 const KeyInfo* key = getKey(device, scanCode, usageCode);
746 if (key) {
747 if (outKeycode) {
748 *outKeycode = key->keyCode;
749 }
750 if (outFlags) {
751 *outFlags = key->flags;
752 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700753 if (outMetaState) {
754 *outMetaState = metaState;
755 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800756 return OK;
757 }
758 }
759 return NAME_NOT_FOUND;
760 }
761
762 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
763 if (usageCode) {
764 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
765 if (index >= 0) {
766 return &device->keysByUsageCode.valueAt(index);
767 }
768 }
769 if (scanCode) {
770 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
771 if (index >= 0) {
772 return &device->keysByScanCode.valueAt(index);
773 }
774 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700775 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800776 }
777
Chris Yea52ade12020-08-27 16:49:20 -0700778 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800779
Chris Yef59a2f42020-10-16 12:55:26 -0700780 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(int32_t deviceId,
781 int32_t absCode) {
782 Device* device = getDevice(deviceId);
783 if (!device) {
784 return Errorf("Sensor device not found.");
785 }
786 auto it = device->sensorsByAbsCode.find(absCode);
787 if (it == device->sensorsByAbsCode.end()) {
788 return Errorf("Sensor map not found.");
789 }
790 const SensorInfo& info = it->second;
791 return std::make_pair(info.sensorType, info.sensorDataIndex);
792 }
793
Chris Yea52ade12020-08-27 16:49:20 -0700794 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800795 mExcludedDevices = devices;
796 }
797
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000798 size_t getEvents(int, RawEvent* buffer, size_t bufferSize) override {
799 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800800
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000801 const size_t filledSize = std::min(mEvents.size(), bufferSize);
802 std::copy(mEvents.begin(), mEvents.begin() + filledSize, buffer);
803
804 mEvents.erase(mEvents.begin(), mEvents.begin() + filledSize);
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700805 mEventsCondition.notify_all();
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000806 return filledSize;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800807 }
808
Chris Yea52ade12020-08-27 16:49:20 -0700809 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600810 auto it = mVideoFrames.find(deviceId);
811 if (it != mVideoFrames.end()) {
812 std::vector<TouchVideoFrame> frames = std::move(it->second);
813 mVideoFrames.erase(deviceId);
814 return frames;
815 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800816 return {};
817 }
818
Chris Yea52ade12020-08-27 16:49:20 -0700819 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820 Device* device = getDevice(deviceId);
821 if (device) {
822 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
823 if (index >= 0) {
824 return device->scanCodeStates.valueAt(index);
825 }
826 }
827 return AKEY_STATE_UNKNOWN;
828 }
829
Chris Yea52ade12020-08-27 16:49:20 -0700830 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800831 Device* device = getDevice(deviceId);
832 if (device) {
833 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
834 if (index >= 0) {
835 return device->keyCodeStates.valueAt(index);
836 }
837 }
838 return AKEY_STATE_UNKNOWN;
839 }
840
Chris Yea52ade12020-08-27 16:49:20 -0700841 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800842 Device* device = getDevice(deviceId);
843 if (device) {
844 ssize_t index = device->switchStates.indexOfKey(sw);
845 if (index >= 0) {
846 return device->switchStates.valueAt(index);
847 }
848 }
849 return AKEY_STATE_UNKNOWN;
850 }
851
Chris Yea52ade12020-08-27 16:49:20 -0700852 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
853 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800854 Device* device = getDevice(deviceId);
855 if (device) {
856 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
857 if (index >= 0) {
858 *outValue = device->absoluteAxisValue.valueAt(index);
859 return OK;
860 }
861 }
862 *outValue = 0;
863 return -1;
864 }
865
Chris Yea52ade12020-08-27 16:49:20 -0700866 // Return true if the device has non-empty key layout.
867 bool markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
868 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869 bool result = false;
870 Device* device = getDevice(deviceId);
871 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700872 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800873 for (size_t i = 0; i < numCodes; i++) {
874 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
875 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
876 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800877 }
878 }
879 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
880 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
881 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800882 }
883 }
884 }
885 }
886 return result;
887 }
888
Chris Yea52ade12020-08-27 16:49:20 -0700889 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800890 Device* device = getDevice(deviceId);
891 if (device) {
892 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
893 return index >= 0;
894 }
895 return false;
896 }
897
Arthur Hungcb40a002021-08-03 14:31:01 +0000898 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
899 Device* device = getDevice(deviceId);
900 if (!device) {
901 return false;
902 }
903 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
904 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
905 return true;
906 }
907 }
908 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
909 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
910 return true;
911 }
912 }
913 return false;
914 }
915
Chris Yea52ade12020-08-27 16:49:20 -0700916 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800917 Device* device = getDevice(deviceId);
918 return device && device->leds.indexOfKey(led) >= 0;
919 }
920
Chris Yea52ade12020-08-27 16:49:20 -0700921 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800922 Device* device = getDevice(deviceId);
923 if (device) {
924 ssize_t index = device->leds.indexOfKey(led);
925 if (index >= 0) {
926 device->leds.replaceValueAt(led, on);
927 } else {
928 ADD_FAILURE()
929 << "Attempted to set the state of an LED that the EventHub declared "
930 "was not present. led=" << led;
931 }
932 }
933 }
934
Chris Yea52ade12020-08-27 16:49:20 -0700935 void getVirtualKeyDefinitions(
936 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800937 outVirtualKeys.clear();
938
939 Device* device = getDevice(deviceId);
940 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800941 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800942 }
943 }
944
Chris Yea52ade12020-08-27 16:49:20 -0700945 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700946 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800947 }
948
Chris Yea52ade12020-08-27 16:49:20 -0700949 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800950 return false;
951 }
952
Chris Yea52ade12020-08-27 16:49:20 -0700953 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800954
Chris Yea52ade12020-08-27 16:49:20 -0700955 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800956
Chris Ye87143712020-11-10 05:05:58 +0000957 std::vector<int32_t> getVibratorIds(int32_t deviceId) override { return mVibrators; };
958
Chris Yee2b1e5c2021-03-10 22:45:12 -0800959 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
960 return BATTERY_CAPACITY;
961 }
Kim Low03ea0352020-11-06 12:45:07 -0800962
Chris Yee2b1e5c2021-03-10 22:45:12 -0800963 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
964 return BATTERY_STATUS;
965 }
966
967 const std::vector<int32_t> getRawBatteryIds(int32_t deviceId) { return {}; }
968
969 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId, int32_t batteryId) {
970 return std::nullopt;
971 }
Kim Low03ea0352020-11-06 12:45:07 -0800972
Chris Ye3fdbfef2021-01-06 18:45:18 -0800973 const std::vector<int32_t> getRawLightIds(int32_t deviceId) override {
974 std::vector<int32_t> ids;
975 for (const auto& [rawId, info] : mRawLightInfos) {
976 ids.push_back(rawId);
977 }
978 return ids;
979 }
980
981 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) override {
982 auto it = mRawLightInfos.find(lightId);
983 if (it == mRawLightInfos.end()) {
984 return std::nullopt;
985 }
986 return it->second;
987 }
988
989 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
990 mLightBrightness.emplace(lightId, brightness);
991 }
992
993 void setLightIntensities(int32_t deviceId, int32_t lightId,
994 std::unordered_map<LightColor, int32_t> intensities) override {
995 mLightIntensities.emplace(lightId, intensities);
996 };
997
998 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) override {
999 auto lightIt = mLightBrightness.find(lightId);
1000 if (lightIt == mLightBrightness.end()) {
1001 return std::nullopt;
1002 }
1003 return lightIt->second;
1004 }
1005
1006 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
1007 int32_t deviceId, int32_t lightId) override {
1008 auto lightIt = mLightIntensities.find(lightId);
1009 if (lightIt == mLightIntensities.end()) {
1010 return std::nullopt;
1011 }
1012 return lightIt->second;
1013 };
1014
Narayan Kamath39efe3e2014-10-17 10:37:08 +01001015 virtual bool isExternal(int32_t) const {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001016 return false;
1017 }
1018
Chris Yea52ade12020-08-27 16:49:20 -07001019 void dump(std::string&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001020
Chris Yea52ade12020-08-27 16:49:20 -07001021 void monitor() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001022
Chris Yea52ade12020-08-27 16:49:20 -07001023 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001024
Chris Yea52ade12020-08-27 16:49:20 -07001025 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001026};
1027
Michael Wrightd02c5b62014-02-10 15:10:22 -08001028// --- FakeInputMapper ---
1029
1030class FakeInputMapper : public InputMapper {
1031 uint32_t mSources;
1032 int32_t mKeyboardType;
1033 int32_t mMetaState;
1034 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1035 KeyedVector<int32_t, int32_t> mScanCodeStates;
1036 KeyedVector<int32_t, int32_t> mSwitchStates;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001037 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001038
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001039 std::mutex mLock;
1040 std::condition_variable mStateChangedCondition;
1041 bool mConfigureWasCalled GUARDED_BY(mLock);
1042 bool mResetWasCalled GUARDED_BY(mLock);
1043 bool mProcessWasCalled GUARDED_BY(mLock);
1044 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001045
Arthur Hungc23540e2018-11-29 20:42:11 +08001046 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001047public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001048 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1049 : InputMapper(deviceContext),
1050 mSources(sources),
1051 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001052 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001053 mConfigureWasCalled(false),
1054 mResetWasCalled(false),
1055 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001056
Chris Yea52ade12020-08-27 16:49:20 -07001057 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001058
1059 void setKeyboardType(int32_t keyboardType) {
1060 mKeyboardType = keyboardType;
1061 }
1062
1063 void setMetaState(int32_t metaState) {
1064 mMetaState = metaState;
1065 }
1066
1067 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001068 std::unique_lock<std::mutex> lock(mLock);
1069 base::ScopedLockAssertion assumeLocked(mLock);
1070 const bool configureCalled =
1071 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1072 return mConfigureWasCalled;
1073 });
1074 if (!configureCalled) {
1075 FAIL() << "Expected configure() to have been called.";
1076 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001077 mConfigureWasCalled = false;
1078 }
1079
1080 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001081 std::unique_lock<std::mutex> lock(mLock);
1082 base::ScopedLockAssertion assumeLocked(mLock);
1083 const bool resetCalled =
1084 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1085 return mResetWasCalled;
1086 });
1087 if (!resetCalled) {
1088 FAIL() << "Expected reset() to have been called.";
1089 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090 mResetWasCalled = false;
1091 }
1092
Yi Kong9b14ac62018-07-17 13:48:38 -07001093 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001094 std::unique_lock<std::mutex> lock(mLock);
1095 base::ScopedLockAssertion assumeLocked(mLock);
1096 const bool processCalled =
1097 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1098 return mProcessWasCalled;
1099 });
1100 if (!processCalled) {
1101 FAIL() << "Expected process() to have been called.";
1102 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001103 if (outLastEvent) {
1104 *outLastEvent = mLastEvent;
1105 }
1106 mProcessWasCalled = false;
1107 }
1108
1109 void setKeyCodeState(int32_t keyCode, int32_t state) {
1110 mKeyCodeStates.replaceValueFor(keyCode, state);
1111 }
1112
1113 void setScanCodeState(int32_t scanCode, int32_t state) {
1114 mScanCodeStates.replaceValueFor(scanCode, state);
1115 }
1116
1117 void setSwitchState(int32_t switchCode, int32_t state) {
1118 mSwitchStates.replaceValueFor(switchCode, state);
1119 }
1120
1121 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001122 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001123 }
1124
1125private:
Chris Yea52ade12020-08-27 16:49:20 -07001126 uint32_t getSources() override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001127
Chris Yea52ade12020-08-27 16:49:20 -07001128 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001129 InputMapper::populateDeviceInfo(deviceInfo);
1130
1131 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1132 deviceInfo->setKeyboardType(mKeyboardType);
1133 }
1134 }
1135
Chris Yea52ade12020-08-27 16:49:20 -07001136 void configure(nsecs_t, const InputReaderConfiguration* config, uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001137 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001138 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001139
1140 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001141 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001142 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1143 mViewport = config->getDisplayViewportByPort(*displayPort);
1144 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001145
1146 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001147 }
1148
Chris Yea52ade12020-08-27 16:49:20 -07001149 void reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001150 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001151 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001152 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001153 }
1154
Chris Yea52ade12020-08-27 16:49:20 -07001155 void process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001156 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001157 mLastEvent = *rawEvent;
1158 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001159 mStateChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001160 }
1161
Chris Yea52ade12020-08-27 16:49:20 -07001162 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001163 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1164 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1165 }
1166
Chris Yea52ade12020-08-27 16:49:20 -07001167 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001168 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1169 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1170 }
1171
Chris Yea52ade12020-08-27 16:49:20 -07001172 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001173 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1174 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1175 }
1176
Chris Yea52ade12020-08-27 16:49:20 -07001177 // Return true if the device has non-empty key layout.
1178 bool markSupportedKeyCodes(uint32_t, size_t numCodes, const int32_t* keyCodes,
1179 uint8_t* outFlags) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001180 for (size_t i = 0; i < numCodes; i++) {
1181 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1182 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1183 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001184 }
1185 }
1186 }
Chris Yea52ade12020-08-27 16:49:20 -07001187 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001188 return result;
1189 }
1190
1191 virtual int32_t getMetaState() {
1192 return mMetaState;
1193 }
1194
1195 virtual void fadePointer() {
1196 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001197
1198 virtual std::optional<int32_t> getAssociatedDisplay() {
1199 if (mViewport) {
1200 return std::make_optional(mViewport->displayId);
1201 }
1202 return std::nullopt;
1203 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001204};
1205
1206
1207// --- InstrumentedInputReader ---
1208
1209class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001210 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001211
1212public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001213 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1214 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001215 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001216 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001217
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001218 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001219
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001220 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001221
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001222 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001223 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001224 InputDeviceIdentifier identifier;
1225 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001226 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001227 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001228 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001229 }
1230
Prabir Pradhan28efc192019-11-05 01:10:04 +00001231 // Make the protected loopOnce method accessible to tests.
1232 using InputReader::loopOnce;
1233
Michael Wrightd02c5b62014-02-10 15:10:22 -08001234protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001235 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1236 const InputDeviceIdentifier& identifier)
1237 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001238 if (!mNextDevices.empty()) {
1239 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1240 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001241 return device;
1242 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001243 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001244 }
1245
arthurhungdcef2dc2020-08-11 14:47:50 +08001246 // --- FakeInputReaderContext ---
1247 class FakeInputReaderContext : public ContextImpl {
1248 int32_t mGlobalMetaState;
1249 bool mUpdateGlobalMetaStateWasCalled;
1250 int32_t mGeneration;
1251
1252 public:
1253 FakeInputReaderContext(InputReader* reader)
1254 : ContextImpl(reader),
1255 mGlobalMetaState(0),
1256 mUpdateGlobalMetaStateWasCalled(false),
1257 mGeneration(1) {}
1258
1259 virtual ~FakeInputReaderContext() {}
1260
1261 void assertUpdateGlobalMetaStateWasCalled() {
1262 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1263 << "Expected updateGlobalMetaState() to have been called.";
1264 mUpdateGlobalMetaStateWasCalled = false;
1265 }
1266
1267 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1268
1269 uint32_t getGeneration() { return mGeneration; }
1270
1271 void updateGlobalMetaState() override {
1272 mUpdateGlobalMetaStateWasCalled = true;
1273 ContextImpl::updateGlobalMetaState();
1274 }
1275
1276 int32_t getGlobalMetaState() override {
1277 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1278 }
1279
1280 int32_t bumpGeneration() override {
1281 mGeneration = ContextImpl::bumpGeneration();
1282 return mGeneration;
1283 }
1284 } mFakeContext;
1285
Michael Wrightd02c5b62014-02-10 15:10:22 -08001286 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001287
1288public:
1289 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001290};
1291
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001292// --- InputReaderPolicyTest ---
1293class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001294protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001295 sp<FakeInputReaderPolicy> mFakePolicy;
1296
Chris Yea52ade12020-08-27 16:49:20 -07001297 void SetUp() override { mFakePolicy = new FakeInputReaderPolicy(); }
1298 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001299};
1300
1301/**
1302 * Check that empty set of viewports is an acceptable configuration.
1303 * Also try to get internal viewport two different ways - by type and by uniqueId.
1304 *
1305 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1306 * Such configuration is not currently allowed.
1307 */
1308TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001309 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001310
1311 // We didn't add any viewports yet, so there shouldn't be any.
1312 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001313 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001314 ASSERT_FALSE(internalViewport);
1315
1316 // Add an internal viewport, then clear it
1317 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001318 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001319 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001320
1321 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001322 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001323 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001324 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001325
1326 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001327 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001328 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001329 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001330
1331 mFakePolicy->clearViewports();
1332 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001333 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001334 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001335 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001336 ASSERT_FALSE(internalViewport);
1337}
1338
1339TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1340 const std::string internalUniqueId = "local:0";
1341 const std::string externalUniqueId = "local:1";
1342 const std::string virtualUniqueId1 = "virtual:2";
1343 const std::string virtualUniqueId2 = "virtual:3";
1344 constexpr int32_t virtualDisplayId1 = 2;
1345 constexpr int32_t virtualDisplayId2 = 3;
1346
1347 // Add an internal viewport
1348 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001349 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1350 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001351 // Add an external viewport
1352 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001353 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1354 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001355 // Add an virtual viewport
1356 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001357 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1358 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001359 // Add another virtual viewport
1360 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001361 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1362 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001363
1364 // Check matching by type for internal
1365 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001366 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001367 ASSERT_TRUE(internalViewport);
1368 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1369
1370 // Check matching by type for external
1371 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001372 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001373 ASSERT_TRUE(externalViewport);
1374 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1375
1376 // Check matching by uniqueId for virtual viewport #1
1377 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001378 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001379 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001380 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001381 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1382 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1383
1384 // Check matching by uniqueId for virtual viewport #2
1385 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001386 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001387 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001388 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001389 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1390 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1391}
1392
1393
1394/**
1395 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1396 * that lookup works by checking display id.
1397 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1398 */
1399TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1400 const std::string uniqueId1 = "uniqueId1";
1401 const std::string uniqueId2 = "uniqueId2";
1402 constexpr int32_t displayId1 = 2;
1403 constexpr int32_t displayId2 = 3;
1404
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001405 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1406 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001407 for (const ViewportType& type : types) {
1408 mFakePolicy->clearViewports();
1409 // Add a viewport
1410 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001411 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1412 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001413 // Add another viewport
1414 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001415 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1416 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001417
1418 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001419 std::optional<DisplayViewport> viewport1 =
1420 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001421 ASSERT_TRUE(viewport1);
1422 ASSERT_EQ(displayId1, viewport1->displayId);
1423 ASSERT_EQ(type, viewport1->type);
1424
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001425 std::optional<DisplayViewport> viewport2 =
1426 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001427 ASSERT_TRUE(viewport2);
1428 ASSERT_EQ(displayId2, viewport2->displayId);
1429 ASSERT_EQ(type, viewport2->type);
1430
1431 // When there are multiple viewports of the same kind, and uniqueId is not specified
1432 // in the call to getDisplayViewport, then that situation is not supported.
1433 // The viewports can be stored in any order, so we cannot rely on the order, since that
1434 // is just implementation detail.
1435 // However, we can check that it still returns *a* viewport, we just cannot assert
1436 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001437 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001438 ASSERT_TRUE(someViewport);
1439 }
1440}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001441
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001442/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001443 * When we have multiple internal displays make sure we always return the default display when
1444 * querying by type.
1445 */
1446TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1447 const std::string uniqueId1 = "uniqueId1";
1448 const std::string uniqueId2 = "uniqueId2";
1449 constexpr int32_t nonDefaultDisplayId = 2;
1450 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1451 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1452
1453 // Add the default display first and ensure it gets returned.
1454 mFakePolicy->clearViewports();
1455 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001456 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001457 ViewportType::INTERNAL);
1458 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001459 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001460 ViewportType::INTERNAL);
1461
1462 std::optional<DisplayViewport> viewport =
1463 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1464 ASSERT_TRUE(viewport);
1465 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1466 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1467
1468 // Add the default display second to make sure order doesn't matter.
1469 mFakePolicy->clearViewports();
1470 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001471 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001472 ViewportType::INTERNAL);
1473 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001474 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001475 ViewportType::INTERNAL);
1476
1477 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1478 ASSERT_TRUE(viewport);
1479 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1480 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1481}
1482
1483/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001484 * Check getDisplayViewportByPort
1485 */
1486TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001487 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001488 const std::string uniqueId1 = "uniqueId1";
1489 const std::string uniqueId2 = "uniqueId2";
1490 constexpr int32_t displayId1 = 1;
1491 constexpr int32_t displayId2 = 2;
1492 const uint8_t hdmi1 = 0;
1493 const uint8_t hdmi2 = 1;
1494 const uint8_t hdmi3 = 2;
1495
1496 mFakePolicy->clearViewports();
1497 // Add a viewport that's associated with some display port that's not of interest.
1498 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001499 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1500 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001501 // Add another viewport, connected to HDMI1 port
1502 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001503 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1504 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001505
1506 // Check that correct display viewport was returned by comparing the display ports.
1507 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1508 ASSERT_TRUE(hdmi1Viewport);
1509 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1510 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1511
1512 // Check that we can still get the same viewport using the uniqueId
1513 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1514 ASSERT_TRUE(hdmi1Viewport);
1515 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1516 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1517 ASSERT_EQ(type, hdmi1Viewport->type);
1518
1519 // Check that we cannot find a port with "HDMI2", because we never added one
1520 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1521 ASSERT_FALSE(hdmi2Viewport);
1522}
1523
Michael Wrightd02c5b62014-02-10 15:10:22 -08001524// --- InputReaderTest ---
1525
1526class InputReaderTest : public testing::Test {
1527protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001528 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001529 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001530 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001531 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001532
Chris Yea52ade12020-08-27 16:49:20 -07001533 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001534 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001535 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001536 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001537
Prabir Pradhan28efc192019-11-05 01:10:04 +00001538 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001539 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001540 }
1541
Chris Yea52ade12020-08-27 16:49:20 -07001542 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001543 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001544 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001545 }
1546
Chris Ye1b0c7342020-07-28 21:57:03 -07001547 void addDevice(int32_t eventHubId, const std::string& name, Flags<InputDeviceClass> classes,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001548 const PropertyMap* configuration) {
1549 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001550
1551 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001552 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001553 }
1554 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001555 mReader->loopOnce();
1556 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001557 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1558 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001559 }
1560
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001561 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001562 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001563 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001564 }
1565
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001566 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001567 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001568 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001569 }
1570
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001571 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001572 const std::string& name,
1573 Flags<InputDeviceClass> classes, uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001574 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001575 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1576 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001577 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001578 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001579 return mapper;
1580 }
1581};
1582
Chris Ye98d3f532020-10-01 21:48:59 -07001583TEST_F(InputReaderTest, PolicyGetInputDevices) {
1584 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
1585 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", Flags<InputDeviceClass>(0),
1586 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001587
1588 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001589 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001590 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001591 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001592 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001593 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1594 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001595 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001596}
1597
Chris Yee7310032020-09-22 15:36:28 -07001598TEST_F(InputReaderTest, GetMergedInputDevices) {
1599 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1600 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1601 // Add two subdevices to device
1602 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1603 // Must add at least one mapper or the device will be ignored!
1604 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1605 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1606
1607 // Push same device instance for next device to be added, so they'll have same identifier.
1608 mReader->pushNextDevice(device);
1609 mReader->pushNextDevice(device);
1610 ASSERT_NO_FATAL_FAILURE(
1611 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1612 ASSERT_NO_FATAL_FAILURE(
1613 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1614
1615 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001616 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001617}
1618
Chris Yee14523a2020-12-19 13:46:00 -08001619TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1620 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1621 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1622 // Add two subdevices to device
1623 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1624 // Must add at least one mapper or the device will be ignored!
1625 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1626 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1627
1628 // Push same device instance for next device to be added, so they'll have same identifier.
1629 mReader->pushNextDevice(device);
1630 mReader->pushNextDevice(device);
1631 // Sensor device is initially disabled
1632 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1633 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1634 nullptr));
1635 // Device is disabled because the only sub device is a sensor device and disabled initially.
1636 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1637 ASSERT_FALSE(device->isEnabled());
1638 ASSERT_NO_FATAL_FAILURE(
1639 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1640 // The merged device is enabled if any sub device is enabled
1641 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1642 ASSERT_TRUE(device->isEnabled());
1643}
1644
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001645TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001646 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001647 constexpr Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001648 constexpr int32_t eventHubId = 1;
1649 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001650 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001651 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001652 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001653 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001654
Yi Kong9b14ac62018-07-17 13:48:38 -07001655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001656
1657 NotifyDeviceResetArgs resetArgs;
1658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001659 ASSERT_EQ(deviceId, resetArgs.deviceId);
1660
1661 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001662 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001663 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001664
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001666 ASSERT_EQ(deviceId, resetArgs.deviceId);
1667 ASSERT_EQ(device->isEnabled(), false);
1668
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001669 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001670 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001673 ASSERT_EQ(device->isEnabled(), false);
1674
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001675 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001676 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001678 ASSERT_EQ(deviceId, resetArgs.deviceId);
1679 ASSERT_EQ(device->isEnabled(), true);
1680}
1681
Michael Wrightd02c5b62014-02-10 15:10:22 -08001682TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001683 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001684 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001685 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001686 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001687 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001688 AINPUT_SOURCE_KEYBOARD, nullptr);
1689 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001690
1691 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1692 AINPUT_SOURCE_ANY, AKEYCODE_A))
1693 << "Should return unknown when the device id is >= 0 but unknown.";
1694
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001695 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1696 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1697 << "Should return unknown when the device id is valid but the sources are not "
1698 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001699
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001700 ASSERT_EQ(AKEY_STATE_DOWN,
1701 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1702 AKEYCODE_A))
1703 << "Should return value provided by mapper when device id is valid and the device "
1704 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001705
1706 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1707 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1708 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1709
1710 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1711 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1712 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1713}
1714
1715TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001716 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001717 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001718 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001719 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001720 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001721 AINPUT_SOURCE_KEYBOARD, nullptr);
1722 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001723
1724 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1725 AINPUT_SOURCE_ANY, KEY_A))
1726 << "Should return unknown when the device id is >= 0 but unknown.";
1727
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001728 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1729 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1730 << "Should return unknown when the device id is valid but the sources are not "
1731 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001732
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001733 ASSERT_EQ(AKEY_STATE_DOWN,
1734 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1735 KEY_A))
1736 << "Should return value provided by mapper when device id is valid and the device "
1737 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001738
1739 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1740 AINPUT_SOURCE_TRACKBALL, KEY_A))
1741 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1742
1743 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1744 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1745 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1746}
1747
1748TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001749 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001750 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001751 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001752 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001753 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001754 AINPUT_SOURCE_KEYBOARD, nullptr);
1755 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001756
1757 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1758 AINPUT_SOURCE_ANY, SW_LID))
1759 << "Should return unknown when the device id is >= 0 but unknown.";
1760
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001761 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1762 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1763 << "Should return unknown when the device id is valid but the sources are not "
1764 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001765
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001766 ASSERT_EQ(AKEY_STATE_DOWN,
1767 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1768 SW_LID))
1769 << "Should return value provided by mapper when device id is valid and the device "
1770 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001771
1772 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1773 AINPUT_SOURCE_TRACKBALL, SW_LID))
1774 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1775
1776 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1777 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1778 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1779}
1780
1781TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001782 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001783 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001784 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001785 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001786 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001787 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001788
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001789 mapper.addSupportedKeyCode(AKEYCODE_A);
1790 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001791
1792 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
1793 uint8_t flags[4] = { 0, 0, 0, 1 };
1794
1795 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, 4, keyCodes, flags))
1796 << "Should return false when device id is >= 0 but unknown.";
1797 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1798
1799 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001800 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1801 << "Should return false when device id is valid but the sources are not supported by "
1802 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001803 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1804
1805 flags[3] = 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001806 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4,
1807 keyCodes, flags))
1808 << "Should return value provided by mapper when device id is valid and the device "
1809 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001810 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1811
1812 flags[3] = 1;
1813 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1814 << "Should return false when the device id is < 0 but the sources are not supported by any device.";
1815 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1816
1817 flags[3] = 1;
1818 ASSERT_TRUE(mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
1819 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1820 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1821}
1822
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001823TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001824 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001825 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001826
1827 NotifyConfigurationChangedArgs args;
1828
1829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1830 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1831}
1832
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001833TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001834 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001835 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001836 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001837 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001838 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001839 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001840 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001841 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001842
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001843 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001844 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001845 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1846
1847 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001848 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001849 ASSERT_EQ(when, event.when);
1850 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001851 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001852 ASSERT_EQ(EV_KEY, event.type);
1853 ASSERT_EQ(KEY_A, event.code);
1854 ASSERT_EQ(1, event.value);
1855}
1856
Garfield Tan1c7bc862020-01-28 13:24:04 -08001857TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001858 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001859 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001860 constexpr int32_t eventHubId = 1;
1861 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001862 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001863 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001864 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001865 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001866
1867 NotifyDeviceResetArgs resetArgs;
1868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001869 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001870
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001871 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001872 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001874 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001875 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001876
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001877 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001878 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001880 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001881 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001882
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001883 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001884 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001886 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001887 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001888}
1889
Garfield Tan1c7bc862020-01-28 13:24:04 -08001890TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1891 constexpr int32_t deviceId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001892 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001893 constexpr int32_t eventHubId = 1;
1894 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1895 // Must add at least one mapper or the device will be ignored!
1896 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001897 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001898 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1899
1900 NotifyDeviceResetArgs resetArgs;
1901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1902 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1903}
1904
Arthur Hungc23540e2018-11-29 20:42:11 +08001905TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001906 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Chris Ye1b0c7342020-07-28 21:57:03 -07001907 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001908 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001909 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001910 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1911 FakeInputMapper& mapper =
1912 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001913 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001914
1915 const uint8_t hdmi1 = 1;
1916
1917 // Associated touch screen with second display.
1918 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1919
1920 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001921 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001922 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001923 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001924 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001925 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001926 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001927 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001928 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001929 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001930
1931 // Add the device, and make sure all of the callbacks are triggered.
1932 // The device is added after the input port associations are processed since
1933 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001934 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001937 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001938
Arthur Hung2c9a3342019-07-23 14:18:59 +08001939 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001940 ASSERT_EQ(deviceId, device->getId());
1941 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1942 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001943
1944 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001945 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001946 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001947 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001948}
1949
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001950TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1951 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1952 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1953 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1954 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1955 // Must add at least one mapper or the device will be ignored!
1956 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1957 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1958 mReader->pushNextDevice(device);
1959 mReader->pushNextDevice(device);
1960 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1961 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1962
1963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1964
1965 NotifyDeviceResetArgs resetArgs;
1966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1967 ASSERT_EQ(deviceId, resetArgs.deviceId);
1968 ASSERT_TRUE(device->isEnabled());
1969 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1970 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1971
1972 disableDevice(deviceId);
1973 mReader->loopOnce();
1974
1975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1976 ASSERT_EQ(deviceId, resetArgs.deviceId);
1977 ASSERT_FALSE(device->isEnabled());
1978 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1979 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1980
1981 enableDevice(deviceId);
1982 mReader->loopOnce();
1983
1984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1985 ASSERT_EQ(deviceId, resetArgs.deviceId);
1986 ASSERT_TRUE(device->isEnabled());
1987 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1988 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1989}
1990
1991TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1992 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1993 constexpr Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
1994 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1995 // Add two subdevices to device
1996 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1997 FakeInputMapper& mapperDevice1 =
1998 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1999 FakeInputMapper& mapperDevice2 =
2000 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2001 mReader->pushNextDevice(device);
2002 mReader->pushNextDevice(device);
2003 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2004 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2005
2006 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2007 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2008
2009 ASSERT_EQ(AKEY_STATE_DOWN,
2010 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2011 ASSERT_EQ(AKEY_STATE_DOWN,
2012 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2013 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2014 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2015}
2016
Prabir Pradhan7e186182020-11-10 13:56:45 -08002017TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2018 NotifyPointerCaptureChangedArgs args;
2019
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002020 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002021 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2022 mReader->loopOnce();
2023 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002024 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2025 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002026
2027 mFakePolicy->setPointerCapture(false);
2028 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2029 mReader->loopOnce();
2030 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002031 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002032
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002033 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002034 // does not change.
2035 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2036 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002037 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002038}
2039
Chris Ye87143712020-11-10 05:05:58 +00002040class FakeVibratorInputMapper : public FakeInputMapper {
2041public:
2042 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2043 : FakeInputMapper(deviceContext, sources) {}
2044
2045 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2046};
2047
2048TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2049 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2050 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
2051 constexpr int32_t eventHubId = 1;
2052 const char* DEVICE_LOCATION = "BLUETOOTH";
2053 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2054 FakeVibratorInputMapper& mapper =
2055 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2056 mReader->pushNextDevice(device);
2057
2058 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2059 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2060
2061 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2062 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2063}
2064
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002065// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002066
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002067class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002068public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002069 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002070
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002071 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002072
2073 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2074
2075 void dump(std::string& dump) override {}
2076
2077 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2078 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002079 }
2080
Chris Yee2b1e5c2021-03-10 22:45:12 -08002081 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2082 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002083 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002084
2085 bool setLightColor(int32_t lightId, int32_t color) override {
2086 getDeviceContext().setLightBrightness(lightId, color >> 24);
2087 return true;
2088 }
2089
2090 std::optional<int32_t> getLightColor(int32_t lightId) override {
2091 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2092 if (!result.has_value()) {
2093 return std::nullopt;
2094 }
2095 return result.value() << 24;
2096 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002097
2098 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2099
2100 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2101
2102private:
2103 InputDeviceContext& mDeviceContext;
2104 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2105 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002106};
2107
Chris Yee2b1e5c2021-03-10 22:45:12 -08002108TEST_F(InputReaderTest, BatteryGetCapacity) {
2109 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2110 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2111 constexpr int32_t eventHubId = 1;
2112 const char* DEVICE_LOCATION = "BLUETOOTH";
2113 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002114 FakePeripheralController& controller =
2115 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002116 mReader->pushNextDevice(device);
2117
2118 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2119
2120 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2121 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2122}
2123
2124TEST_F(InputReaderTest, BatteryGetStatus) {
2125 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2126 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2127 constexpr int32_t eventHubId = 1;
2128 const char* DEVICE_LOCATION = "BLUETOOTH";
2129 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002130 FakePeripheralController& controller =
2131 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002132 mReader->pushNextDevice(device);
2133
2134 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2135
2136 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2137 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2138}
2139
Chris Ye3fdbfef2021-01-06 18:45:18 -08002140TEST_F(InputReaderTest, LightGetColor) {
2141 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2142 Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
2143 constexpr int32_t eventHubId = 1;
2144 const char* DEVICE_LOCATION = "BLUETOOTH";
2145 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002146 FakePeripheralController& controller =
2147 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002148 mReader->pushNextDevice(device);
2149 RawLightInfo info = {.id = 1,
2150 .name = "Mono",
2151 .maxBrightness = 255,
2152 .flags = InputLightClass::BRIGHTNESS,
2153 .path = ""};
2154 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2155 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2156
2157 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002158
Chris Yee2b1e5c2021-03-10 22:45:12 -08002159 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2160 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002161 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2162 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2163}
2164
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002165// --- InputReaderIntegrationTest ---
2166
2167// These tests create and interact with the InputReader only through its interface.
2168// The InputReader is started during SetUp(), which starts its processing in its own
2169// thread. The tests use linux uinput to emulate input devices.
2170// NOTE: Interacting with the physical device while these tests are running may cause
2171// the tests to fail.
2172class InputReaderIntegrationTest : public testing::Test {
2173protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002174 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002175 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002176 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002177
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002178 std::shared_ptr<FakePointerController> mFakePointerController;
2179
Chris Yea52ade12020-08-27 16:49:20 -07002180 void SetUp() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002181 mFakePolicy = new FakeInputReaderPolicy();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002182 mFakePointerController = std::make_shared<FakePointerController>();
2183 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002184 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2185 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002186
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002187 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2188 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002189 ASSERT_EQ(mReader->start(), OK);
2190
2191 // Since this test is run on a real device, all the input devices connected
2192 // to the test device will show up in mReader. We wait for those input devices to
2193 // show up before beginning the tests.
2194 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2195 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2196 }
2197
Chris Yea52ade12020-08-27 16:49:20 -07002198 void TearDown() override {
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002199 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002200 mReader.reset();
2201 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002202 mFakePolicy.clear();
2203 }
2204};
2205
2206TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2207 // An invalid input device that is only used for this test.
2208 class InvalidUinputDevice : public UinputDevice {
2209 public:
2210 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2211
2212 private:
2213 void configureDevice(int fd, uinput_user_dev* device) override {}
2214 };
2215
2216 const size_t numDevices = mFakePolicy->getInputDevices().size();
2217
2218 // UinputDevice does not set any event or key bits, so InputReader should not
2219 // consider it as a valid device.
2220 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2221 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2222 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2223 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2224
2225 invalidDevice.reset();
2226 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2227 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2228 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2229}
2230
2231TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2232 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2233
2234 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2235 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2236 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2237 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2238
2239 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002240 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002241 const auto& it =
2242 std::find_if(inputDevices.begin(), inputDevices.end(),
2243 [&keyboard](const InputDeviceInfo& info) {
2244 return info.getIdentifier().name == keyboard->getName();
2245 });
2246
2247 ASSERT_NE(it, inputDevices.end());
2248 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2249 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2250 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002251
2252 keyboard.reset();
2253 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2254 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2255 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2256}
2257
2258TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2259 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2260 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2261
2262 NotifyConfigurationChangedArgs configChangedArgs;
2263 ASSERT_NO_FATAL_FAILURE(
2264 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002265 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002266 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2267
2268 NotifyKeyArgs keyArgs;
2269 keyboard->pressAndReleaseHomeKey();
2270 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2271 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002272 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002273 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002274 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002275 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002276 prevTimestamp = keyArgs.eventTime;
2277
2278 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2279 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002280 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002281 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002282 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002283}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002284
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002285/**
2286 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2287 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2288 * are passed to the listener.
2289 */
2290static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2291TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2292 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2293 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2294 NotifyKeyArgs keyArgs;
2295
2296 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2297 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2298 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2299 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2300
2301 controller->pressAndReleaseKey(BTN_GEAR_UP);
2302 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2303 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2304 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2305}
2306
Arthur Hungaab25622020-01-16 11:22:11 +08002307// --- TouchProcessTest ---
2308class TouchIntegrationTest : public InputReaderIntegrationTest {
2309protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002310 const std::string UNIQUE_ID = "local:0";
2311
Chris Yea52ade12020-08-27 16:49:20 -07002312 void SetUp() override {
Arthur Hungaab25622020-01-16 11:22:11 +08002313 InputReaderIntegrationTest::SetUp();
2314 // At least add an internal display.
2315 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2316 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002317 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002318
2319 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2320 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2321 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2322 }
2323
2324 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2325 int32_t orientation, const std::string& uniqueId,
2326 std::optional<uint8_t> physicalPort,
2327 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002328 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2329 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002330 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2331 }
2332
2333 std::unique_ptr<UinputTouchScreen> mDevice;
2334};
2335
2336TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2337 NotifyMotionArgs args;
2338 const Point centerPoint = mDevice->getCenterPoint();
2339
2340 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002341 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002342 mDevice->sendDown(centerPoint);
2343 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2344 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2345
2346 // ACTION_MOVE
2347 mDevice->sendMove(centerPoint + Point(1, 1));
2348 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2350
2351 // ACTION_UP
2352 mDevice->sendUp();
2353 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2354 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2355}
2356
2357TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2358 NotifyMotionArgs args;
2359 const Point centerPoint = mDevice->getCenterPoint();
2360
2361 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002362 mDevice->sendSlot(FIRST_SLOT);
2363 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002364 mDevice->sendDown(centerPoint);
2365 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2366 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2367
2368 // ACTION_POINTER_DOWN (Second slot)
2369 const Point secondPoint = centerPoint + Point(100, 100);
2370 mDevice->sendSlot(SECOND_SLOT);
2371 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2372 mDevice->sendDown(secondPoint + Point(1, 1));
2373 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002374 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002375
2376 // ACTION_MOVE (Second slot)
2377 mDevice->sendMove(secondPoint);
2378 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2380
2381 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002382 mDevice->sendPointerUp();
Arthur Hungaab25622020-01-16 11:22:11 +08002383 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002384 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002385
2386 // ACTION_UP
2387 mDevice->sendSlot(FIRST_SLOT);
2388 mDevice->sendUp();
2389 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2390 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2391}
2392
2393TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2394 NotifyMotionArgs args;
2395 const Point centerPoint = mDevice->getCenterPoint();
2396
2397 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002398 mDevice->sendSlot(FIRST_SLOT);
2399 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002400 mDevice->sendDown(centerPoint);
2401 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2402 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2403
arthurhungcc7f9802020-04-30 17:55:40 +08002404 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002405 const Point secondPoint = centerPoint + Point(100, 100);
2406 mDevice->sendSlot(SECOND_SLOT);
2407 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2408 mDevice->sendDown(secondPoint);
2409 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002410 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002411
arthurhungcc7f9802020-04-30 17:55:40 +08002412 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002413 mDevice->sendMove(secondPoint + Point(1, 1));
2414 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2415 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2416
arthurhungcc7f9802020-04-30 17:55:40 +08002417 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2418 // a palm event.
2419 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002420 mDevice->sendToolType(MT_TOOL_PALM);
2421 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002422 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002423 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002424
arthurhungcc7f9802020-04-30 17:55:40 +08002425 // Send up to second slot, expect first slot send moving.
2426 mDevice->sendPointerUp();
2427 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002429
arthurhungcc7f9802020-04-30 17:55:40 +08002430 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002431 mDevice->sendSlot(FIRST_SLOT);
2432 mDevice->sendUp();
2433
arthurhungcc7f9802020-04-30 17:55:40 +08002434 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2435 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002436}
2437
Michael Wrightd02c5b62014-02-10 15:10:22 -08002438// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002439class InputDeviceTest : public testing::Test {
2440protected:
2441 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002442 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002443 static const int32_t DEVICE_ID;
2444 static const int32_t DEVICE_GENERATION;
2445 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002446 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002447 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002448
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002449 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002450 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002451 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002452 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002453 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002454
Chris Yea52ade12020-08-27 16:49:20 -07002455 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002456 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002457 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002458 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002459 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002460 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002461 InputDeviceIdentifier identifier;
2462 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002463 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002464 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002465 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002466 mReader->pushNextDevice(mDevice);
2467 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, Flags<InputDeviceClass>(0));
2468 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002469 }
2470
Chris Yea52ade12020-08-27 16:49:20 -07002471 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002472 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002473 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002474 }
2475};
2476
2477const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002478const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002479const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002480const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2481const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002482const Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2483 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002484const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002485
2486TEST_F(InputDeviceTest, ImmutableProperties) {
2487 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002488 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Chris Ye1b0c7342020-07-28 21:57:03 -07002489 ASSERT_EQ(Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002490}
2491
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002492TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2493 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002494}
2495
Michael Wrightd02c5b62014-02-10 15:10:22 -08002496TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2497 // Configuration.
2498 InputReaderConfiguration config;
2499 mDevice->configure(ARBITRARY_TIME, &config, 0);
2500
2501 // Reset.
2502 mDevice->reset(ARBITRARY_TIME);
2503
2504 NotifyDeviceResetArgs resetArgs;
2505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2506 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2507 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2508
2509 // Metadata.
2510 ASSERT_TRUE(mDevice->isIgnored());
2511 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2512
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002513 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002514 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002515 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002516 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2517 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2518
2519 // State queries.
2520 ASSERT_EQ(0, mDevice->getMetaState());
2521
2522 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2523 << "Ignored device should return unknown key code state.";
2524 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2525 << "Ignored device should return unknown scan code state.";
2526 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2527 << "Ignored device should return unknown switch state.";
2528
2529 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
2530 uint8_t flags[2] = { 0, 1 };
2531 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 2, keyCodes, flags))
2532 << "Ignored device should never mark any key codes.";
2533 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2534 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2535}
2536
2537TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2538 // Configuration.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002539 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8("key"), String8("value"));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002540
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002541 FakeInputMapper& mapper1 =
2542 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002543 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2544 mapper1.setMetaState(AMETA_ALT_ON);
2545 mapper1.addSupportedKeyCode(AKEYCODE_A);
2546 mapper1.addSupportedKeyCode(AKEYCODE_B);
2547 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2548 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2549 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2550 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2551 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002552
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002553 FakeInputMapper& mapper2 =
2554 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002555 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002556
2557 InputReaderConfiguration config;
2558 mDevice->configure(ARBITRARY_TIME, &config, 0);
2559
2560 String8 propertyValue;
2561 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty(String8("key"), propertyValue))
2562 << "Device should have read configuration during configuration phase.";
2563 ASSERT_STREQ("value", propertyValue.string());
2564
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002565 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2566 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002567
2568 // Reset
2569 mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002570 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2571 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002572
2573 NotifyDeviceResetArgs resetArgs;
2574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2575 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2576 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2577
2578 // Metadata.
2579 ASSERT_FALSE(mDevice->isIgnored());
2580 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2581
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002582 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002583 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002584 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002585 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2586 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2587
2588 // State queries.
2589 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2590 << "Should query mappers and combine meta states.";
2591
2592 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2593 << "Should return unknown key code state when source not supported.";
2594 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2595 << "Should return unknown scan code state when source not supported.";
2596 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2597 << "Should return unknown switch state when source not supported.";
2598
2599 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2600 << "Should query mapper when source is supported.";
2601 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2602 << "Should query mapper when source is supported.";
2603 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2604 << "Should query mapper when source is supported.";
2605
2606 const int32_t keyCodes[4] = { AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2 };
2607 uint8_t flags[4] = { 0, 0, 0, 1 };
2608 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, 4, keyCodes, flags))
2609 << "Should do nothing when source is unsupported.";
2610 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2611 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2612 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2613 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2614
2615 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, 4, keyCodes, flags))
2616 << "Should query mapper when source is supported.";
2617 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2618 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2619 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2620 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2621
2622 // Event handling.
2623 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002624 event.deviceId = EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002625 mDevice->process(&event, 1);
2626
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002627 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2628 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002629}
2630
Arthur Hung2c9a3342019-07-23 14:18:59 +08002631// A single input device is associated with a specific display. Check that:
2632// 1. Device is disabled if the viewport corresponding to the associated display is not found
2633// 2. Device is disabled when setEnabled API is called
2634TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002635 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002636
2637 // First Configuration.
2638 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2639
2640 // Device should be enabled by default.
2641 ASSERT_TRUE(mDevice->isEnabled());
2642
2643 // Prepare associated info.
2644 constexpr uint8_t hdmi = 1;
2645 const std::string UNIQUE_ID = "local:1";
2646
2647 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2648 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2649 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2650 // Device should be disabled because it is associated with a specific display via
2651 // input port <-> display port association, but the corresponding display is not found
2652 ASSERT_FALSE(mDevice->isEnabled());
2653
2654 // Prepare displays.
2655 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002656 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2657 ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002658 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2659 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2660 ASSERT_TRUE(mDevice->isEnabled());
2661
2662 // Device should be disabled after set disable.
2663 mFakePolicy->addDisabledDevice(mDevice->getId());
2664 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2665 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2666 ASSERT_FALSE(mDevice->isEnabled());
2667
2668 // Device should still be disabled even found the associated display.
2669 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2670 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2671 ASSERT_FALSE(mDevice->isEnabled());
2672}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002673
Christine Franks1ba71cc2021-04-07 14:37:42 -07002674TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2675 // Device should be enabled by default.
2676 mFakePolicy->clearViewports();
2677 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2678 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2679 ASSERT_TRUE(mDevice->isEnabled());
2680
2681 // Device should be disabled because it is associated with a specific display, but the
2682 // corresponding display is not found.
2683 const std::string DISPLAY_UNIQUE_ID = "displayUniqueId";
2684 mFakePolicy->addInputUniqueIdAssociation(DEVICE_NAME, DISPLAY_UNIQUE_ID);
2685 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2686 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2687 ASSERT_FALSE(mDevice->isEnabled());
2688
2689 // Device should be enabled when a display is found.
2690 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2691 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2692 NO_PORT, ViewportType::INTERNAL);
2693 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2694 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2695 ASSERT_TRUE(mDevice->isEnabled());
2696
2697 // Device should be disabled after set disable.
2698 mFakePolicy->addDisabledDevice(mDevice->getId());
2699 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2700 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2701 ASSERT_FALSE(mDevice->isEnabled());
2702
2703 // Device should still be disabled even found the associated display.
2704 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2705 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2706 ASSERT_FALSE(mDevice->isEnabled());
2707}
2708
Michael Wrightd02c5b62014-02-10 15:10:22 -08002709// --- InputMapperTest ---
2710
2711class InputMapperTest : public testing::Test {
2712protected:
2713 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002714 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002715 static const int32_t DEVICE_ID;
2716 static const int32_t DEVICE_GENERATION;
2717 static const int32_t DEVICE_CONTROLLER_NUMBER;
Chris Ye1b0c7342020-07-28 21:57:03 -07002718 static const Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002719 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002720
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002721 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002722 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002723 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002724 std::unique_ptr<InstrumentedInputReader> mReader;
2725 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002726
Chris Ye1b0c7342020-07-28 21:57:03 -07002727 virtual void SetUp(Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002728 mFakeEventHub = std::make_unique<FakeEventHub>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002729 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002730 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002731 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002732 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08002733 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002734 }
2735
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002736 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002737 SetUp(DEVICE_CLASSES);
2738 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002739
Chris Yea52ade12020-08-27 16:49:20 -07002740 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002741 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002742 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002743 }
2744
2745 void addConfigurationProperty(const char* key, const char* value) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002746 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, String8(key), String8(value));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002747 }
2748
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002749 void configureDevice(uint32_t changes) {
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002750 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002751 mReader->requestRefreshConfiguration(changes);
2752 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002753 }
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002754 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2755 }
2756
arthurhungdcef2dc2020-08-11 14:47:50 +08002757 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2758 const std::string& location, int32_t eventHubId,
2759 Flags<InputDeviceClass> classes) {
2760 InputDeviceIdentifier identifier;
2761 identifier.name = name;
2762 identifier.location = location;
2763 std::shared_ptr<InputDevice> device =
2764 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2765 identifier);
2766 mReader->pushNextDevice(device);
2767 mFakeEventHub->addDevice(eventHubId, name, classes);
2768 mReader->loopOnce();
2769 return device;
2770 }
2771
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002772 template <class T, typename... Args>
2773 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002774 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002775 configureDevice(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002776 mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002777 mapper.reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002778 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002779 }
2780
2781 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002782 int32_t orientation, const std::string& uniqueId,
2783 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002784 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2785 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002786 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2787 }
2788
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002789 void clearViewports() {
2790 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002791 }
2792
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002793 void process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type, int32_t code,
2794 int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002795 RawEvent event;
2796 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002797 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002798 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002799 event.type = type;
2800 event.code = code;
2801 event.value = value;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002802 mapper.process(&event);
arthurhungdcef2dc2020-08-11 14:47:50 +08002803 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002804 }
2805
2806 static void assertMotionRange(const InputDeviceInfo& info,
2807 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2808 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002809 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002810 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2811 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2812 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2813 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2814 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2815 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2816 }
2817
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002818 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
2819 float size, float touchMajor, float touchMinor, float toolMajor,
2820 float toolMinor, float orientation, float distance,
2821 float scaledAxisEpsilon = 1.f) {
2822 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
2823 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2825 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002826 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2827 scaledAxisEpsilon);
2828 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2829 scaledAxisEpsilon);
2830 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2831 scaledAxisEpsilon);
2832 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2833 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002834 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2835 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2836 }
2837
Michael Wright17db18e2020-06-26 20:51:44 +01002838 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002839 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002840 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002841 ASSERT_NEAR(x, actualX, 1);
2842 ASSERT_NEAR(y, actualY, 1);
2843 }
2844};
2845
2846const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002847const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002848const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002849const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2850const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Chris Ye1b0c7342020-07-28 21:57:03 -07002851const Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2852 Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002853const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002854
2855// --- SwitchInputMapperTest ---
2856
2857class SwitchInputMapperTest : public InputMapperTest {
2858protected:
2859};
2860
2861TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002862 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002863
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002864 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002865}
2866
2867TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002868 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002869
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002870 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002871 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002872
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002873 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002874 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002875}
2876
2877TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002878 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002879
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2881 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2882 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2883 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002884
2885 NotifySwitchArgs args;
2886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySwitchWasCalled(&args));
2887 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002888 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2889 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002890 args.switchMask);
2891 ASSERT_EQ(uint32_t(0), args.policyFlags);
2892}
2893
Chris Ye87143712020-11-10 05:05:58 +00002894// --- VibratorInputMapperTest ---
2895class VibratorInputMapperTest : public InputMapperTest {
2896protected:
2897 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2898};
2899
2900TEST_F(VibratorInputMapperTest, GetSources) {
2901 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2902
2903 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2904}
2905
2906TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2907 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2908
2909 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2910}
2911
2912TEST_F(VibratorInputMapperTest, Vibrate) {
2913 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002914 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002915 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2916
2917 VibrationElement pattern(2);
2918 VibrationSequence sequence(2);
2919 pattern.duration = std::chrono::milliseconds(200);
2920 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2921 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2922 sequence.addElement(pattern);
2923 pattern.duration = std::chrono::milliseconds(500);
2924 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2925 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2926 sequence.addElement(pattern);
2927
2928 std::vector<int64_t> timings = {0, 1};
2929 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2930
2931 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002932 // Start vibrating
2933 mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002934 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002935 // Verify vibrator state listener was notified.
2936 mReader->loopOnce();
2937 NotifyVibratorStateArgs args;
2938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2939 ASSERT_EQ(DEVICE_ID, args.deviceId);
2940 ASSERT_TRUE(args.isOn);
2941 // Stop vibrating
2942 mapper.cancelVibrate(VIBRATION_TOKEN);
2943 ASSERT_FALSE(mapper.isVibrating());
2944 // Verify vibrator state listener was notified.
2945 mReader->loopOnce();
2946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyVibratorStateWasCalled(&args));
2947 ASSERT_EQ(DEVICE_ID, args.deviceId);
2948 ASSERT_FALSE(args.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002949}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002950
Chris Yef59a2f42020-10-16 12:55:26 -07002951// --- SensorInputMapperTest ---
2952
2953class SensorInputMapperTest : public InputMapperTest {
2954protected:
2955 static const int32_t ACCEL_RAW_MIN;
2956 static const int32_t ACCEL_RAW_MAX;
2957 static const int32_t ACCEL_RAW_FUZZ;
2958 static const int32_t ACCEL_RAW_FLAT;
2959 static const int32_t ACCEL_RAW_RESOLUTION;
2960
2961 static const int32_t GYRO_RAW_MIN;
2962 static const int32_t GYRO_RAW_MAX;
2963 static const int32_t GYRO_RAW_FUZZ;
2964 static const int32_t GYRO_RAW_FLAT;
2965 static const int32_t GYRO_RAW_RESOLUTION;
2966
2967 static const float GRAVITY_MS2_UNIT;
2968 static const float DEGREE_RADIAN_UNIT;
2969
2970 void prepareAccelAxes();
2971 void prepareGyroAxes();
2972 void setAccelProperties();
2973 void setGyroProperties();
2974 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2975};
2976
2977const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2978const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2979const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2980const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2981const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2982
2983const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2984const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2985const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2986const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2987const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2988
2989const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2990const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2991
2992void SensorInputMapperTest::prepareAccelAxes() {
2993 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2994 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2995 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2996 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2997 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2998 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2999}
3000
3001void SensorInputMapperTest::prepareGyroAxes() {
3002 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3003 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3004 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3005 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3006 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3007 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3008}
3009
3010void SensorInputMapperTest::setAccelProperties() {
3011 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3012 /* sensorDataIndex */ 0);
3013 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3014 /* sensorDataIndex */ 1);
3015 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3016 /* sensorDataIndex */ 2);
3017 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3018 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3019 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3020 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3021 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3022}
3023
3024void SensorInputMapperTest::setGyroProperties() {
3025 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3026 /* sensorDataIndex */ 0);
3027 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3028 /* sensorDataIndex */ 1);
3029 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3030 /* sensorDataIndex */ 2);
3031 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3032 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3033 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3034 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3035 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3036}
3037
3038TEST_F(SensorInputMapperTest, GetSources) {
3039 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3040
3041 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3042}
3043
3044TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3045 setAccelProperties();
3046 prepareAccelAxes();
3047 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3048
3049 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3050 std::chrono::microseconds(10000),
3051 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003052 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3054 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3055 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3056 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3057 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003058
3059 NotifySensorArgs args;
3060 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3061 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3062 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3063
3064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3065 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3066 ASSERT_EQ(args.deviceId, DEVICE_ID);
3067 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3068 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3069 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3070 ASSERT_EQ(args.values, values);
3071 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3072}
3073
3074TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3075 setGyroProperties();
3076 prepareGyroAxes();
3077 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3078
3079 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3080 std::chrono::microseconds(10000),
3081 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003082 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3084 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3085 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3086 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3087 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003088
3089 NotifySensorArgs args;
3090 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3091 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3092 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3093
3094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3095 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3096 ASSERT_EQ(args.deviceId, DEVICE_ID);
3097 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3098 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3099 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3100 ASSERT_EQ(args.values, values);
3101 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3102}
3103
Michael Wrightd02c5b62014-02-10 15:10:22 -08003104// --- KeyboardInputMapperTest ---
3105
3106class KeyboardInputMapperTest : public InputMapperTest {
3107protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003108 const std::string UNIQUE_ID = "local:0";
3109
3110 void prepareDisplay(int32_t orientation);
3111
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003112 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003113 int32_t originalKeyCode, int32_t rotatedKeyCode,
3114 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003115};
3116
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003117/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3118 * orientation.
3119 */
3120void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003121 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3122 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003123}
3124
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003125void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003126 int32_t originalScanCode, int32_t originalKeyCode,
3127 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128 NotifyKeyArgs args;
3129
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003130 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3132 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3133 ASSERT_EQ(originalScanCode, args.scanCode);
3134 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003135 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003136
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3139 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3140 ASSERT_EQ(originalScanCode, args.scanCode);
3141 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003142 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003143}
3144
Michael Wrightd02c5b62014-02-10 15:10:22 -08003145TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003146 KeyboardInputMapper& mapper =
3147 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3148 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003149
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003150 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003151}
3152
3153TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3154 const int32_t USAGE_A = 0x070004;
3155 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003156 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3157 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003158 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3159 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3160 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003161
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003162 KeyboardInputMapper& mapper =
3163 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3164 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003165 // Initial metastate to AMETA_NONE.
3166 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3167 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168
3169 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003171 NotifyKeyArgs args;
3172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3173 ASSERT_EQ(DEVICE_ID, args.deviceId);
3174 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3175 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3176 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3177 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3178 ASSERT_EQ(KEY_HOME, args.scanCode);
3179 ASSERT_EQ(AMETA_NONE, args.metaState);
3180 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3181 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3182 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3183
3184 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003185 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3187 ASSERT_EQ(DEVICE_ID, args.deviceId);
3188 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3189 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3190 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3191 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3192 ASSERT_EQ(KEY_HOME, args.scanCode);
3193 ASSERT_EQ(AMETA_NONE, args.metaState);
3194 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3195 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3196 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3197
3198 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3200 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3202 ASSERT_EQ(DEVICE_ID, args.deviceId);
3203 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3204 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3205 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3206 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3207 ASSERT_EQ(0, args.scanCode);
3208 ASSERT_EQ(AMETA_NONE, args.metaState);
3209 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3210 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3211 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3212
3213 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003214 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3215 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3217 ASSERT_EQ(DEVICE_ID, args.deviceId);
3218 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3219 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3220 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3221 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3222 ASSERT_EQ(0, args.scanCode);
3223 ASSERT_EQ(AMETA_NONE, args.metaState);
3224 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3225 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3226 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3227
3228 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003229 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3230 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3232 ASSERT_EQ(DEVICE_ID, args.deviceId);
3233 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3234 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3235 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3236 ASSERT_EQ(0, args.keyCode);
3237 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3238 ASSERT_EQ(AMETA_NONE, args.metaState);
3239 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3240 ASSERT_EQ(0U, args.policyFlags);
3241 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3242
3243 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003244 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3245 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3247 ASSERT_EQ(DEVICE_ID, args.deviceId);
3248 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3249 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3250 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3251 ASSERT_EQ(0, args.keyCode);
3252 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3253 ASSERT_EQ(AMETA_NONE, args.metaState);
3254 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3255 ASSERT_EQ(0U, args.policyFlags);
3256 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3257}
3258
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003259/**
3260 * Ensure that the readTime is set to the time when the EV_KEY is received.
3261 */
3262TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3263 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3264
3265 KeyboardInputMapper& mapper =
3266 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3267 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3268 NotifyKeyArgs args;
3269
3270 // Key down
3271 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3273 ASSERT_EQ(12, args.readTime);
3274
3275 // Key up
3276 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3278 ASSERT_EQ(15, args.readTime);
3279}
3280
Michael Wrightd02c5b62014-02-10 15:10:22 -08003281TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003282 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3283 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003284 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3285 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3286 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003287
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003288 KeyboardInputMapper& mapper =
3289 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3290 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291
arthurhungc903df12020-08-11 15:08:42 +08003292 // Initial metastate to AMETA_NONE.
3293 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3294 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003295
3296 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003298 NotifyKeyArgs args;
3299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3300 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003301 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003302 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003303
3304 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003305 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3307 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003308 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003309
3310 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003311 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3313 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003314 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003315
3316 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003317 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3319 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003320 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003321 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003322}
3323
3324TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003325 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3326 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3327 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3328 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003329
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003330 KeyboardInputMapper& mapper =
3331 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3332 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003334 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003335 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3336 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3337 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3338 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3339 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3340 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3341 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3342 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3343}
3344
3345TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003346 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3347 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3348 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3349 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003350
Michael Wrightd02c5b62014-02-10 15:10:22 -08003351 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003352 KeyboardInputMapper& mapper =
3353 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3354 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003356 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003357 ASSERT_NO_FATAL_FAILURE(
3358 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3359 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3360 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3361 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3362 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3363 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3364 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003365
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003366 clearViewports();
3367 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003368 ASSERT_NO_FATAL_FAILURE(
3369 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3370 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3371 AKEYCODE_DPAD_UP, DISPLAY_ID));
3372 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3373 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3374 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3375 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003376
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003377 clearViewports();
3378 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003379 ASSERT_NO_FATAL_FAILURE(
3380 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3381 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3382 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3383 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3384 AKEYCODE_DPAD_UP, DISPLAY_ID));
3385 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3386 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003387
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003388 clearViewports();
3389 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003390 ASSERT_NO_FATAL_FAILURE(
3391 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3392 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3393 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3394 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3395 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3396 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3397 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003398
3399 // Special case: if orientation changes while key is down, we still emit the same keycode
3400 // in the key up as we did in the key down.
3401 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003402 clearViewports();
3403 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003404 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3406 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3407 ASSERT_EQ(KEY_UP, args.scanCode);
3408 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3409
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003410 clearViewports();
3411 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003412 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3414 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3415 ASSERT_EQ(KEY_UP, args.scanCode);
3416 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3417}
3418
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003419TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3420 // If the keyboard is not orientation aware,
3421 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003422 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003423
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003424 KeyboardInputMapper& mapper =
3425 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3426 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003427 NotifyKeyArgs args;
3428
3429 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003432 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3434 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3435
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003436 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003437 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3441 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3442}
3443
3444TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3445 // If the keyboard is orientation aware,
3446 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003447 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003448
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003449 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003450 KeyboardInputMapper& mapper =
3451 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3452 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003453 NotifyKeyArgs args;
3454
3455 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3456 // ^--- already checked by the previous test
3457
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003458 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003459 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003460 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003462 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3464 ASSERT_EQ(DISPLAY_ID, args.displayId);
3465
3466 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003467 clearViewports();
3468 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003469 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003470 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003472 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3474 ASSERT_EQ(newDisplayId, args.displayId);
3475}
3476
Michael Wrightd02c5b62014-02-10 15:10:22 -08003477TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003478 KeyboardInputMapper& mapper =
3479 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3480 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003481
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003482 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003483 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003484
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003485 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003486 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003487}
3488
3489TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003490 KeyboardInputMapper& mapper =
3491 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3492 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003493
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003494 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003495 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003496
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003497 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003498 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003499}
3500
3501TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003502 KeyboardInputMapper& mapper =
3503 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3504 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003505
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003506 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003507
3508 const int32_t keyCodes[2] = { AKEYCODE_A, AKEYCODE_B };
3509 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003510 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 1, keyCodes, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511 ASSERT_TRUE(flags[0]);
3512 ASSERT_FALSE(flags[1]);
3513}
3514
3515TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003516 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3517 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3518 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3519 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3520 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3521 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003523 KeyboardInputMapper& mapper =
3524 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3525 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Chris Yea52ade12020-08-27 16:49:20 -07003526 // Initialize metastate to AMETA_NUM_LOCK_ON.
arthurhungc903df12020-08-11 15:08:42 +08003527 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3528 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003529
3530 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003531 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3532 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3533 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003534
3535 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003536 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3537 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003538 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3539 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3540 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003541 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003542
3543 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003546 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3547 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3548 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003549 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003550
3551 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003552 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3553 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003554 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3555 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3556 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003557 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003558
3559 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003560 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3561 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003562 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3563 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3564 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003565 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003566
3567 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003568 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003570 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3571 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3572 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003573 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003574
3575 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003576 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003578 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3579 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3580 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003581 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003582}
3583
Chris Yea52ade12020-08-27 16:49:20 -07003584TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3585 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3586 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3587 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3588 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3589
3590 KeyboardInputMapper& mapper =
3591 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3592 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3593
3594 // Initial metastate should be AMETA_NONE as no meta keys added.
3595 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3596 // Meta state should be AMETA_NONE after reset
3597 mapper.reset(ARBITRARY_TIME);
3598 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3599 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3600 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3601 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3602
3603 NotifyKeyArgs args;
3604 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003605 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3607 ASSERT_EQ(AMETA_NONE, args.metaState);
3608 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3609 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3610 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3611
3612 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003613 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3615 ASSERT_EQ(AMETA_NONE, args.metaState);
3616 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3617 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3618 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3619}
3620
Arthur Hung2c9a3342019-07-23 14:18:59 +08003621TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3622 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003623 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3624 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3625 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3626 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003627
3628 // keyboard 2.
3629 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003630 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003631 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003632 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003633 std::shared_ptr<InputDevice> device2 =
3634 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3635 Flags<InputDeviceClass>(0));
3636
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003637 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3638 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3639 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3640 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003641
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003642 KeyboardInputMapper& mapper =
3643 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3644 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003645
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003646 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003647 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003648 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003649 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3650 device2->reset(ARBITRARY_TIME);
3651
3652 // Prepared displays and associated info.
3653 constexpr uint8_t hdmi1 = 0;
3654 constexpr uint8_t hdmi2 = 1;
3655 const std::string SECONDARY_UNIQUE_ID = "local:1";
3656
3657 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3658 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3659
3660 // No associated display viewport found, should disable the device.
3661 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3662 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3663 ASSERT_FALSE(device2->isEnabled());
3664
3665 // Prepare second display.
3666 constexpr int32_t newDisplayId = 2;
3667 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003668 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003669 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003670 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003671 // Default device will reconfigure above, need additional reconfiguration for another device.
3672 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3673 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3674
3675 // Device should be enabled after the associated display is found.
3676 ASSERT_TRUE(mDevice->isEnabled());
3677 ASSERT_TRUE(device2->isEnabled());
3678
3679 // Test pad key events
3680 ASSERT_NO_FATAL_FAILURE(
3681 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3682 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3683 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3684 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3685 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3686 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3687 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3688
3689 ASSERT_NO_FATAL_FAILURE(
3690 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3691 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3692 AKEYCODE_DPAD_RIGHT, newDisplayId));
3693 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3694 AKEYCODE_DPAD_DOWN, newDisplayId));
3695 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3696 AKEYCODE_DPAD_LEFT, newDisplayId));
3697}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003698
arthurhungc903df12020-08-11 15:08:42 +08003699TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3700 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3701 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3702 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3703 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3704 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3705 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3706
3707 KeyboardInputMapper& mapper =
3708 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3709 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3710 // Initial metastate to AMETA_NONE.
3711 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3712 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3713
3714 // Initialization should have turned all of the lights off.
3715 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3716 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3717 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3718
3719 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003720 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3721 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003722 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3723 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3724
3725 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003726 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3727 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003728 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3729 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3730
3731 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003732 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3733 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003734 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3735 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3736
3737 mFakeEventHub->removeDevice(EVENTHUB_ID);
3738 mReader->loopOnce();
3739
3740 // keyboard 2 should default toggle keys.
3741 const std::string USB2 = "USB2";
3742 const std::string DEVICE_NAME2 = "KEYBOARD2";
3743 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3744 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3745 std::shared_ptr<InputDevice> device2 =
3746 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3747 Flags<InputDeviceClass>(0));
3748 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3749 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3750 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3751 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3752 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3753 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3754
arthurhung6fe95782020-10-05 22:41:16 +08003755 KeyboardInputMapper& mapper2 =
3756 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3757 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
arthurhungc903df12020-08-11 15:08:42 +08003758 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
3759 device2->reset(ARBITRARY_TIME);
3760
3761 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3762 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3763 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003764 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3765 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003766}
3767
Arthur Hungcb40a002021-08-03 14:31:01 +00003768TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3769 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3770 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3771 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3772
3773 // Suppose we have two mappers. (DPAD + KEYBOARD)
3774 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3775 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3776 KeyboardInputMapper& mapper =
3777 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3778 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3779 // Initialize metastate to AMETA_NUM_LOCK_ON.
3780 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
3781 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3782
3783 mReader->toggleCapsLockState(DEVICE_ID);
3784 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3785}
3786
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003787// --- KeyboardInputMapperTest_ExternalDevice ---
3788
3789class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3790protected:
Chris Yea52ade12020-08-27 16:49:20 -07003791 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003792};
3793
3794TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003795 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3796 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003797
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003798 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3799 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3800 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3801 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003802
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003803 KeyboardInputMapper& mapper =
3804 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3805 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003806
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003808 NotifyKeyArgs args;
3809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3810 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3811
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003812 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3814 ASSERT_EQ(uint32_t(0), args.policyFlags);
3815
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3818 ASSERT_EQ(uint32_t(0), args.policyFlags);
3819
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003820 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3822 ASSERT_EQ(uint32_t(0), args.policyFlags);
3823
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3826 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3827
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003828 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3830 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3831}
3832
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003833TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003834 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003835
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003836 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3837 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3838 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003839
Powei Fengd041c5d2019-05-03 17:11:33 -07003840 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003841 KeyboardInputMapper& mapper =
3842 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3843 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003844
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003845 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003846 NotifyKeyArgs args;
3847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3848 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3849
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003850 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3852 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3853
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003854 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3856 ASSERT_EQ(uint32_t(0), args.policyFlags);
3857
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003858 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3860 ASSERT_EQ(uint32_t(0), args.policyFlags);
3861
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003862 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3864 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3865
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003866 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3868 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3869}
3870
Michael Wrightd02c5b62014-02-10 15:10:22 -08003871// --- CursorInputMapperTest ---
3872
3873class CursorInputMapperTest : public InputMapperTest {
3874protected:
3875 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3876
Michael Wright17db18e2020-06-26 20:51:44 +01003877 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003878
Chris Yea52ade12020-08-27 16:49:20 -07003879 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003880 InputMapperTest::SetUp();
3881
Michael Wright17db18e2020-06-26 20:51:44 +01003882 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003883 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003884 }
3885
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003886 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3887 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003888
3889 void prepareDisplay(int32_t orientation) {
3890 const std::string uniqueId = "local:0";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003891 const ViewportType viewportType = ViewportType::INTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003892 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3893 orientation, uniqueId, NO_PORT, viewportType);
3894 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003895
3896 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3897 float pressure) {
3898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3899 0.0f, 0.0f, 0.0f, EPSILON));
3900 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003901};
3902
3903const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3904
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003905void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3906 int32_t originalY, int32_t rotatedX,
3907 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003908 NotifyMotionArgs args;
3909
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003910 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3911 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3912 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3914 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003915 ASSERT_NO_FATAL_FAILURE(
3916 assertCursorPointerCoords(args.pointerCoords[0],
3917 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3918 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003919}
3920
3921TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003922 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003923 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003924
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003925 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003926}
3927
3928TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003929 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003930 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003931
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003932 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003933}
3934
3935TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003936 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003937 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003938
3939 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003940 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003941
3942 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003943 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3944 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003945 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3946 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3947
3948 // When the bounds are set, then there should be a valid motion range.
3949 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3950
3951 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003952 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003953
3954 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3955 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3956 1, 800 - 1, 0.0f, 0.0f));
3957 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3958 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3959 2, 480 - 1, 0.0f, 0.0f));
3960 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3961 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3962 0.0f, 1.0f, 0.0f, 0.0f));
3963}
3964
3965TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003966 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003967 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003968
3969 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003970 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003971
3972 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3973 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3974 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3975 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3976 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3977 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3978 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3979 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3980 0.0f, 1.0f, 0.0f, 0.0f));
3981}
3982
3983TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003984 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003985 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003986
arthurhungdcef2dc2020-08-11 14:47:50 +08003987 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003988
3989 NotifyMotionArgs args;
3990
3991 // Button press.
3992 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3996 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3997 ASSERT_EQ(DEVICE_ID, args.deviceId);
3998 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3999 ASSERT_EQ(uint32_t(0), args.policyFlags);
4000 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4001 ASSERT_EQ(0, args.flags);
4002 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4003 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4004 ASSERT_EQ(0, args.edgeFlags);
4005 ASSERT_EQ(uint32_t(1), args.pointerCount);
4006 ASSERT_EQ(0, args.pointerProperties[0].id);
4007 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004008 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004009 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4010 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4011 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4012
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4014 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4015 ASSERT_EQ(DEVICE_ID, args.deviceId);
4016 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4017 ASSERT_EQ(uint32_t(0), args.policyFlags);
4018 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4019 ASSERT_EQ(0, args.flags);
4020 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4021 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4022 ASSERT_EQ(0, args.edgeFlags);
4023 ASSERT_EQ(uint32_t(1), args.pointerCount);
4024 ASSERT_EQ(0, args.pointerProperties[0].id);
4025 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004026 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004027 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4028 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4029 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4030
Michael Wrightd02c5b62014-02-10 15:10:22 -08004031 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004032 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4033 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4035 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4036 ASSERT_EQ(DEVICE_ID, args.deviceId);
4037 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4038 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004039 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4040 ASSERT_EQ(0, args.flags);
4041 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4042 ASSERT_EQ(0, args.buttonState);
4043 ASSERT_EQ(0, args.edgeFlags);
4044 ASSERT_EQ(uint32_t(1), args.pointerCount);
4045 ASSERT_EQ(0, args.pointerProperties[0].id);
4046 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004047 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004048 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4049 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4050 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4051
4052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4053 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4054 ASSERT_EQ(DEVICE_ID, args.deviceId);
4055 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4056 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004057 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4058 ASSERT_EQ(0, args.flags);
4059 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4060 ASSERT_EQ(0, args.buttonState);
4061 ASSERT_EQ(0, args.edgeFlags);
4062 ASSERT_EQ(uint32_t(1), args.pointerCount);
4063 ASSERT_EQ(0, args.pointerProperties[0].id);
4064 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004065 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004066 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4067 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4068 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4069}
4070
4071TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004073 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004074
4075 NotifyMotionArgs args;
4076
4077 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004078 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4081 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004082 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4083 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4084 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004085
4086 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004087 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4088 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4090 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004091 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4092 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004093}
4094
4095TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004096 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004097 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004098
4099 NotifyMotionArgs args;
4100
4101 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004102 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4103 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4105 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004106 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4109 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004110 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004111
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004113 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4114 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004116 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004117 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004118
4119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004120 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004121 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004122}
4123
4124TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004126 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004127
4128 NotifyMotionArgs args;
4129
4130 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004131 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4132 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4134 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4136 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004137 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4138 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4139 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4142 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004143 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4144 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4145 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004146
Michael Wrightd02c5b62014-02-10 15:10:22 -08004147 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4150 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004153 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4154 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4155 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004156
4157 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4159 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004161 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004162 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004163
4164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004166 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004167}
4168
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004169TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004171 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4172 // need to be rotated.
4173 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004174 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004175
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004176 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4178 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4179 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4180 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4181 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4182 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4183 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4184 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4185}
4186
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004187TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004188 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004189 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4190 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004191 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004192
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004193 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004194 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4195 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4196 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4197 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4198 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4199 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4200 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4201 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4202
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004203 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004204 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4205 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4206 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4207 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4208 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4209 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4210 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4211 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004213 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4215 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4216 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4217 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4218 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4219 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4220 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4221 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4222
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004223 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004224 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4225 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4226 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4227 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4228 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4229 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4230 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4231 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232}
4233
4234TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004236 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237
4238 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4239 mFakePointerController->setPosition(100, 200);
4240 mFakePointerController->setButtonState(0);
4241
4242 NotifyMotionArgs motionArgs;
4243 NotifyKeyArgs keyArgs;
4244
4245 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004246 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4249 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4250 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4251 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004252 ASSERT_NO_FATAL_FAILURE(
4253 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4256 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4257 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4258 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004259 ASSERT_NO_FATAL_FAILURE(
4260 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004261
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004262 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004265 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266 ASSERT_EQ(0, motionArgs.buttonState);
4267 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004268 ASSERT_NO_FATAL_FAILURE(
4269 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270
4271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004272 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273 ASSERT_EQ(0, motionArgs.buttonState);
4274 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004275 ASSERT_NO_FATAL_FAILURE(
4276 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004277
4278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004280 ASSERT_EQ(0, motionArgs.buttonState);
4281 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004282 ASSERT_NO_FATAL_FAILURE(
4283 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284
4285 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004286 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4288 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4290 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4291 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4292 motionArgs.buttonState);
4293 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4294 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004295 ASSERT_NO_FATAL_FAILURE(
4296 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004297
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4299 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4300 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4301 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4302 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004303 ASSERT_NO_FATAL_FAILURE(
4304 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004305
4306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4307 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4308 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4309 motionArgs.buttonState);
4310 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4311 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004312 ASSERT_NO_FATAL_FAILURE(
4313 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004314
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004315 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004318 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004319 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4320 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004321 ASSERT_NO_FATAL_FAILURE(
4322 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004323
4324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004326 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4327 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004328 ASSERT_NO_FATAL_FAILURE(
4329 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004334 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4335 ASSERT_EQ(0, motionArgs.buttonState);
4336 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004337 ASSERT_NO_FATAL_FAILURE(
4338 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004341
4342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004343 ASSERT_EQ(0, motionArgs.buttonState);
4344 ASSERT_EQ(0, mFakePointerController->getButtonState());
4345 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004346 ASSERT_NO_FATAL_FAILURE(
4347 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004348
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4350 ASSERT_EQ(0, motionArgs.buttonState);
4351 ASSERT_EQ(0, mFakePointerController->getButtonState());
4352 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004353 ASSERT_NO_FATAL_FAILURE(
4354 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004355
4356 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004357 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4360 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4361 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004362
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004364 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4366 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004367 ASSERT_NO_FATAL_FAILURE(
4368 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004369
4370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4371 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4372 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4373 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004374 ASSERT_NO_FATAL_FAILURE(
4375 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004380 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381 ASSERT_EQ(0, motionArgs.buttonState);
4382 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004383 ASSERT_NO_FATAL_FAILURE(
4384 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004385
4386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004387 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004388 ASSERT_EQ(0, motionArgs.buttonState);
4389 ASSERT_EQ(0, mFakePointerController->getButtonState());
4390
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004391 ASSERT_NO_FATAL_FAILURE(
4392 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4394 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4395 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4396
4397 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004398 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4401 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4402 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004403
Michael Wrightd02c5b62014-02-10 15:10:22 -08004404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004405 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4407 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004408 ASSERT_NO_FATAL_FAILURE(
4409 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004410
4411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4412 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4413 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4414 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004415 ASSERT_NO_FATAL_FAILURE(
4416 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004417
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004421 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422 ASSERT_EQ(0, motionArgs.buttonState);
4423 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004424 ASSERT_NO_FATAL_FAILURE(
4425 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004426
4427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4428 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4429 ASSERT_EQ(0, motionArgs.buttonState);
4430 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004431 ASSERT_NO_FATAL_FAILURE(
4432 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004433
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4435 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4436 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4437
4438 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4442 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4443 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004444
Michael Wrightd02c5b62014-02-10 15:10:22 -08004445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004446 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4448 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004449 ASSERT_NO_FATAL_FAILURE(
4450 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004451
4452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4453 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4454 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4455 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004456 ASSERT_NO_FATAL_FAILURE(
4457 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004458
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4460 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004462 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463 ASSERT_EQ(0, motionArgs.buttonState);
4464 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004465 ASSERT_NO_FATAL_FAILURE(
4466 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004467
4468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4469 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4470 ASSERT_EQ(0, motionArgs.buttonState);
4471 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004472 ASSERT_NO_FATAL_FAILURE(
4473 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004474
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4476 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4477 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4478
4479 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004480 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4483 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4484 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004485
Michael Wrightd02c5b62014-02-10 15:10:22 -08004486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004487 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4489 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004490 ASSERT_NO_FATAL_FAILURE(
4491 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004492
4493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4494 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4495 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4496 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004497 ASSERT_NO_FATAL_FAILURE(
4498 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004503 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004504 ASSERT_EQ(0, motionArgs.buttonState);
4505 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004506 ASSERT_NO_FATAL_FAILURE(
4507 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004508
4509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4510 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4511 ASSERT_EQ(0, motionArgs.buttonState);
4512 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004513 ASSERT_NO_FATAL_FAILURE(
4514 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004515
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4517 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4518 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4519}
4520
4521TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004523 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004524
4525 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4526 mFakePointerController->setPosition(100, 200);
4527 mFakePointerController->setButtonState(0);
4528
4529 NotifyMotionArgs args;
4530
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004531 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4532 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4533 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004535 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4536 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4538 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 +01004539 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004540}
4541
4542TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004543 addConfigurationProperty("cursor.mode", "pointer");
4544 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004545 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004546
4547 NotifyDeviceResetArgs resetArgs;
4548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4549 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4550 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4551
4552 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4553 mFakePointerController->setPosition(100, 200);
4554 mFakePointerController->setButtonState(0);
4555
4556 NotifyMotionArgs args;
4557
4558 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004559 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4560 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4561 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4563 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4564 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4566 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 +01004567 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004568
4569 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004570 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4573 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4574 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4576 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4578 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4579 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4581 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4582
4583 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004584 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4585 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4587 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4588 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4590 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4592 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4593 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4595 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4596
4597 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004598 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4599 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4600 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4602 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4603 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4605 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 +01004606 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004607
4608 // Disable pointer capture and check that the device generation got bumped
4609 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004610 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004611 mFakePolicy->setPointerCapture(false);
4612 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004613 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004614
4615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4616 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4617 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4618
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004619 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4620 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4621 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4623 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004624 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4626 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 +01004627 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004628}
4629
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004630TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004631 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004632
Garfield Tan888a6a42020-01-09 11:39:16 -08004633 // Setup for second display.
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004634 constexpr int32_t SECOND_DISPLAY_ID = 1;
Garfield Tan888a6a42020-01-09 11:39:16 -08004635 const std::string SECOND_DISPLAY_UNIQUE_ID = "local:1";
4636 mFakePolicy->addDisplayViewport(SECOND_DISPLAY_ID, 800, 480, DISPLAY_ORIENTATION_0,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00004637 true /*isActive*/, SECOND_DISPLAY_UNIQUE_ID, NO_PORT,
4638 ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08004639 mFakePolicy->setDefaultPointerDisplayId(SECOND_DISPLAY_ID);
4640 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4641
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004642 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4643 mFakePointerController->setPosition(100, 200);
4644 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004645
4646 NotifyMotionArgs args;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4648 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4649 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4651 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4652 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4654 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 +01004655 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004656 ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId);
4657}
4658
Michael Wrightd02c5b62014-02-10 15:10:22 -08004659// --- TouchInputMapperTest ---
4660
4661class TouchInputMapperTest : public InputMapperTest {
4662protected:
4663 static const int32_t RAW_X_MIN;
4664 static const int32_t RAW_X_MAX;
4665 static const int32_t RAW_Y_MIN;
4666 static const int32_t RAW_Y_MAX;
4667 static const int32_t RAW_TOUCH_MIN;
4668 static const int32_t RAW_TOUCH_MAX;
4669 static const int32_t RAW_TOOL_MIN;
4670 static const int32_t RAW_TOOL_MAX;
4671 static const int32_t RAW_PRESSURE_MIN;
4672 static const int32_t RAW_PRESSURE_MAX;
4673 static const int32_t RAW_ORIENTATION_MIN;
4674 static const int32_t RAW_ORIENTATION_MAX;
4675 static const int32_t RAW_DISTANCE_MIN;
4676 static const int32_t RAW_DISTANCE_MAX;
4677 static const int32_t RAW_TILT_MIN;
4678 static const int32_t RAW_TILT_MAX;
4679 static const int32_t RAW_ID_MIN;
4680 static const int32_t RAW_ID_MAX;
4681 static const int32_t RAW_SLOT_MIN;
4682 static const int32_t RAW_SLOT_MAX;
4683 static const float X_PRECISION;
4684 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004685 static const float X_PRECISION_VIRTUAL;
4686 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687
4688 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004689 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004690
4691 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4692
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004693 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004694 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004695
Michael Wrightd02c5b62014-02-10 15:10:22 -08004696 enum Axes {
4697 POSITION = 1 << 0,
4698 TOUCH = 1 << 1,
4699 TOOL = 1 << 2,
4700 PRESSURE = 1 << 3,
4701 ORIENTATION = 1 << 4,
4702 MINOR = 1 << 5,
4703 ID = 1 << 6,
4704 DISTANCE = 1 << 7,
4705 TILT = 1 << 8,
4706 SLOT = 1 << 9,
4707 TOOL_TYPE = 1 << 10,
4708 };
4709
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004710 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4711 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004712 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004713 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004714 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004715 int32_t toRawX(float displayX);
4716 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004717 int32_t toRotatedRawX(float displayX);
4718 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004719 float toCookedX(float rawX, float rawY);
4720 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004721 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004722 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004723 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004724 float toDisplayY(int32_t rawY, int32_t displayHeight);
4725
Michael Wrightd02c5b62014-02-10 15:10:22 -08004726};
4727
4728const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4729const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4730const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4731const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4732const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4733const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4734const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4735const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004736const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4737const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004738const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4739const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4740const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4741const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4742const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4743const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4744const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4745const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4746const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4747const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4748const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4749const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004750const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4751 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4752const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4753 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004754const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4755 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004756
4757const float TouchInputMapperTest::GEOMETRIC_SCALE =
4758 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4759 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4760
4761const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4762 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4763 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4764};
4765
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004766void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004767 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4768 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004769}
4770
4771void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4772 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4773 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004774}
4775
Santos Cordonfa5cf462017-04-05 10:37:00 -07004776void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004777 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4778 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4779 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004780}
4781
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004783 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4784 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4785 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4786 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004787}
4788
Jason Gerecke489fda82012-09-07 17:19:40 -07004789void TouchInputMapperTest::prepareLocationCalibration() {
4790 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4791}
4792
Michael Wrightd02c5b62014-02-10 15:10:22 -08004793int32_t TouchInputMapperTest::toRawX(float displayX) {
4794 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4795}
4796
4797int32_t TouchInputMapperTest::toRawY(float displayY) {
4798 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4799}
4800
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004801int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4802 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4803}
4804
4805int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4806 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4807}
4808
Jason Gerecke489fda82012-09-07 17:19:40 -07004809float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4810 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4811 return rawX;
4812}
4813
4814float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4815 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4816 return rawY;
4817}
4818
Michael Wrightd02c5b62014-02-10 15:10:22 -08004819float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004820 return toDisplayX(rawX, DISPLAY_WIDTH);
4821}
4822
4823float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4824 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004825}
4826
4827float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004828 return toDisplayY(rawY, DISPLAY_HEIGHT);
4829}
4830
4831float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4832 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833}
4834
4835
4836// --- SingleTouchInputMapperTest ---
4837
4838class SingleTouchInputMapperTest : public TouchInputMapperTest {
4839protected:
4840 void prepareButtons();
4841 void prepareAxes(int axes);
4842
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004843 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4844 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4845 void processUp(SingleTouchInputMapper& mappery);
4846 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4847 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4848 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4849 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4850 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4851 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004852};
4853
4854void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004855 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004856}
4857
4858void SingleTouchInputMapperTest::prepareAxes(int axes) {
4859 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004860 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4861 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004862 }
4863 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004864 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4865 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004866 }
4867 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004868 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4869 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004870 }
4871 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004872 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4873 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004874 }
4875 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004876 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4877 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004878 }
4879}
4880
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004881void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004882 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4883 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4884 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004885}
4886
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004887void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004888 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004890}
4891
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004892void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004894}
4895
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004896void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004897 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004898}
4899
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004900void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4901 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004902 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004903}
4904
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004905void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004906 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004907}
4908
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004909void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4910 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004911 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4912 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004913}
4914
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004915void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4916 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004917 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004918}
4919
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004920void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004921 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004922}
4923
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004925 prepareButtons();
4926 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004927 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004928
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004929 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930}
4931
4932TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndIsACursor_ReturnsTouchPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004933 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_X);
4934 mFakeEventHub->addRelativeAxis(EVENTHUB_ID, REL_Y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935 prepareButtons();
4936 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004937 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004939 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004940}
4941
4942TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchPad_ReturnsTouchPad) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943 prepareButtons();
4944 prepareAxes(POSITION);
4945 addConfigurationProperty("touch.deviceType", "touchPad");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004946 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004947
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004948 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004949}
4950
4951TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004952 prepareButtons();
4953 prepareAxes(POSITION);
4954 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004955 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004956
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004957 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004958}
4959
4960TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004961 addConfigurationProperty("touch.deviceType", "touchScreen");
4962 prepareDisplay(DISPLAY_ORIENTATION_0);
4963 prepareButtons();
4964 prepareAxes(POSITION);
4965 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004966 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004967
4968 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004969 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004970
4971 // Virtual key is down.
4972 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4973 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4974 processDown(mapper, x, y);
4975 processSync(mapper);
4976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4977
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004978 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004979
4980 // Virtual key is up.
4981 processUp(mapper);
4982 processSync(mapper);
4983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4984
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004985 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004986}
4987
4988TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004989 addConfigurationProperty("touch.deviceType", "touchScreen");
4990 prepareDisplay(DISPLAY_ORIENTATION_0);
4991 prepareButtons();
4992 prepareAxes(POSITION);
4993 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004994 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995
4996 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004997 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004998
4999 // Virtual key is down.
5000 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5001 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5002 processDown(mapper, x, y);
5003 processSync(mapper);
5004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5005
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005006 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005007
5008 // Virtual key is up.
5009 processUp(mapper);
5010 processSync(mapper);
5011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5012
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005013 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005014}
5015
5016TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005017 addConfigurationProperty("touch.deviceType", "touchScreen");
5018 prepareDisplay(DISPLAY_ORIENTATION_0);
5019 prepareButtons();
5020 prepareAxes(POSITION);
5021 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005022 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023
5024 const int32_t keys[2] = { AKEYCODE_HOME, AKEYCODE_A };
5025 uint8_t flags[2] = { 0, 0 };
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005026 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, 2, keys, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005027 ASSERT_TRUE(flags[0]);
5028 ASSERT_FALSE(flags[1]);
5029}
5030
5031TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005032 addConfigurationProperty("touch.deviceType", "touchScreen");
5033 prepareDisplay(DISPLAY_ORIENTATION_0);
5034 prepareButtons();
5035 prepareAxes(POSITION);
5036 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005037 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005038
arthurhungdcef2dc2020-08-11 14:47:50 +08005039 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005040
5041 NotifyKeyArgs args;
5042
5043 // Press virtual key.
5044 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5045 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5046 processDown(mapper, x, y);
5047 processSync(mapper);
5048
5049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5050 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5051 ASSERT_EQ(DEVICE_ID, args.deviceId);
5052 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5053 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5054 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5055 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5056 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5057 ASSERT_EQ(KEY_HOME, args.scanCode);
5058 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5059 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5060
5061 // Release virtual key.
5062 processUp(mapper);
5063 processSync(mapper);
5064
5065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5066 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5067 ASSERT_EQ(DEVICE_ID, args.deviceId);
5068 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5069 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5070 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5071 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5072 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5073 ASSERT_EQ(KEY_HOME, args.scanCode);
5074 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5075 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5076
5077 // Should not have sent any motions.
5078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5079}
5080
5081TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005082 addConfigurationProperty("touch.deviceType", "touchScreen");
5083 prepareDisplay(DISPLAY_ORIENTATION_0);
5084 prepareButtons();
5085 prepareAxes(POSITION);
5086 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005087 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005088
arthurhungdcef2dc2020-08-11 14:47:50 +08005089 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005090
5091 NotifyKeyArgs keyArgs;
5092
5093 // Press virtual key.
5094 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5095 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5096 processDown(mapper, x, y);
5097 processSync(mapper);
5098
5099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5100 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5101 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5102 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5103 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5104 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5105 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5106 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5107 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5108 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5109 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5110
5111 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5112 // into the display area.
5113 y -= 100;
5114 processMove(mapper, x, y);
5115 processSync(mapper);
5116
5117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5118 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5119 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5120 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5121 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5122 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5123 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5124 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5125 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5126 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5127 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5128 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5129
5130 NotifyMotionArgs motionArgs;
5131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5132 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5133 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5134 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5135 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5136 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5137 ASSERT_EQ(0, motionArgs.flags);
5138 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5139 ASSERT_EQ(0, motionArgs.buttonState);
5140 ASSERT_EQ(0, motionArgs.edgeFlags);
5141 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5142 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5143 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5144 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5145 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5146 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5147 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5148 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5149
5150 // Keep moving out of bounds. Should generate a pointer move.
5151 y -= 50;
5152 processMove(mapper, x, y);
5153 processSync(mapper);
5154
5155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5156 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5157 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5158 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5159 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5160 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5161 ASSERT_EQ(0, motionArgs.flags);
5162 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5163 ASSERT_EQ(0, motionArgs.buttonState);
5164 ASSERT_EQ(0, motionArgs.edgeFlags);
5165 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5166 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5169 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5170 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5171 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5172 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5173
5174 // Release out of bounds. Should generate a pointer up.
5175 processUp(mapper);
5176 processSync(mapper);
5177
5178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5179 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5180 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5181 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5182 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5183 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5184 ASSERT_EQ(0, motionArgs.flags);
5185 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5186 ASSERT_EQ(0, motionArgs.buttonState);
5187 ASSERT_EQ(0, motionArgs.edgeFlags);
5188 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5189 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5190 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5191 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5192 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5193 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5194 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5195 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5196
5197 // Should not have sent any more keys or motions.
5198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5200}
5201
5202TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005203 addConfigurationProperty("touch.deviceType", "touchScreen");
5204 prepareDisplay(DISPLAY_ORIENTATION_0);
5205 prepareButtons();
5206 prepareAxes(POSITION);
5207 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005208 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005209
arthurhungdcef2dc2020-08-11 14:47:50 +08005210 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005211
5212 NotifyMotionArgs motionArgs;
5213
5214 // Initially go down out of bounds.
5215 int32_t x = -10;
5216 int32_t y = -10;
5217 processDown(mapper, x, y);
5218 processSync(mapper);
5219
5220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5221
5222 // Move into the display area. Should generate a pointer down.
5223 x = 50;
5224 y = 75;
5225 processMove(mapper, x, y);
5226 processSync(mapper);
5227
5228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5229 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5230 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5231 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5232 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5233 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5234 ASSERT_EQ(0, motionArgs.flags);
5235 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5236 ASSERT_EQ(0, motionArgs.buttonState);
5237 ASSERT_EQ(0, motionArgs.edgeFlags);
5238 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5239 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5240 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5242 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5243 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5244 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5245 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5246
5247 // Release. Should generate a pointer up.
5248 processUp(mapper);
5249 processSync(mapper);
5250
5251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5252 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5253 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5254 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5255 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5256 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5257 ASSERT_EQ(0, motionArgs.flags);
5258 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5259 ASSERT_EQ(0, motionArgs.buttonState);
5260 ASSERT_EQ(0, motionArgs.edgeFlags);
5261 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5262 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5263 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5265 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5266 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5267 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5268 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5269
5270 // Should not have sent any more keys or motions.
5271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5273}
5274
Santos Cordonfa5cf462017-04-05 10:37:00 -07005275TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005276 addConfigurationProperty("touch.deviceType", "touchScreen");
5277 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5278
5279 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5280 prepareButtons();
5281 prepareAxes(POSITION);
5282 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005283 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005284
arthurhungdcef2dc2020-08-11 14:47:50 +08005285 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005286
5287 NotifyMotionArgs motionArgs;
5288
5289 // Down.
5290 int32_t x = 100;
5291 int32_t y = 125;
5292 processDown(mapper, x, y);
5293 processSync(mapper);
5294
5295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5296 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5297 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5298 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5299 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5300 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5301 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5302 ASSERT_EQ(0, motionArgs.flags);
5303 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5304 ASSERT_EQ(0, motionArgs.buttonState);
5305 ASSERT_EQ(0, motionArgs.edgeFlags);
5306 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5307 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5308 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5310 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5311 1, 0, 0, 0, 0, 0, 0, 0));
5312 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5313 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5314 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5315
5316 // Move.
5317 x += 50;
5318 y += 75;
5319 processMove(mapper, x, y);
5320 processSync(mapper);
5321
5322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5323 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5324 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5325 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5326 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5327 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5329 ASSERT_EQ(0, motionArgs.flags);
5330 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5331 ASSERT_EQ(0, motionArgs.buttonState);
5332 ASSERT_EQ(0, motionArgs.edgeFlags);
5333 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5334 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5335 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5337 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5338 1, 0, 0, 0, 0, 0, 0, 0));
5339 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5340 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5341 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5342
5343 // Up.
5344 processUp(mapper);
5345 processSync(mapper);
5346
5347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5348 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5349 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5350 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5351 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5352 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5353 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5354 ASSERT_EQ(0, motionArgs.flags);
5355 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5356 ASSERT_EQ(0, motionArgs.buttonState);
5357 ASSERT_EQ(0, motionArgs.edgeFlags);
5358 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5359 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5360 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5362 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5363 1, 0, 0, 0, 0, 0, 0, 0));
5364 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5365 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5366 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5367
5368 // Should not have sent any more keys or motions.
5369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5371}
5372
Michael Wrightd02c5b62014-02-10 15:10:22 -08005373TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374 addConfigurationProperty("touch.deviceType", "touchScreen");
5375 prepareDisplay(DISPLAY_ORIENTATION_0);
5376 prepareButtons();
5377 prepareAxes(POSITION);
5378 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005379 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005380
arthurhungdcef2dc2020-08-11 14:47:50 +08005381 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005382
5383 NotifyMotionArgs motionArgs;
5384
5385 // Down.
5386 int32_t x = 100;
5387 int32_t y = 125;
5388 processDown(mapper, x, y);
5389 processSync(mapper);
5390
5391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5392 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5393 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5394 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5395 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5396 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5397 ASSERT_EQ(0, motionArgs.flags);
5398 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5399 ASSERT_EQ(0, motionArgs.buttonState);
5400 ASSERT_EQ(0, motionArgs.edgeFlags);
5401 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5402 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5403 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5405 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5406 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5407 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5408 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5409
5410 // Move.
5411 x += 50;
5412 y += 75;
5413 processMove(mapper, x, y);
5414 processSync(mapper);
5415
5416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5417 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5418 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5419 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5420 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5421 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5422 ASSERT_EQ(0, motionArgs.flags);
5423 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5424 ASSERT_EQ(0, motionArgs.buttonState);
5425 ASSERT_EQ(0, motionArgs.edgeFlags);
5426 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5427 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5428 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5429 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5430 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5431 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5432 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5433 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5434
5435 // Up.
5436 processUp(mapper);
5437 processSync(mapper);
5438
5439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5440 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5441 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5442 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5443 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5444 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5445 ASSERT_EQ(0, motionArgs.flags);
5446 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5447 ASSERT_EQ(0, motionArgs.buttonState);
5448 ASSERT_EQ(0, motionArgs.edgeFlags);
5449 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5450 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5451 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5453 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5454 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5455 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5456 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5457
5458 // Should not have sent any more keys or motions.
5459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5461}
5462
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005463TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005464 addConfigurationProperty("touch.deviceType", "touchScreen");
5465 prepareButtons();
5466 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005467 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5468 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005469 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005470
5471 NotifyMotionArgs args;
5472
5473 // Rotation 90.
5474 prepareDisplay(DISPLAY_ORIENTATION_90);
5475 processDown(mapper, toRawX(50), toRawY(75));
5476 processSync(mapper);
5477
5478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5479 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5480 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5481
5482 processUp(mapper);
5483 processSync(mapper);
5484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5485}
5486
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005487TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005488 addConfigurationProperty("touch.deviceType", "touchScreen");
5489 prepareButtons();
5490 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005491 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5492 // orientation-aware are affected by display rotation.
5493 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005494 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005495
5496 NotifyMotionArgs args;
5497
5498 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005499 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005500 prepareDisplay(DISPLAY_ORIENTATION_0);
5501 processDown(mapper, toRawX(50), toRawY(75));
5502 processSync(mapper);
5503
5504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5505 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5506 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5507
5508 processUp(mapper);
5509 processSync(mapper);
5510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5511
5512 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005513 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005514 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005515 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005516 processSync(mapper);
5517
5518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5519 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5520 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5521
5522 processUp(mapper);
5523 processSync(mapper);
5524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5525
5526 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005527 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005528 prepareDisplay(DISPLAY_ORIENTATION_180);
5529 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5530 processSync(mapper);
5531
5532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5533 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5534 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5535
5536 processUp(mapper);
5537 processSync(mapper);
5538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5539
5540 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005541 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005542 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005543 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005544 processSync(mapper);
5545
5546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5547 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5548 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5549
5550 processUp(mapper);
5551 processSync(mapper);
5552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5553}
5554
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005555TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5556 addConfigurationProperty("touch.deviceType", "touchScreen");
5557 prepareButtons();
5558 prepareAxes(POSITION);
5559 addConfigurationProperty("touch.orientationAware", "1");
5560 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5561 clearViewports();
5562 prepareDisplay(DISPLAY_ORIENTATION_0);
5563 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5564 NotifyMotionArgs args;
5565
5566 // Orientation 0.
5567 processDown(mapper, toRawX(50), toRawY(75));
5568 processSync(mapper);
5569
5570 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5571 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5572 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5573
5574 processUp(mapper);
5575 processSync(mapper);
5576 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5577}
5578
5579TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5580 addConfigurationProperty("touch.deviceType", "touchScreen");
5581 prepareButtons();
5582 prepareAxes(POSITION);
5583 addConfigurationProperty("touch.orientationAware", "1");
5584 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5585 clearViewports();
5586 prepareDisplay(DISPLAY_ORIENTATION_0);
5587 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5588 NotifyMotionArgs args;
5589
5590 // Orientation 90.
5591 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5592 processSync(mapper);
5593
5594 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5595 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5596 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5597
5598 processUp(mapper);
5599 processSync(mapper);
5600 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5601}
5602
5603TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5604 addConfigurationProperty("touch.deviceType", "touchScreen");
5605 prepareButtons();
5606 prepareAxes(POSITION);
5607 addConfigurationProperty("touch.orientationAware", "1");
5608 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5609 clearViewports();
5610 prepareDisplay(DISPLAY_ORIENTATION_0);
5611 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5612 NotifyMotionArgs args;
5613
5614 // Orientation 180.
5615 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5616 processSync(mapper);
5617
5618 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5619 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5620 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5621
5622 processUp(mapper);
5623 processSync(mapper);
5624 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5625}
5626
5627TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5628 addConfigurationProperty("touch.deviceType", "touchScreen");
5629 prepareButtons();
5630 prepareAxes(POSITION);
5631 addConfigurationProperty("touch.orientationAware", "1");
5632 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5633 clearViewports();
5634 prepareDisplay(DISPLAY_ORIENTATION_0);
5635 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5636 NotifyMotionArgs args;
5637
5638 // Orientation 270.
5639 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5640 processSync(mapper);
5641
5642 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5643 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5644 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5645
5646 processUp(mapper);
5647 processSync(mapper);
5648 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5649}
5650
5651TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5652 addConfigurationProperty("touch.deviceType", "touchScreen");
5653 prepareButtons();
5654 prepareAxes(POSITION);
5655 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5656 // orientation-aware are affected by display rotation.
5657 addConfigurationProperty("touch.orientationAware", "0");
5658 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5659 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5660
5661 NotifyMotionArgs args;
5662
5663 // Orientation 90, Rotation 0.
5664 clearViewports();
5665 prepareDisplay(DISPLAY_ORIENTATION_0);
5666 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5667 processSync(mapper);
5668
5669 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5670 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5671 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5672
5673 processUp(mapper);
5674 processSync(mapper);
5675 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5676
5677 // Orientation 90, Rotation 90.
5678 clearViewports();
5679 prepareDisplay(DISPLAY_ORIENTATION_90);
5680 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
5681 processSync(mapper);
5682
5683 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5684 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5685 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5686
5687 processUp(mapper);
5688 processSync(mapper);
5689 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5690
5691 // Orientation 90, Rotation 180.
5692 clearViewports();
5693 prepareDisplay(DISPLAY_ORIENTATION_180);
5694 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5695 processSync(mapper);
5696
5697 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5698 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5699 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5700
5701 processUp(mapper);
5702 processSync(mapper);
5703 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5704
5705 // Orientation 90, Rotation 270.
5706 clearViewports();
5707 prepareDisplay(DISPLAY_ORIENTATION_270);
5708 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
5709 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
5710 processSync(mapper);
5711
5712 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5713 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5714 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5715
5716 processUp(mapper);
5717 processSync(mapper);
5718 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5719}
5720
Michael Wrightd02c5b62014-02-10 15:10:22 -08005721TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005722 addConfigurationProperty("touch.deviceType", "touchScreen");
5723 prepareDisplay(DISPLAY_ORIENTATION_0);
5724 prepareButtons();
5725 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005726 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005727
5728 // These calculations are based on the input device calibration documentation.
5729 int32_t rawX = 100;
5730 int32_t rawY = 200;
5731 int32_t rawPressure = 10;
5732 int32_t rawToolMajor = 12;
5733 int32_t rawDistance = 2;
5734 int32_t rawTiltX = 30;
5735 int32_t rawTiltY = 110;
5736
5737 float x = toDisplayX(rawX);
5738 float y = toDisplayY(rawY);
5739 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5740 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5741 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5742 float distance = float(rawDistance);
5743
5744 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5745 float tiltScale = M_PI / 180;
5746 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5747 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5748 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5749 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5750
5751 processDown(mapper, rawX, rawY);
5752 processPressure(mapper, rawPressure);
5753 processToolMajor(mapper, rawToolMajor);
5754 processDistance(mapper, rawDistance);
5755 processTilt(mapper, rawTiltX, rawTiltY);
5756 processSync(mapper);
5757
5758 NotifyMotionArgs args;
5759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5761 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5762 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5763}
5764
Jason Gerecke489fda82012-09-07 17:19:40 -07005765TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005766 addConfigurationProperty("touch.deviceType", "touchScreen");
5767 prepareDisplay(DISPLAY_ORIENTATION_0);
5768 prepareLocationCalibration();
5769 prepareButtons();
5770 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005771 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005772
5773 int32_t rawX = 100;
5774 int32_t rawY = 200;
5775
5776 float x = toDisplayX(toCookedX(rawX, rawY));
5777 float y = toDisplayY(toCookedY(rawX, rawY));
5778
5779 processDown(mapper, rawX, rawY);
5780 processSync(mapper);
5781
5782 NotifyMotionArgs args;
5783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5785 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5786}
5787
Michael Wrightd02c5b62014-02-10 15:10:22 -08005788TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005789 addConfigurationProperty("touch.deviceType", "touchScreen");
5790 prepareDisplay(DISPLAY_ORIENTATION_0);
5791 prepareButtons();
5792 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005793 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005794
5795 NotifyMotionArgs motionArgs;
5796 NotifyKeyArgs keyArgs;
5797
5798 processDown(mapper, 100, 200);
5799 processSync(mapper);
5800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5801 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5802 ASSERT_EQ(0, motionArgs.buttonState);
5803
5804 // press BTN_LEFT, release BTN_LEFT
5805 processKey(mapper, BTN_LEFT, 1);
5806 processSync(mapper);
5807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5808 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5809 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5810
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5812 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5813 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5814
Michael Wrightd02c5b62014-02-10 15:10:22 -08005815 processKey(mapper, BTN_LEFT, 0);
5816 processSync(mapper);
5817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005818 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005819 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005820
5821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005822 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005823 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005824
5825 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5826 processKey(mapper, BTN_RIGHT, 1);
5827 processKey(mapper, BTN_MIDDLE, 1);
5828 processSync(mapper);
5829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5830 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5831 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5832 motionArgs.buttonState);
5833
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5835 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5836 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5837
5838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5839 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5840 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5841 motionArgs.buttonState);
5842
Michael Wrightd02c5b62014-02-10 15:10:22 -08005843 processKey(mapper, BTN_RIGHT, 0);
5844 processSync(mapper);
5845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005846 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005847 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005848
5849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005851 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005852
5853 processKey(mapper, BTN_MIDDLE, 0);
5854 processSync(mapper);
5855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005856 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005857 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005858
5859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005860 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005861 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005862
5863 // press BTN_BACK, release BTN_BACK
5864 processKey(mapper, BTN_BACK, 1);
5865 processSync(mapper);
5866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5867 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5868 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005869
Michael Wrightd02c5b62014-02-10 15:10:22 -08005870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005871 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005872 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5873
5874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5875 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5876 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005877
5878 processKey(mapper, BTN_BACK, 0);
5879 processSync(mapper);
5880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005881 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005882 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005883
5884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005885 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005886 ASSERT_EQ(0, motionArgs.buttonState);
5887
Michael Wrightd02c5b62014-02-10 15:10:22 -08005888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5889 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5890 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5891
5892 // press BTN_SIDE, release BTN_SIDE
5893 processKey(mapper, BTN_SIDE, 1);
5894 processSync(mapper);
5895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5896 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5897 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005898
Michael Wrightd02c5b62014-02-10 15:10:22 -08005899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005900 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005901 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5902
5903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5904 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5905 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005906
5907 processKey(mapper, BTN_SIDE, 0);
5908 processSync(mapper);
5909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005910 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005911 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005912
5913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005914 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005915 ASSERT_EQ(0, motionArgs.buttonState);
5916
Michael Wrightd02c5b62014-02-10 15:10:22 -08005917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5918 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5919 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5920
5921 // press BTN_FORWARD, release BTN_FORWARD
5922 processKey(mapper, BTN_FORWARD, 1);
5923 processSync(mapper);
5924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5925 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5926 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005927
Michael Wrightd02c5b62014-02-10 15:10:22 -08005928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005929 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005930 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5931
5932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5933 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5934 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005935
5936 processKey(mapper, BTN_FORWARD, 0);
5937 processSync(mapper);
5938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005939 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005940 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005941
5942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005943 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005944 ASSERT_EQ(0, motionArgs.buttonState);
5945
Michael Wrightd02c5b62014-02-10 15:10:22 -08005946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5947 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5948 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5949
5950 // press BTN_EXTRA, release BTN_EXTRA
5951 processKey(mapper, BTN_EXTRA, 1);
5952 processSync(mapper);
5953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5954 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5955 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005956
Michael Wrightd02c5b62014-02-10 15:10:22 -08005957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005958 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005959 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5960
5961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5962 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5963 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005964
5965 processKey(mapper, BTN_EXTRA, 0);
5966 processSync(mapper);
5967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005968 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005969 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005970
5971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005972 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005973 ASSERT_EQ(0, motionArgs.buttonState);
5974
Michael Wrightd02c5b62014-02-10 15:10:22 -08005975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5976 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5977 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5978
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5980
Michael Wrightd02c5b62014-02-10 15:10:22 -08005981 // press BTN_STYLUS, release BTN_STYLUS
5982 processKey(mapper, BTN_STYLUS, 1);
5983 processSync(mapper);
5984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5985 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005986 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5987
5988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5989 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5990 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005991
5992 processKey(mapper, BTN_STYLUS, 0);
5993 processSync(mapper);
5994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005995 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005996 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005997
5998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005999 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006000 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006001
6002 // press BTN_STYLUS2, release BTN_STYLUS2
6003 processKey(mapper, BTN_STYLUS2, 1);
6004 processSync(mapper);
6005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006007 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6008
6009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6010 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6011 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006012
6013 processKey(mapper, BTN_STYLUS2, 0);
6014 processSync(mapper);
6015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006016 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006017 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006018
6019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006021 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006022
6023 // release touch
6024 processUp(mapper);
6025 processSync(mapper);
6026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6027 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6028 ASSERT_EQ(0, motionArgs.buttonState);
6029}
6030
6031TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006032 addConfigurationProperty("touch.deviceType", "touchScreen");
6033 prepareDisplay(DISPLAY_ORIENTATION_0);
6034 prepareButtons();
6035 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006036 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006037
6038 NotifyMotionArgs motionArgs;
6039
6040 // default tool type is finger
6041 processDown(mapper, 100, 200);
6042 processSync(mapper);
6043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6044 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6045 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6046
6047 // eraser
6048 processKey(mapper, BTN_TOOL_RUBBER, 1);
6049 processSync(mapper);
6050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6052 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6053
6054 // stylus
6055 processKey(mapper, BTN_TOOL_RUBBER, 0);
6056 processKey(mapper, BTN_TOOL_PEN, 1);
6057 processSync(mapper);
6058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6060 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6061
6062 // brush
6063 processKey(mapper, BTN_TOOL_PEN, 0);
6064 processKey(mapper, BTN_TOOL_BRUSH, 1);
6065 processSync(mapper);
6066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6068 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6069
6070 // pencil
6071 processKey(mapper, BTN_TOOL_BRUSH, 0);
6072 processKey(mapper, BTN_TOOL_PENCIL, 1);
6073 processSync(mapper);
6074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6075 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6076 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6077
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006078 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006079 processKey(mapper, BTN_TOOL_PENCIL, 0);
6080 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6081 processSync(mapper);
6082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6084 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6085
6086 // mouse
6087 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6088 processKey(mapper, BTN_TOOL_MOUSE, 1);
6089 processSync(mapper);
6090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6091 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6092 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6093
6094 // lens
6095 processKey(mapper, BTN_TOOL_MOUSE, 0);
6096 processKey(mapper, BTN_TOOL_LENS, 1);
6097 processSync(mapper);
6098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6099 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6100 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6101
6102 // double-tap
6103 processKey(mapper, BTN_TOOL_LENS, 0);
6104 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6105 processSync(mapper);
6106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6108 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6109
6110 // triple-tap
6111 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6112 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6113 processSync(mapper);
6114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6116 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6117
6118 // quad-tap
6119 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6120 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6121 processSync(mapper);
6122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6124 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6125
6126 // finger
6127 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6128 processKey(mapper, BTN_TOOL_FINGER, 1);
6129 processSync(mapper);
6130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6132 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6133
6134 // stylus trumps finger
6135 processKey(mapper, BTN_TOOL_PEN, 1);
6136 processSync(mapper);
6137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6138 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6139 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6140
6141 // eraser trumps stylus
6142 processKey(mapper, BTN_TOOL_RUBBER, 1);
6143 processSync(mapper);
6144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6145 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6146 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6147
6148 // mouse trumps eraser
6149 processKey(mapper, BTN_TOOL_MOUSE, 1);
6150 processSync(mapper);
6151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6153 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6154
6155 // back to default tool type
6156 processKey(mapper, BTN_TOOL_MOUSE, 0);
6157 processKey(mapper, BTN_TOOL_RUBBER, 0);
6158 processKey(mapper, BTN_TOOL_PEN, 0);
6159 processKey(mapper, BTN_TOOL_FINGER, 0);
6160 processSync(mapper);
6161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6162 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6163 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6164}
6165
6166TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006167 addConfigurationProperty("touch.deviceType", "touchScreen");
6168 prepareDisplay(DISPLAY_ORIENTATION_0);
6169 prepareButtons();
6170 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006171 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006172 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006173
6174 NotifyMotionArgs motionArgs;
6175
6176 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6177 processKey(mapper, BTN_TOOL_FINGER, 1);
6178 processMove(mapper, 100, 200);
6179 processSync(mapper);
6180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6181 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6182 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6183 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6184
6185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6186 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6187 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6188 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6189
6190 // move a little
6191 processMove(mapper, 150, 250);
6192 processSync(mapper);
6193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6194 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6195 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6196 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6197
6198 // down when BTN_TOUCH is pressed, pressure defaults to 1
6199 processKey(mapper, BTN_TOUCH, 1);
6200 processSync(mapper);
6201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6202 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6203 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6204 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6205
6206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6207 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6209 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6210
6211 // up when BTN_TOUCH is released, hover restored
6212 processKey(mapper, BTN_TOUCH, 0);
6213 processSync(mapper);
6214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6215 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6216 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6217 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6218
6219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6220 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6221 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6222 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6223
6224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6225 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6226 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6227 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6228
6229 // exit hover when pointer goes away
6230 processKey(mapper, BTN_TOOL_FINGER, 0);
6231 processSync(mapper);
6232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6233 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6234 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6235 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6236}
6237
6238TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006239 addConfigurationProperty("touch.deviceType", "touchScreen");
6240 prepareDisplay(DISPLAY_ORIENTATION_0);
6241 prepareButtons();
6242 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006243 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006244
6245 NotifyMotionArgs motionArgs;
6246
6247 // initially hovering because pressure is 0
6248 processDown(mapper, 100, 200);
6249 processPressure(mapper, 0);
6250 processSync(mapper);
6251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6252 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6254 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6255
6256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6257 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6259 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6260
6261 // move a little
6262 processMove(mapper, 150, 250);
6263 processSync(mapper);
6264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6265 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6266 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6267 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6268
6269 // down when pressure is non-zero
6270 processPressure(mapper, RAW_PRESSURE_MAX);
6271 processSync(mapper);
6272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6273 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6275 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6276
6277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6278 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6279 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6280 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6281
6282 // up when pressure becomes 0, hover restored
6283 processPressure(mapper, 0);
6284 processSync(mapper);
6285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6286 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6288 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6289
6290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6291 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6293 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6294
6295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6296 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6298 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6299
6300 // exit hover when pointer goes away
6301 processUp(mapper);
6302 processSync(mapper);
6303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6304 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6305 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6306 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6307}
6308
Prabir Pradhan5632d622021-09-06 07:57:20 -07006309// --- TouchDisplayProjectionTest ---
6310
6311class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6312public:
6313 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6314 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6315 // rotated equivalent of the given un-rotated physical display bounds.
6316 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6317 uint32_t inverseRotationFlags;
6318 auto width = DISPLAY_WIDTH;
6319 auto height = DISPLAY_HEIGHT;
6320 switch (orientation) {
6321 case DISPLAY_ORIENTATION_90:
6322 inverseRotationFlags = ui::Transform::ROT_270;
6323 std::swap(width, height);
6324 break;
6325 case DISPLAY_ORIENTATION_180:
6326 inverseRotationFlags = ui::Transform::ROT_180;
6327 break;
6328 case DISPLAY_ORIENTATION_270:
6329 inverseRotationFlags = ui::Transform::ROT_90;
6330 std::swap(width, height);
6331 break;
6332 case DISPLAY_ORIENTATION_0:
6333 inverseRotationFlags = ui::Transform::ROT_0;
6334 break;
6335 default:
6336 FAIL() << "Invalid orientation: " << orientation;
6337 }
6338
6339 const ui::Transform rotation(inverseRotationFlags, width, height);
6340 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6341
6342 std::optional<DisplayViewport> internalViewport =
6343 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6344 DisplayViewport& v = *internalViewport;
6345 v.displayId = DISPLAY_ID;
6346 v.orientation = orientation;
6347
6348 v.logicalLeft = 0;
6349 v.logicalTop = 0;
6350 v.logicalRight = 100;
6351 v.logicalBottom = 100;
6352
6353 v.physicalLeft = rotatedPhysicalDisplay.left;
6354 v.physicalTop = rotatedPhysicalDisplay.top;
6355 v.physicalRight = rotatedPhysicalDisplay.right;
6356 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6357
6358 v.deviceWidth = width;
6359 v.deviceHeight = height;
6360
6361 v.isActive = true;
6362 v.uniqueId = UNIQUE_ID;
6363 v.type = ViewportType::INTERNAL;
6364 mFakePolicy->updateViewport(v);
6365 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6366 }
6367
6368 void assertReceivedMove(const Point& point) {
6369 NotifyMotionArgs motionArgs;
6370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6372 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6374 1, 0, 0, 0, 0, 0, 0, 0));
6375 }
6376};
6377
6378TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6379 addConfigurationProperty("touch.deviceType", "touchScreen");
6380 prepareDisplay(DISPLAY_ORIENTATION_0);
6381
6382 prepareButtons();
6383 prepareAxes(POSITION);
6384 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6385
6386 NotifyMotionArgs motionArgs;
6387
6388 // Configure the DisplayViewport such that the logical display maps to a subsection of
6389 // the display panel called the physical display. Here, the physical display is bounded by the
6390 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6391 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6392 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6393 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6394
6395 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6396 DISPLAY_ORIENTATION_270}) {
6397 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6398
6399 // Touches outside the physical display should be ignored, and should not generate any
6400 // events. Ensure touches at the following points that lie outside of the physical display
6401 // area do not generate any events.
6402 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6403 processDown(mapper, toRawX(point.x), toRawY(point.y));
6404 processSync(mapper);
6405 processUp(mapper);
6406 processSync(mapper);
6407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6408 << "Unexpected event generated for touch outside physical display at point: "
6409 << point.x << ", " << point.y;
6410 }
6411 }
6412}
6413
6414TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6415 addConfigurationProperty("touch.deviceType", "touchScreen");
6416 prepareDisplay(DISPLAY_ORIENTATION_0);
6417
6418 prepareButtons();
6419 prepareAxes(POSITION);
6420 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6421
6422 NotifyMotionArgs motionArgs;
6423
6424 // Configure the DisplayViewport such that the logical display maps to a subsection of
6425 // the display panel called the physical display. Here, the physical display is bounded by the
6426 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6427 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6428
6429 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6430 DISPLAY_ORIENTATION_270}) {
6431 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6432
6433 // Touches that start outside the physical display should be ignored until it enters the
6434 // physical display bounds, at which point it should generate a down event. Start a touch at
6435 // the point (5, 100), which is outside the physical display bounds.
6436 static const Point kOutsidePoint{5, 100};
6437 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6438 processSync(mapper);
6439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6440
6441 // Move the touch into the physical display area. This should generate a pointer down.
6442 processMove(mapper, toRawX(11), toRawY(21));
6443 processSync(mapper);
6444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6445 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6446 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6447 ASSERT_NO_FATAL_FAILURE(
6448 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6449
6450 // Move the touch inside the physical display area. This should generate a pointer move.
6451 processMove(mapper, toRawX(69), toRawY(159));
6452 processSync(mapper);
6453 assertReceivedMove({69, 159});
6454
6455 // Move outside the physical display area. Since the pointer is already down, this should
6456 // now continue generating events.
6457 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6458 processSync(mapper);
6459 assertReceivedMove(kOutsidePoint);
6460
6461 // Release. This should generate a pointer up.
6462 processUp(mapper);
6463 processSync(mapper);
6464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6465 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6467 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6468
6469 // Ensure no more events were generated.
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6472 }
6473}
6474
Michael Wrightd02c5b62014-02-10 15:10:22 -08006475// --- MultiTouchInputMapperTest ---
6476
6477class MultiTouchInputMapperTest : public TouchInputMapperTest {
6478protected:
6479 void prepareAxes(int axes);
6480
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006481 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6482 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6483 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6484 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6485 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6486 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6487 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6488 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6489 void processId(MultiTouchInputMapper& mapper, int32_t id);
6490 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6491 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6492 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
6493 void processMTSync(MultiTouchInputMapper& mapper);
6494 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006495};
6496
6497void MultiTouchInputMapperTest::prepareAxes(int axes) {
6498 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006499 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6500 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006501 }
6502 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006503 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6504 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006505 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006506 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6507 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006508 }
6509 }
6510 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006511 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6512 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006513 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006514 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006515 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006516 }
6517 }
6518 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006519 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6520 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006521 }
6522 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006523 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6524 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006525 }
6526 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006527 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6528 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006529 }
6530 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006531 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6532 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006533 }
6534 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006535 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6536 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006537 }
6538 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006539 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006540 }
6541}
6542
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006543void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6544 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006547}
6548
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006549void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6550 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006551 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006552}
6553
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006554void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6555 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006556 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006557}
6558
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006559void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006560 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006561}
6562
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006563void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006564 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006565}
6566
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006567void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6568 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006570}
6571
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006572void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006573 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006574}
6575
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006576void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006578}
6579
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006580void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006581 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006582}
6583
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006584void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006586}
6587
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006588void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006589 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006590}
6591
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006592void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6593 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006594 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006595}
6596
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006597void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006598 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006599}
6600
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006601void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006602 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006603}
6604
Michael Wrightd02c5b62014-02-10 15:10:22 -08006605TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006606 addConfigurationProperty("touch.deviceType", "touchScreen");
6607 prepareDisplay(DISPLAY_ORIENTATION_0);
6608 prepareAxes(POSITION);
6609 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006610 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006611
arthurhungdcef2dc2020-08-11 14:47:50 +08006612 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006613
6614 NotifyMotionArgs motionArgs;
6615
6616 // Two fingers down at once.
6617 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6618 processPosition(mapper, x1, y1);
6619 processMTSync(mapper);
6620 processPosition(mapper, x2, y2);
6621 processMTSync(mapper);
6622 processSync(mapper);
6623
6624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6625 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6626 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6627 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6628 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6629 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6630 ASSERT_EQ(0, motionArgs.flags);
6631 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6632 ASSERT_EQ(0, motionArgs.buttonState);
6633 ASSERT_EQ(0, motionArgs.edgeFlags);
6634 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6635 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6638 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6639 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6640 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6641 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6642
6643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6644 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6645 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6646 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6647 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006648 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006649 ASSERT_EQ(0, motionArgs.flags);
6650 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6651 ASSERT_EQ(0, motionArgs.buttonState);
6652 ASSERT_EQ(0, motionArgs.edgeFlags);
6653 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6654 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6655 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6656 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6657 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6659 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6661 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6662 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6663 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6664 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6665
6666 // Move.
6667 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6668 processPosition(mapper, x1, y1);
6669 processMTSync(mapper);
6670 processPosition(mapper, x2, y2);
6671 processMTSync(mapper);
6672 processSync(mapper);
6673
6674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6675 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6676 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6677 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6678 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6679 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6680 ASSERT_EQ(0, motionArgs.flags);
6681 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6682 ASSERT_EQ(0, motionArgs.buttonState);
6683 ASSERT_EQ(0, motionArgs.edgeFlags);
6684 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6685 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6686 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6687 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6688 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6689 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6690 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6692 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6693 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6694 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6695 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6696
6697 // First finger up.
6698 x2 += 15; y2 -= 20;
6699 processPosition(mapper, x2, y2);
6700 processMTSync(mapper);
6701 processSync(mapper);
6702
6703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6704 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6705 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6706 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6707 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006708 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006709 ASSERT_EQ(0, motionArgs.flags);
6710 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6711 ASSERT_EQ(0, motionArgs.buttonState);
6712 ASSERT_EQ(0, motionArgs.edgeFlags);
6713 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6714 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6715 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6716 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6719 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6721 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6722 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6723 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6724 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6725
6726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6727 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6728 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6729 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6730 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6731 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6732 ASSERT_EQ(0, motionArgs.flags);
6733 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6734 ASSERT_EQ(0, motionArgs.buttonState);
6735 ASSERT_EQ(0, motionArgs.edgeFlags);
6736 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6737 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6738 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6740 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6741 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6742 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6743 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6744
6745 // Move.
6746 x2 += 20; y2 -= 25;
6747 processPosition(mapper, x2, y2);
6748 processMTSync(mapper);
6749 processSync(mapper);
6750
6751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6752 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6753 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6754 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6755 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6756 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6757 ASSERT_EQ(0, motionArgs.flags);
6758 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6759 ASSERT_EQ(0, motionArgs.buttonState);
6760 ASSERT_EQ(0, motionArgs.edgeFlags);
6761 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6762 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
6763 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6764 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6765 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6766 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6767 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6768 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6769
6770 // New finger down.
6771 int32_t x3 = 700, y3 = 300;
6772 processPosition(mapper, x2, y2);
6773 processMTSync(mapper);
6774 processPosition(mapper, x3, y3);
6775 processMTSync(mapper);
6776 processSync(mapper);
6777
6778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6779 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6780 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6781 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6782 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006783 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006784 ASSERT_EQ(0, motionArgs.flags);
6785 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6786 ASSERT_EQ(0, motionArgs.buttonState);
6787 ASSERT_EQ(0, motionArgs.edgeFlags);
6788 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6789 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6790 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6791 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6792 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6794 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6796 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6797 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6798 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6799 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6800
6801 // Second finger up.
6802 x3 += 30; y3 -= 20;
6803 processPosition(mapper, x3, y3);
6804 processMTSync(mapper);
6805 processSync(mapper);
6806
6807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6808 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6809 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6810 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6811 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006812 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006813 ASSERT_EQ(0, motionArgs.flags);
6814 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6815 ASSERT_EQ(0, motionArgs.buttonState);
6816 ASSERT_EQ(0, motionArgs.edgeFlags);
6817 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6818 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6819 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6820 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6821 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6823 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6825 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6826 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6827 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6828 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6829
6830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6831 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6832 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6833 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6834 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6835 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6836 ASSERT_EQ(0, motionArgs.flags);
6837 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6838 ASSERT_EQ(0, motionArgs.buttonState);
6839 ASSERT_EQ(0, motionArgs.edgeFlags);
6840 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6841 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6842 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6843 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6844 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6845 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6846 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6847 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6848
6849 // Last finger up.
6850 processMTSync(mapper);
6851 processSync(mapper);
6852
6853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6854 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6855 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6856 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6857 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6858 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6859 ASSERT_EQ(0, motionArgs.flags);
6860 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6861 ASSERT_EQ(0, motionArgs.buttonState);
6862 ASSERT_EQ(0, motionArgs.edgeFlags);
6863 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6864 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6865 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6867 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
6868 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6869 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6870 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6871
6872 // Should not have sent any more keys or motions.
6873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6875}
6876
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006877TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
6878 addConfigurationProperty("touch.deviceType", "touchScreen");
6879 prepareDisplay(DISPLAY_ORIENTATION_0);
6880
6881 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
6882 /*fuzz*/ 0, /*resolution*/ 10);
6883 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
6884 /*fuzz*/ 0, /*resolution*/ 11);
6885 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
6886 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
6887 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
6888 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
6889 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6890 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
6891 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6892 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
6893
6894 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
6895
6896 // X and Y axes
6897 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
6898 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
6899 // Touch major and minor
6900 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
6901 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
6902 // Tool major and minor
6903 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
6904 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
6905}
6906
6907TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
6908 addConfigurationProperty("touch.deviceType", "touchScreen");
6909 prepareDisplay(DISPLAY_ORIENTATION_0);
6910
6911 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
6912 /*fuzz*/ 0, /*resolution*/ 10);
6913 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
6914 /*fuzz*/ 0, /*resolution*/ 11);
6915
6916 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
6917
6918 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
6919
6920 // Touch major and minor
6921 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
6922 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
6923 // Tool major and minor
6924 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
6925 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
6926}
6927
Michael Wrightd02c5b62014-02-10 15:10:22 -08006928TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006929 addConfigurationProperty("touch.deviceType", "touchScreen");
6930 prepareDisplay(DISPLAY_ORIENTATION_0);
6931 prepareAxes(POSITION | ID);
6932 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006933 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006934
arthurhungdcef2dc2020-08-11 14:47:50 +08006935 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006936
6937 NotifyMotionArgs motionArgs;
6938
6939 // Two fingers down at once.
6940 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6941 processPosition(mapper, x1, y1);
6942 processId(mapper, 1);
6943 processMTSync(mapper);
6944 processPosition(mapper, x2, y2);
6945 processId(mapper, 2);
6946 processMTSync(mapper);
6947 processSync(mapper);
6948
6949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6950 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6951 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6952 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6953 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6954 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6955 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6956
6957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006958 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006959 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6960 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6961 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6962 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6963 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6965 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6967 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6968
6969 // Move.
6970 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
6971 processPosition(mapper, x1, y1);
6972 processId(mapper, 1);
6973 processMTSync(mapper);
6974 processPosition(mapper, x2, y2);
6975 processId(mapper, 2);
6976 processMTSync(mapper);
6977 processSync(mapper);
6978
6979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6980 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6981 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
6982 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6983 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6984 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
6985 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
6986 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6987 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6988 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
6989 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
6990
6991 // First finger up.
6992 x2 += 15; y2 -= 20;
6993 processPosition(mapper, x2, y2);
6994 processId(mapper, 2);
6995 processMTSync(mapper);
6996 processSync(mapper);
6997
6998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08006999 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007000 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7001 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7002 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7003 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7004 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7005 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7006 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7007 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7008 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7009
7010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7011 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7012 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7013 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7014 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7015 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7016 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7017
7018 // Move.
7019 x2 += 20; y2 -= 25;
7020 processPosition(mapper, x2, y2);
7021 processId(mapper, 2);
7022 processMTSync(mapper);
7023 processSync(mapper);
7024
7025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7026 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7027 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7028 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7029 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7030 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7031 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7032
7033 // New finger down.
7034 int32_t x3 = 700, y3 = 300;
7035 processPosition(mapper, x2, y2);
7036 processId(mapper, 2);
7037 processMTSync(mapper);
7038 processPosition(mapper, x3, y3);
7039 processId(mapper, 3);
7040 processMTSync(mapper);
7041 processSync(mapper);
7042
7043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007044 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007045 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7046 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7047 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7048 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7049 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7050 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7051 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7053 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7054
7055 // Second finger up.
7056 x3 += 30; y3 -= 20;
7057 processPosition(mapper, x3, y3);
7058 processId(mapper, 3);
7059 processMTSync(mapper);
7060 processSync(mapper);
7061
7062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007063 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007064 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7065 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7066 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7067 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7068 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7069 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7070 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7071 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7072 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7073
7074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7075 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7076 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7077 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7078 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7080 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7081
7082 // Last finger up.
7083 processMTSync(mapper);
7084 processSync(mapper);
7085
7086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7087 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7088 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7089 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7090 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7091 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7092 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7093
7094 // Should not have sent any more keys or motions.
7095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7097}
7098
7099TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007100 addConfigurationProperty("touch.deviceType", "touchScreen");
7101 prepareDisplay(DISPLAY_ORIENTATION_0);
7102 prepareAxes(POSITION | ID | SLOT);
7103 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007104 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007105
arthurhungdcef2dc2020-08-11 14:47:50 +08007106 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007107
7108 NotifyMotionArgs motionArgs;
7109
7110 // Two fingers down at once.
7111 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7112 processPosition(mapper, x1, y1);
7113 processId(mapper, 1);
7114 processSlot(mapper, 1);
7115 processPosition(mapper, x2, y2);
7116 processId(mapper, 2);
7117 processSync(mapper);
7118
7119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7120 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7121 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7122 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7124 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7125 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7126
7127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007128 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007129 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7130 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7131 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7132 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7133 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7134 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7135 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7136 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7137 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7138
7139 // Move.
7140 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7141 processSlot(mapper, 0);
7142 processPosition(mapper, x1, y1);
7143 processSlot(mapper, 1);
7144 processPosition(mapper, x2, y2);
7145 processSync(mapper);
7146
7147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7148 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7149 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7150 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7151 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7152 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7153 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7154 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7155 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7156 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7157 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7158
7159 // First finger up.
7160 x2 += 15; y2 -= 20;
7161 processSlot(mapper, 0);
7162 processId(mapper, -1);
7163 processSlot(mapper, 1);
7164 processPosition(mapper, x2, y2);
7165 processSync(mapper);
7166
7167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007168 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007169 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7170 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7171 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7172 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7175 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7176 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7177 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7178
7179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7181 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7182 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7183 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7185 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7186
7187 // Move.
7188 x2 += 20; y2 -= 25;
7189 processPosition(mapper, x2, y2);
7190 processSync(mapper);
7191
7192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7193 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7194 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7195 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7196 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7198 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7199
7200 // New finger down.
7201 int32_t x3 = 700, y3 = 300;
7202 processPosition(mapper, x2, y2);
7203 processSlot(mapper, 0);
7204 processId(mapper, 3);
7205 processPosition(mapper, x3, y3);
7206 processSync(mapper);
7207
7208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007209 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007210 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7211 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7212 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7213 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7214 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7215 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7216 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7217 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7218 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7219
7220 // Second finger up.
7221 x3 += 30; y3 -= 20;
7222 processSlot(mapper, 1);
7223 processId(mapper, -1);
7224 processSlot(mapper, 0);
7225 processPosition(mapper, x3, y3);
7226 processSync(mapper);
7227
7228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007229 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007230 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7231 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7232 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7233 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7234 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7236 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7238 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7239
7240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7241 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7242 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7243 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7244 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7246 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7247
7248 // Last finger up.
7249 processId(mapper, -1);
7250 processSync(mapper);
7251
7252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7253 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7254 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7255 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7256 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7258 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7259
7260 // Should not have sent any more keys or motions.
7261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7263}
7264
7265TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007266 addConfigurationProperty("touch.deviceType", "touchScreen");
7267 prepareDisplay(DISPLAY_ORIENTATION_0);
7268 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007269 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007270
7271 // These calculations are based on the input device calibration documentation.
7272 int32_t rawX = 100;
7273 int32_t rawY = 200;
7274 int32_t rawTouchMajor = 7;
7275 int32_t rawTouchMinor = 6;
7276 int32_t rawToolMajor = 9;
7277 int32_t rawToolMinor = 8;
7278 int32_t rawPressure = 11;
7279 int32_t rawDistance = 0;
7280 int32_t rawOrientation = 3;
7281 int32_t id = 5;
7282
7283 float x = toDisplayX(rawX);
7284 float y = toDisplayY(rawY);
7285 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7286 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7287 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7288 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7289 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7290 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7291 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7292 float distance = float(rawDistance);
7293
7294 processPosition(mapper, rawX, rawY);
7295 processTouchMajor(mapper, rawTouchMajor);
7296 processTouchMinor(mapper, rawTouchMinor);
7297 processToolMajor(mapper, rawToolMajor);
7298 processToolMinor(mapper, rawToolMinor);
7299 processPressure(mapper, rawPressure);
7300 processOrientation(mapper, rawOrientation);
7301 processDistance(mapper, rawDistance);
7302 processId(mapper, id);
7303 processMTSync(mapper);
7304 processSync(mapper);
7305
7306 NotifyMotionArgs args;
7307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7308 ASSERT_EQ(0, args.pointerProperties[0].id);
7309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7310 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7311 orientation, distance));
7312}
7313
7314TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007315 addConfigurationProperty("touch.deviceType", "touchScreen");
7316 prepareDisplay(DISPLAY_ORIENTATION_0);
7317 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7318 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007319 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007320
7321 // These calculations are based on the input device calibration documentation.
7322 int32_t rawX = 100;
7323 int32_t rawY = 200;
7324 int32_t rawTouchMajor = 140;
7325 int32_t rawTouchMinor = 120;
7326 int32_t rawToolMajor = 180;
7327 int32_t rawToolMinor = 160;
7328
7329 float x = toDisplayX(rawX);
7330 float y = toDisplayY(rawY);
7331 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7332 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7333 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7334 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7335 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7336
7337 processPosition(mapper, rawX, rawY);
7338 processTouchMajor(mapper, rawTouchMajor);
7339 processTouchMinor(mapper, rawTouchMinor);
7340 processToolMajor(mapper, rawToolMajor);
7341 processToolMinor(mapper, rawToolMinor);
7342 processMTSync(mapper);
7343 processSync(mapper);
7344
7345 NotifyMotionArgs args;
7346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7347 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7348 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7349}
7350
7351TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007352 addConfigurationProperty("touch.deviceType", "touchScreen");
7353 prepareDisplay(DISPLAY_ORIENTATION_0);
7354 prepareAxes(POSITION | TOUCH | TOOL);
7355 addConfigurationProperty("touch.size.calibration", "diameter");
7356 addConfigurationProperty("touch.size.scale", "10");
7357 addConfigurationProperty("touch.size.bias", "160");
7358 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007359 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007360
7361 // These calculations are based on the input device calibration documentation.
7362 // Note: We only provide a single common touch/tool value because the device is assumed
7363 // not to emit separate values for each pointer (isSummed = 1).
7364 int32_t rawX = 100;
7365 int32_t rawY = 200;
7366 int32_t rawX2 = 150;
7367 int32_t rawY2 = 250;
7368 int32_t rawTouchMajor = 5;
7369 int32_t rawToolMajor = 8;
7370
7371 float x = toDisplayX(rawX);
7372 float y = toDisplayY(rawY);
7373 float x2 = toDisplayX(rawX2);
7374 float y2 = toDisplayY(rawY2);
7375 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7376 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7377 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7378
7379 processPosition(mapper, rawX, rawY);
7380 processTouchMajor(mapper, rawTouchMajor);
7381 processToolMajor(mapper, rawToolMajor);
7382 processMTSync(mapper);
7383 processPosition(mapper, rawX2, rawY2);
7384 processTouchMajor(mapper, rawTouchMajor);
7385 processToolMajor(mapper, rawToolMajor);
7386 processMTSync(mapper);
7387 processSync(mapper);
7388
7389 NotifyMotionArgs args;
7390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7391 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7392
7393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007394 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007395 ASSERT_EQ(size_t(2), args.pointerCount);
7396 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7397 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7399 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7400}
7401
7402TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007403 addConfigurationProperty("touch.deviceType", "touchScreen");
7404 prepareDisplay(DISPLAY_ORIENTATION_0);
7405 prepareAxes(POSITION | TOUCH | TOOL);
7406 addConfigurationProperty("touch.size.calibration", "area");
7407 addConfigurationProperty("touch.size.scale", "43");
7408 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007409 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007410
7411 // These calculations are based on the input device calibration documentation.
7412 int32_t rawX = 100;
7413 int32_t rawY = 200;
7414 int32_t rawTouchMajor = 5;
7415 int32_t rawToolMajor = 8;
7416
7417 float x = toDisplayX(rawX);
7418 float y = toDisplayY(rawY);
7419 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7420 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7421 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7422
7423 processPosition(mapper, rawX, rawY);
7424 processTouchMajor(mapper, rawTouchMajor);
7425 processToolMajor(mapper, rawToolMajor);
7426 processMTSync(mapper);
7427 processSync(mapper);
7428
7429 NotifyMotionArgs args;
7430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7432 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7433}
7434
7435TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007436 addConfigurationProperty("touch.deviceType", "touchScreen");
7437 prepareDisplay(DISPLAY_ORIENTATION_0);
7438 prepareAxes(POSITION | PRESSURE);
7439 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7440 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007441 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007442
Michael Wrightaa449c92017-12-13 21:21:43 +00007443 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007444 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007445 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7446 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7447 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7448
Michael Wrightd02c5b62014-02-10 15:10:22 -08007449 // These calculations are based on the input device calibration documentation.
7450 int32_t rawX = 100;
7451 int32_t rawY = 200;
7452 int32_t rawPressure = 60;
7453
7454 float x = toDisplayX(rawX);
7455 float y = toDisplayY(rawY);
7456 float pressure = float(rawPressure) * 0.01f;
7457
7458 processPosition(mapper, rawX, rawY);
7459 processPressure(mapper, rawPressure);
7460 processMTSync(mapper);
7461 processSync(mapper);
7462
7463 NotifyMotionArgs args;
7464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7465 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7466 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7467}
7468
7469TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007470 addConfigurationProperty("touch.deviceType", "touchScreen");
7471 prepareDisplay(DISPLAY_ORIENTATION_0);
7472 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007473 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007474
7475 NotifyMotionArgs motionArgs;
7476 NotifyKeyArgs keyArgs;
7477
7478 processId(mapper, 1);
7479 processPosition(mapper, 100, 200);
7480 processSync(mapper);
7481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7482 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7483 ASSERT_EQ(0, motionArgs.buttonState);
7484
7485 // press BTN_LEFT, release BTN_LEFT
7486 processKey(mapper, BTN_LEFT, 1);
7487 processSync(mapper);
7488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7490 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7491
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7493 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7494 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7495
Michael Wrightd02c5b62014-02-10 15:10:22 -08007496 processKey(mapper, BTN_LEFT, 0);
7497 processSync(mapper);
7498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007499 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007500 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007501
7502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007503 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007504 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007505
7506 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7507 processKey(mapper, BTN_RIGHT, 1);
7508 processKey(mapper, BTN_MIDDLE, 1);
7509 processSync(mapper);
7510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7511 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7512 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7513 motionArgs.buttonState);
7514
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7516 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7517 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7518
7519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7520 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7521 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7522 motionArgs.buttonState);
7523
Michael Wrightd02c5b62014-02-10 15:10:22 -08007524 processKey(mapper, BTN_RIGHT, 0);
7525 processSync(mapper);
7526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007527 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007528 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007529
7530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007531 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007532 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007533
7534 processKey(mapper, BTN_MIDDLE, 0);
7535 processSync(mapper);
7536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007537 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007538 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007539
7540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007541 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007542 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007543
7544 // press BTN_BACK, release BTN_BACK
7545 processKey(mapper, BTN_BACK, 1);
7546 processSync(mapper);
7547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7548 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7549 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007550
Michael Wrightd02c5b62014-02-10 15:10:22 -08007551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007553 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7554
7555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7556 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7557 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007558
7559 processKey(mapper, BTN_BACK, 0);
7560 processSync(mapper);
7561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007562 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007563 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007564
7565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007566 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007567 ASSERT_EQ(0, motionArgs.buttonState);
7568
Michael Wrightd02c5b62014-02-10 15:10:22 -08007569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7570 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7571 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7572
7573 // press BTN_SIDE, release BTN_SIDE
7574 processKey(mapper, BTN_SIDE, 1);
7575 processSync(mapper);
7576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7577 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7578 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007579
Michael Wrightd02c5b62014-02-10 15:10:22 -08007580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007581 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007582 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7583
7584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7585 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7586 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007587
7588 processKey(mapper, BTN_SIDE, 0);
7589 processSync(mapper);
7590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007591 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007592 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007593
7594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007595 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007596 ASSERT_EQ(0, motionArgs.buttonState);
7597
Michael Wrightd02c5b62014-02-10 15:10:22 -08007598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7599 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7600 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7601
7602 // press BTN_FORWARD, release BTN_FORWARD
7603 processKey(mapper, BTN_FORWARD, 1);
7604 processSync(mapper);
7605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7606 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7607 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007608
Michael Wrightd02c5b62014-02-10 15:10:22 -08007609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007611 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7612
7613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7614 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7615 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007616
7617 processKey(mapper, BTN_FORWARD, 0);
7618 processSync(mapper);
7619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007620 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007621 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007622
7623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007625 ASSERT_EQ(0, motionArgs.buttonState);
7626
Michael Wrightd02c5b62014-02-10 15:10:22 -08007627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7628 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7629 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7630
7631 // press BTN_EXTRA, release BTN_EXTRA
7632 processKey(mapper, BTN_EXTRA, 1);
7633 processSync(mapper);
7634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7635 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7636 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007637
Michael Wrightd02c5b62014-02-10 15:10:22 -08007638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007639 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007640 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7641
7642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7643 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7644 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007645
7646 processKey(mapper, BTN_EXTRA, 0);
7647 processSync(mapper);
7648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007649 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007650 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007651
7652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007653 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007654 ASSERT_EQ(0, motionArgs.buttonState);
7655
Michael Wrightd02c5b62014-02-10 15:10:22 -08007656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7657 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7658 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7659
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7661
Michael Wrightd02c5b62014-02-10 15:10:22 -08007662 // press BTN_STYLUS, release BTN_STYLUS
7663 processKey(mapper, BTN_STYLUS, 1);
7664 processSync(mapper);
7665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7666 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007667 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7668
7669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7670 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7671 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007672
7673 processKey(mapper, BTN_STYLUS, 0);
7674 processSync(mapper);
7675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007676 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007677 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007678
7679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007680 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007681 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007682
7683 // press BTN_STYLUS2, release BTN_STYLUS2
7684 processKey(mapper, BTN_STYLUS2, 1);
7685 processSync(mapper);
7686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7687 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007688 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7689
7690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7691 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7692 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007693
7694 processKey(mapper, BTN_STYLUS2, 0);
7695 processSync(mapper);
7696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007697 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007698 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007699
7700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007701 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007702 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007703
7704 // release touch
7705 processId(mapper, -1);
7706 processSync(mapper);
7707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7708 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7709 ASSERT_EQ(0, motionArgs.buttonState);
7710}
7711
7712TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007713 addConfigurationProperty("touch.deviceType", "touchScreen");
7714 prepareDisplay(DISPLAY_ORIENTATION_0);
7715 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007716 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007717
7718 NotifyMotionArgs motionArgs;
7719
7720 // default tool type is finger
7721 processId(mapper, 1);
7722 processPosition(mapper, 100, 200);
7723 processSync(mapper);
7724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7725 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7726 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7727
7728 // eraser
7729 processKey(mapper, BTN_TOOL_RUBBER, 1);
7730 processSync(mapper);
7731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7732 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7733 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7734
7735 // stylus
7736 processKey(mapper, BTN_TOOL_RUBBER, 0);
7737 processKey(mapper, BTN_TOOL_PEN, 1);
7738 processSync(mapper);
7739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7740 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7741 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7742
7743 // brush
7744 processKey(mapper, BTN_TOOL_PEN, 0);
7745 processKey(mapper, BTN_TOOL_BRUSH, 1);
7746 processSync(mapper);
7747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7749 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7750
7751 // pencil
7752 processKey(mapper, BTN_TOOL_BRUSH, 0);
7753 processKey(mapper, BTN_TOOL_PENCIL, 1);
7754 processSync(mapper);
7755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7756 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7757 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7758
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007759 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007760 processKey(mapper, BTN_TOOL_PENCIL, 0);
7761 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7762 processSync(mapper);
7763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7764 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7765 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7766
7767 // mouse
7768 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7769 processKey(mapper, BTN_TOOL_MOUSE, 1);
7770 processSync(mapper);
7771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7772 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7773 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7774
7775 // lens
7776 processKey(mapper, BTN_TOOL_MOUSE, 0);
7777 processKey(mapper, BTN_TOOL_LENS, 1);
7778 processSync(mapper);
7779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7780 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7781 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7782
7783 // double-tap
7784 processKey(mapper, BTN_TOOL_LENS, 0);
7785 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7786 processSync(mapper);
7787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7788 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7789 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7790
7791 // triple-tap
7792 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7793 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7794 processSync(mapper);
7795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7796 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7797 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7798
7799 // quad-tap
7800 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7801 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7802 processSync(mapper);
7803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7804 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7805 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7806
7807 // finger
7808 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7809 processKey(mapper, BTN_TOOL_FINGER, 1);
7810 processSync(mapper);
7811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7812 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7813 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7814
7815 // stylus trumps finger
7816 processKey(mapper, BTN_TOOL_PEN, 1);
7817 processSync(mapper);
7818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7819 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7820 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7821
7822 // eraser trumps stylus
7823 processKey(mapper, BTN_TOOL_RUBBER, 1);
7824 processSync(mapper);
7825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7826 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7827 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7828
7829 // mouse trumps eraser
7830 processKey(mapper, BTN_TOOL_MOUSE, 1);
7831 processSync(mapper);
7832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7834 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7835
7836 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
7837 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
7838 processSync(mapper);
7839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7840 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7842
7843 // MT tool type trumps BTN tool types: MT_TOOL_PEN
7844 processToolType(mapper, MT_TOOL_PEN);
7845 processSync(mapper);
7846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7847 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7848 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7849
7850 // back to default tool type
7851 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
7852 processKey(mapper, BTN_TOOL_MOUSE, 0);
7853 processKey(mapper, BTN_TOOL_RUBBER, 0);
7854 processKey(mapper, BTN_TOOL_PEN, 0);
7855 processKey(mapper, BTN_TOOL_FINGER, 0);
7856 processSync(mapper);
7857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7858 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7859 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7860}
7861
7862TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007863 addConfigurationProperty("touch.deviceType", "touchScreen");
7864 prepareDisplay(DISPLAY_ORIENTATION_0);
7865 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007866 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007867 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007868
7869 NotifyMotionArgs motionArgs;
7870
7871 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7872 processId(mapper, 1);
7873 processPosition(mapper, 100, 200);
7874 processSync(mapper);
7875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7876 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7877 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7878 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7879
7880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7881 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7882 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7883 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7884
7885 // move a little
7886 processPosition(mapper, 150, 250);
7887 processSync(mapper);
7888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7889 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7891 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7892
7893 // down when BTN_TOUCH is pressed, pressure defaults to 1
7894 processKey(mapper, BTN_TOUCH, 1);
7895 processSync(mapper);
7896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7897 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7899 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7900
7901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7902 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7903 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7904 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7905
7906 // up when BTN_TOUCH is released, hover restored
7907 processKey(mapper, BTN_TOUCH, 0);
7908 processSync(mapper);
7909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7910 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7912 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7913
7914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7915 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7916 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7917 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7918
7919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7920 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7921 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7922 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7923
7924 // exit hover when pointer goes away
7925 processId(mapper, -1);
7926 processSync(mapper);
7927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7928 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7930 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7931}
7932
7933TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007934 addConfigurationProperty("touch.deviceType", "touchScreen");
7935 prepareDisplay(DISPLAY_ORIENTATION_0);
7936 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007937 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007938
7939 NotifyMotionArgs motionArgs;
7940
7941 // initially hovering because pressure is 0
7942 processId(mapper, 1);
7943 processPosition(mapper, 100, 200);
7944 processPressure(mapper, 0);
7945 processSync(mapper);
7946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7947 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7949 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7950
7951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7952 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7953 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7954 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7955
7956 // move a little
7957 processPosition(mapper, 150, 250);
7958 processSync(mapper);
7959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7960 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7961 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7962 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7963
7964 // down when pressure becomes non-zero
7965 processPressure(mapper, RAW_PRESSURE_MAX);
7966 processSync(mapper);
7967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7968 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7970 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7971
7972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7973 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7974 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7975 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7976
7977 // up when pressure becomes 0, hover restored
7978 processPressure(mapper, 0);
7979 processSync(mapper);
7980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7981 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7983 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7984
7985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7986 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7987 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7988 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7989
7990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7991 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7992 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7993 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7994
7995 // exit hover when pointer goes away
7996 processId(mapper, -1);
7997 processSync(mapper);
7998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7999 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8001 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8002}
8003
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008004/**
8005 * Set the input device port <--> display port associations, and check that the
8006 * events are routed to the display that matches the display port.
8007 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8008 */
8009TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008010 const std::string usb2 = "USB2";
8011 const uint8_t hdmi1 = 0;
8012 const uint8_t hdmi2 = 1;
8013 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008014 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008015
8016 addConfigurationProperty("touch.deviceType", "touchScreen");
8017 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008018 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008019
8020 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8021 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8022
8023 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8024 // for this input device is specified, and the matching viewport is not present,
8025 // the input device should be disabled (at the mapper level).
8026
8027 // Add viewport for display 2 on hdmi2
8028 prepareSecondaryDisplay(type, hdmi2);
8029 // Send a touch event
8030 processPosition(mapper, 100, 100);
8031 processSync(mapper);
8032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8033
8034 // Add viewport for display 1 on hdmi1
8035 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8036 // Send a touch event again
8037 processPosition(mapper, 100, 100);
8038 processSync(mapper);
8039
8040 NotifyMotionArgs args;
8041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8042 ASSERT_EQ(DISPLAY_ID, args.displayId);
8043}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008044
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008045TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008046 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008047 std::shared_ptr<FakePointerController> fakePointerController =
8048 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008049 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008050 fakePointerController->setPosition(100, 200);
8051 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008052 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008053
Garfield Tan888a6a42020-01-09 11:39:16 -08008054 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008055 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008056
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008057 prepareDisplay(DISPLAY_ORIENTATION_0);
8058 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008059 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008060
8061 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008062 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008063
8064 NotifyMotionArgs motionArgs;
8065 processPosition(mapper, 100, 100);
8066 processSync(mapper);
8067
8068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8069 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8070 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8071}
8072
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008073/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008074 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8075 */
8076TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8077 addConfigurationProperty("touch.deviceType", "touchScreen");
8078 prepareAxes(POSITION);
8079 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8080
8081 prepareDisplay(DISPLAY_ORIENTATION_0);
8082 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8083 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8084 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8085 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8086
8087 NotifyMotionArgs args;
8088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8089 ASSERT_EQ(26, args.readTime);
8090
8091 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8092 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8093 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8094
8095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8096 ASSERT_EQ(33, args.readTime);
8097}
8098
8099/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008100 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8101 * events should not be delivered to the listener.
8102 */
8103TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8104 addConfigurationProperty("touch.deviceType", "touchScreen");
8105 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8106 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8107 ViewportType::INTERNAL);
8108 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8109 prepareAxes(POSITION);
8110 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8111
8112 NotifyMotionArgs motionArgs;
8113 processPosition(mapper, 100, 100);
8114 processSync(mapper);
8115
8116 mFakeListener->assertNotifyMotionWasNotCalled();
8117}
8118
Garfield Tanc734e4f2021-01-15 20:01:39 -08008119TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8120 addConfigurationProperty("touch.deviceType", "touchScreen");
8121 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8122 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8123 ViewportType::INTERNAL);
8124 std::optional<DisplayViewport> optionalDisplayViewport =
8125 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8126 ASSERT_TRUE(optionalDisplayViewport.has_value());
8127 DisplayViewport displayViewport = *optionalDisplayViewport;
8128
8129 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8130 prepareAxes(POSITION);
8131 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8132
8133 // Finger down
8134 int32_t x = 100, y = 100;
8135 processPosition(mapper, x, y);
8136 processSync(mapper);
8137
8138 NotifyMotionArgs motionArgs;
8139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8140 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8141
8142 // Deactivate display viewport
8143 displayViewport.isActive = false;
8144 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8145 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8146
8147 // Finger move
8148 x += 10, y += 10;
8149 processPosition(mapper, x, y);
8150 processSync(mapper);
8151
8152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8153 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8154
8155 // Reactivate display viewport
8156 displayViewport.isActive = true;
8157 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8158 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8159
8160 // Finger move again
8161 x += 10, y += 10;
8162 processPosition(mapper, x, y);
8163 processSync(mapper);
8164
8165 // Gesture is aborted, so events after display is activated won't be dispatched until there is
8166 // no pointer on the touch device.
8167 mFakeListener->assertNotifyMotionWasNotCalled();
8168}
8169
Arthur Hung7c645402019-01-25 17:45:42 +08008170TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8171 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008172 prepareAxes(POSITION | ID | SLOT);
8173 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008174 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008175
8176 // Create the second touch screen device, and enable multi fingers.
8177 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008178 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008179 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008180 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008181 std::shared_ptr<InputDevice> device2 =
8182 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
8183 Flags<InputDeviceClass>(0));
8184
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008185 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8186 0 /*flat*/, 0 /*fuzz*/);
8187 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8188 0 /*flat*/, 0 /*fuzz*/);
8189 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8190 0 /*flat*/, 0 /*fuzz*/);
8191 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8192 0 /*flat*/, 0 /*fuzz*/);
8193 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8194 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8195 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008196
8197 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008198 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Arthur Hung7c645402019-01-25 17:45:42 +08008199 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0 /*changes*/);
8200 device2->reset(ARBITRARY_TIME);
8201
8202 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008203 std::shared_ptr<FakePointerController> fakePointerController =
8204 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008205 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008206
8207 // Setup policy for associated displays and show touches.
8208 const uint8_t hdmi1 = 0;
8209 const uint8_t hdmi2 = 1;
8210 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8211 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8212 mFakePolicy->setShowTouches(true);
8213
8214 // Create displays.
8215 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008216 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008217
8218 // Default device will reconfigure above, need additional reconfiguration for another device.
8219 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008220 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung7c645402019-01-25 17:45:42 +08008221
8222 // Two fingers down at default display.
8223 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8224 processPosition(mapper, x1, y1);
8225 processId(mapper, 1);
8226 processSlot(mapper, 1);
8227 processPosition(mapper, x2, y2);
8228 processId(mapper, 2);
8229 processSync(mapper);
8230
8231 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8232 fakePointerController->getSpots().find(DISPLAY_ID);
8233 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8234 ASSERT_EQ(size_t(2), iter->second.size());
8235
8236 // Two fingers down at second display.
8237 processPosition(mapper2, x1, y1);
8238 processId(mapper2, 1);
8239 processSlot(mapper2, 1);
8240 processPosition(mapper2, x2, y2);
8241 processId(mapper2, 2);
8242 processSync(mapper2);
8243
8244 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8245 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8246 ASSERT_EQ(size_t(2), iter->second.size());
8247}
8248
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008249TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008250 prepareAxes(POSITION);
8251 addConfigurationProperty("touch.deviceType", "touchScreen");
8252 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008253 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008254
8255 NotifyMotionArgs motionArgs;
8256 // Unrotated video frame
8257 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8258 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008259 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008260 processPosition(mapper, 100, 200);
8261 processSync(mapper);
8262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8263 ASSERT_EQ(frames, motionArgs.videoFrames);
8264
8265 // Subsequent touch events should not have any videoframes
8266 // This is implemented separately in FakeEventHub,
8267 // but that should match the behaviour of TouchVideoDevice.
8268 processPosition(mapper, 200, 200);
8269 processSync(mapper);
8270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8271 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8272}
8273
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008274TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008275 prepareAxes(POSITION);
8276 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008277 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008278 // Unrotated video frame
8279 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8280 NotifyMotionArgs motionArgs;
8281
8282 // Test all 4 orientations
8283 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008284 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8285 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8286 clearViewports();
8287 prepareDisplay(orientation);
8288 std::vector<TouchVideoFrame> frames{frame};
8289 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8290 processPosition(mapper, 100, 200);
8291 processSync(mapper);
8292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8293 ASSERT_EQ(frames, motionArgs.videoFrames);
8294 }
8295}
8296
8297TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8298 prepareAxes(POSITION);
8299 addConfigurationProperty("touch.deviceType", "touchScreen");
8300 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8301 // orientation-aware are affected by display rotation.
8302 addConfigurationProperty("touch.orientationAware", "0");
8303 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8304 // Unrotated video frame
8305 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8306 NotifyMotionArgs motionArgs;
8307
8308 // Test all 4 orientations
8309 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008310 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8311 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8312 clearViewports();
8313 prepareDisplay(orientation);
8314 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008315 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008316 processPosition(mapper, 100, 200);
8317 processSync(mapper);
8318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008319 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8320 // compared to the display. This is so that when the window transform (which contains the
8321 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8322 // window's coordinate space.
8323 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008324 ASSERT_EQ(frames, motionArgs.videoFrames);
8325 }
8326}
8327
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008328TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008329 prepareAxes(POSITION);
8330 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008331 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008332 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8333 // so mix these.
8334 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8335 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8336 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8337 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8338 NotifyMotionArgs motionArgs;
8339
8340 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008341 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008342 processPosition(mapper, 100, 200);
8343 processSync(mapper);
8344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008345 ASSERT_EQ(frames, motionArgs.videoFrames);
8346}
8347
8348TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8349 prepareAxes(POSITION);
8350 addConfigurationProperty("touch.deviceType", "touchScreen");
8351 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8352 // orientation-aware are affected by display rotation.
8353 addConfigurationProperty("touch.orientationAware", "0");
8354 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8355 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8356 // so mix these.
8357 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8358 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8359 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8360 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8361 NotifyMotionArgs motionArgs;
8362
8363 prepareDisplay(DISPLAY_ORIENTATION_90);
8364 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8365 processPosition(mapper, 100, 200);
8366 processSync(mapper);
8367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8368 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8369 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8370 // compared to the display. This is so that when the window transform (which contains the
8371 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8372 // window's coordinate space.
8373 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8374 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008375 ASSERT_EQ(frames, motionArgs.videoFrames);
8376}
8377
Arthur Hung9da14732019-09-02 16:16:58 +08008378/**
8379 * If we had defined port associations, but the viewport is not ready, the touch device would be
8380 * expected to be disabled, and it should be enabled after the viewport has found.
8381 */
8382TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008383 constexpr uint8_t hdmi2 = 1;
8384 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008385 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008386
8387 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8388
8389 addConfigurationProperty("touch.deviceType", "touchScreen");
8390 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008391 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008392
8393 ASSERT_EQ(mDevice->isEnabled(), false);
8394
8395 // Add display on hdmi2, the device should be enabled and can receive touch event.
8396 prepareSecondaryDisplay(type, hdmi2);
8397 ASSERT_EQ(mDevice->isEnabled(), true);
8398
8399 // Send a touch event.
8400 processPosition(mapper, 100, 100);
8401 processSync(mapper);
8402
8403 NotifyMotionArgs args;
8404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8405 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8406}
8407
Arthur Hung421eb1c2020-01-16 00:09:42 +08008408TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008409 addConfigurationProperty("touch.deviceType", "touchScreen");
8410 prepareDisplay(DISPLAY_ORIENTATION_0);
8411 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008412 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008413
8414 NotifyMotionArgs motionArgs;
8415
8416 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8417 // finger down
8418 processId(mapper, 1);
8419 processPosition(mapper, x1, y1);
8420 processSync(mapper);
8421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8422 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8423 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8424
8425 // finger move
8426 processId(mapper, 1);
8427 processPosition(mapper, x2, y2);
8428 processSync(mapper);
8429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8430 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8432
8433 // finger up.
8434 processId(mapper, -1);
8435 processSync(mapper);
8436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8437 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8438 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8439
8440 // new finger down
8441 processId(mapper, 1);
8442 processPosition(mapper, x3, y3);
8443 processSync(mapper);
8444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8445 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8446 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8447}
8448
8449/**
arthurhungcc7f9802020-04-30 17:55:40 +08008450 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8451 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008452 */
arthurhungcc7f9802020-04-30 17:55:40 +08008453TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008454 addConfigurationProperty("touch.deviceType", "touchScreen");
8455 prepareDisplay(DISPLAY_ORIENTATION_0);
8456 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008457 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008458
8459 NotifyMotionArgs motionArgs;
8460
8461 // default tool type is finger
8462 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008463 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008464 processPosition(mapper, x1, y1);
8465 processSync(mapper);
8466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8467 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8468 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8469
8470 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8471 processToolType(mapper, MT_TOOL_PALM);
8472 processSync(mapper);
8473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8474 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8475
8476 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008477 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008478 processPosition(mapper, x2, y2);
8479 processSync(mapper);
8480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8481
8482 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008483 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008484 processSync(mapper);
8485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8486
8487 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008488 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008489 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008490 processPosition(mapper, x3, y3);
8491 processSync(mapper);
8492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8493 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8494 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8495}
8496
arthurhungbf89a482020-04-17 17:37:55 +08008497/**
arthurhungcc7f9802020-04-30 17:55:40 +08008498 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8499 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008500 */
arthurhungcc7f9802020-04-30 17:55:40 +08008501TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008502 addConfigurationProperty("touch.deviceType", "touchScreen");
8503 prepareDisplay(DISPLAY_ORIENTATION_0);
8504 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8505 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8506
8507 NotifyMotionArgs motionArgs;
8508
8509 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008510 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8511 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008512 processPosition(mapper, x1, y1);
8513 processSync(mapper);
8514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8515 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8516 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8517
8518 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008519 processSlot(mapper, SECOND_SLOT);
8520 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008521 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008522 processSync(mapper);
8523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008524 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008525 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8526
8527 // If the tool type of the first finger changes to MT_TOOL_PALM,
8528 // we expect to receive ACTION_POINTER_UP with cancel flag.
8529 processSlot(mapper, FIRST_SLOT);
8530 processId(mapper, FIRST_TRACKING_ID);
8531 processToolType(mapper, MT_TOOL_PALM);
8532 processSync(mapper);
8533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008534 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008535 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8536
8537 // The following MOVE events of second finger should be processed.
8538 processSlot(mapper, SECOND_SLOT);
8539 processId(mapper, SECOND_TRACKING_ID);
8540 processPosition(mapper, x2 + 1, y2 + 1);
8541 processSync(mapper);
8542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8543 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8544 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8545
8546 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
8547 // it. Second finger receive move.
8548 processSlot(mapper, FIRST_SLOT);
8549 processId(mapper, INVALID_TRACKING_ID);
8550 processSync(mapper);
8551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8553 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8554
8555 // Second finger keeps moving.
8556 processSlot(mapper, SECOND_SLOT);
8557 processId(mapper, SECOND_TRACKING_ID);
8558 processPosition(mapper, x2 + 2, y2 + 2);
8559 processSync(mapper);
8560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8561 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8562 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8563
8564 // Second finger up.
8565 processId(mapper, INVALID_TRACKING_ID);
8566 processSync(mapper);
8567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8568 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8569 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8570}
8571
8572/**
8573 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
8574 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
8575 */
8576TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
8577 addConfigurationProperty("touch.deviceType", "touchScreen");
8578 prepareDisplay(DISPLAY_ORIENTATION_0);
8579 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8580 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8581
8582 NotifyMotionArgs motionArgs;
8583
8584 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8585 // First finger down.
8586 processId(mapper, FIRST_TRACKING_ID);
8587 processPosition(mapper, x1, y1);
8588 processSync(mapper);
8589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8590 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8591 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8592
8593 // Second finger down.
8594 processSlot(mapper, SECOND_SLOT);
8595 processId(mapper, SECOND_TRACKING_ID);
8596 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08008597 processSync(mapper);
8598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008599 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08008600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8601
arthurhungcc7f9802020-04-30 17:55:40 +08008602 // If the tool type of the first finger changes to MT_TOOL_PALM,
8603 // we expect to receive ACTION_POINTER_UP with cancel flag.
8604 processSlot(mapper, FIRST_SLOT);
8605 processId(mapper, FIRST_TRACKING_ID);
8606 processToolType(mapper, MT_TOOL_PALM);
8607 processSync(mapper);
8608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008609 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008610 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8611
8612 // Second finger keeps moving.
8613 processSlot(mapper, SECOND_SLOT);
8614 processId(mapper, SECOND_TRACKING_ID);
8615 processPosition(mapper, x2 + 1, y2 + 1);
8616 processSync(mapper);
8617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8619
8620 // second finger becomes palm, receive cancel due to only 1 finger is active.
8621 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008622 processToolType(mapper, MT_TOOL_PALM);
8623 processSync(mapper);
8624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8625 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8626
arthurhungcc7f9802020-04-30 17:55:40 +08008627 // third finger down.
8628 processSlot(mapper, THIRD_SLOT);
8629 processId(mapper, THIRD_TRACKING_ID);
8630 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08008631 processPosition(mapper, x3, y3);
8632 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08008633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8634 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8635 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08008636 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8637
8638 // third finger move
8639 processId(mapper, THIRD_TRACKING_ID);
8640 processPosition(mapper, x3 + 1, y3 + 1);
8641 processSync(mapper);
8642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8643 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8644
8645 // first finger up, third finger receive move.
8646 processSlot(mapper, FIRST_SLOT);
8647 processId(mapper, INVALID_TRACKING_ID);
8648 processSync(mapper);
8649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8651 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8652
8653 // second finger up, third finger receive move.
8654 processSlot(mapper, SECOND_SLOT);
8655 processId(mapper, INVALID_TRACKING_ID);
8656 processSync(mapper);
8657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8658 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8659 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8660
8661 // third finger up.
8662 processSlot(mapper, THIRD_SLOT);
8663 processId(mapper, INVALID_TRACKING_ID);
8664 processSync(mapper);
8665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8666 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8667 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8668}
8669
8670/**
8671 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8672 * and the active finger could still be allowed to receive the events
8673 */
8674TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
8675 addConfigurationProperty("touch.deviceType", "touchScreen");
8676 prepareDisplay(DISPLAY_ORIENTATION_0);
8677 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8678 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8679
8680 NotifyMotionArgs motionArgs;
8681
8682 // default tool type is finger
8683 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8684 processId(mapper, FIRST_TRACKING_ID);
8685 processPosition(mapper, x1, y1);
8686 processSync(mapper);
8687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8688 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8689 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8690
8691 // Second finger down.
8692 processSlot(mapper, SECOND_SLOT);
8693 processId(mapper, SECOND_TRACKING_ID);
8694 processPosition(mapper, x2, y2);
8695 processSync(mapper);
8696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008697 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008698 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8699
8700 // If the tool type of the second finger changes to MT_TOOL_PALM,
8701 // we expect to receive ACTION_POINTER_UP with cancel flag.
8702 processId(mapper, SECOND_TRACKING_ID);
8703 processToolType(mapper, MT_TOOL_PALM);
8704 processSync(mapper);
8705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008706 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008707 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
8708
8709 // The following MOVE event should be processed.
8710 processSlot(mapper, FIRST_SLOT);
8711 processId(mapper, FIRST_TRACKING_ID);
8712 processPosition(mapper, x1 + 1, y1 + 1);
8713 processSync(mapper);
8714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8715 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8716 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8717
8718 // second finger up.
8719 processSlot(mapper, SECOND_SLOT);
8720 processId(mapper, INVALID_TRACKING_ID);
8721 processSync(mapper);
8722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8723 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8724
8725 // first finger keep moving
8726 processSlot(mapper, FIRST_SLOT);
8727 processId(mapper, FIRST_TRACKING_ID);
8728 processPosition(mapper, x1 + 2, y1 + 2);
8729 processSync(mapper);
8730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8731 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8732
8733 // first finger up.
8734 processId(mapper, INVALID_TRACKING_ID);
8735 processSync(mapper);
8736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8737 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8738 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08008739}
8740
Arthur Hung9ad18942021-06-19 02:04:46 +00008741/**
8742 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
8743 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
8744 * cause slot be valid again.
8745 */
8746TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
8747 addConfigurationProperty("touch.deviceType", "touchScreen");
8748 prepareDisplay(DISPLAY_ORIENTATION_0);
8749 prepareAxes(POSITION | ID | SLOT | PRESSURE);
8750 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8751
8752 NotifyMotionArgs motionArgs;
8753
8754 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
8755 // First finger down.
8756 processId(mapper, FIRST_TRACKING_ID);
8757 processPosition(mapper, x1, y1);
8758 processPressure(mapper, RAW_PRESSURE_MAX);
8759 processSync(mapper);
8760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8761 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8762 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8763
8764 // First finger move.
8765 processId(mapper, FIRST_TRACKING_ID);
8766 processPosition(mapper, x1 + 1, y1 + 1);
8767 processPressure(mapper, RAW_PRESSURE_MAX);
8768 processSync(mapper);
8769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8770 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8771 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8772
8773 // Second finger down.
8774 processSlot(mapper, SECOND_SLOT);
8775 processId(mapper, SECOND_TRACKING_ID);
8776 processPosition(mapper, x2, y2);
8777 processPressure(mapper, RAW_PRESSURE_MAX);
8778 processSync(mapper);
8779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008780 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00008781 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8782
8783 // second finger up with some unexpected data.
8784 processSlot(mapper, SECOND_SLOT);
8785 processId(mapper, INVALID_TRACKING_ID);
8786 processPosition(mapper, x2, y2);
8787 processSync(mapper);
8788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008789 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00008790 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
8791
8792 // first finger up with some unexpected data.
8793 processSlot(mapper, FIRST_SLOT);
8794 processId(mapper, INVALID_TRACKING_ID);
8795 processPosition(mapper, x2, y2);
8796 processPressure(mapper, RAW_PRESSURE_MAX);
8797 processSync(mapper);
8798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8799 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8800 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
8801}
8802
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008803// --- MultiTouchInputMapperTest_ExternalDevice ---
8804
8805class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
8806protected:
Chris Yea52ade12020-08-27 16:49:20 -07008807 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008808};
8809
8810/**
8811 * Expect fallback to internal viewport if device is external and external viewport is not present.
8812 */
8813TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
8814 prepareAxes(POSITION);
8815 addConfigurationProperty("touch.deviceType", "touchScreen");
8816 prepareDisplay(DISPLAY_ORIENTATION_0);
8817 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8818
8819 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
8820
8821 NotifyMotionArgs motionArgs;
8822
8823 // Expect the event to be sent to the internal viewport,
8824 // because an external viewport is not present.
8825 processPosition(mapper, 100, 100);
8826 processSync(mapper);
8827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8828 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
8829
8830 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008831 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008832 processPosition(mapper, 100, 100);
8833 processSync(mapper);
8834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8835 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8836}
Arthur Hung4197f6b2020-03-16 15:39:59 +08008837
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008838TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
8839 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
8840 std::shared_ptr<FakePointerController> fakePointerController =
8841 std::make_shared<FakePointerController>();
8842 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8843 fakePointerController->setPosition(0, 0);
8844 fakePointerController->setButtonState(0);
8845
8846 // prepare device and capture
8847 prepareDisplay(DISPLAY_ORIENTATION_0);
8848 prepareAxes(POSITION | ID | SLOT);
8849 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
8850 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
8851 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008852 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008853 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8854
8855 // captured touchpad should be a touchpad source
8856 NotifyDeviceResetArgs resetArgs;
8857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8858 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
8859
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00008860 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07008861
8862 const InputDeviceInfo::MotionRange* relRangeX =
8863 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
8864 ASSERT_NE(relRangeX, nullptr);
8865 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
8866 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
8867 const InputDeviceInfo::MotionRange* relRangeY =
8868 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
8869 ASSERT_NE(relRangeY, nullptr);
8870 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
8871 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
8872
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008873 // run captured pointer tests - note that this is unscaled, so input listener events should be
8874 // identical to what the hardware sends (accounting for any
8875 // calibration).
8876 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07008877 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008878 processId(mapper, 1);
8879 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
8880 processKey(mapper, BTN_TOUCH, 1);
8881 processSync(mapper);
8882
8883 // expect coord[0] to contain initial location of touch 0
8884 NotifyMotionArgs args;
8885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8886 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8887 ASSERT_EQ(1U, args.pointerCount);
8888 ASSERT_EQ(0, args.pointerProperties[0].id);
8889 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
8890 ASSERT_NO_FATAL_FAILURE(
8891 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8892
8893 // FINGER 1 DOWN
8894 processSlot(mapper, 1);
8895 processId(mapper, 2);
8896 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
8897 processSync(mapper);
8898
8899 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008901 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08008902 ASSERT_EQ(2U, args.pointerCount);
8903 ASSERT_EQ(0, args.pointerProperties[0].id);
8904 ASSERT_EQ(1, args.pointerProperties[1].id);
8905 ASSERT_NO_FATAL_FAILURE(
8906 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8907 ASSERT_NO_FATAL_FAILURE(
8908 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
8909
8910 // FINGER 1 MOVE
8911 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
8912 processSync(mapper);
8913
8914 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
8915 // from move
8916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8917 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8918 ASSERT_NO_FATAL_FAILURE(
8919 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
8920 ASSERT_NO_FATAL_FAILURE(
8921 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8922
8923 // FINGER 0 MOVE
8924 processSlot(mapper, 0);
8925 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
8926 processSync(mapper);
8927
8928 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
8929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8930 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8931 ASSERT_NO_FATAL_FAILURE(
8932 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
8933 ASSERT_NO_FATAL_FAILURE(
8934 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
8935
8936 // BUTTON DOWN
8937 processKey(mapper, BTN_LEFT, 1);
8938 processSync(mapper);
8939
8940 // touchinputmapper design sends a move before button press
8941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8942 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8944 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
8945
8946 // BUTTON UP
8947 processKey(mapper, BTN_LEFT, 0);
8948 processSync(mapper);
8949
8950 // touchinputmapper design sends a move after button release
8951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8952 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
8953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8954 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8955
8956 // FINGER 0 UP
8957 processId(mapper, -1);
8958 processSync(mapper);
8959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8960 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
8961
8962 // FINGER 1 MOVE
8963 processSlot(mapper, 1);
8964 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
8965 processSync(mapper);
8966
8967 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
8968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8969 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
8970 ASSERT_EQ(1U, args.pointerCount);
8971 ASSERT_EQ(1, args.pointerProperties[0].id);
8972 ASSERT_NO_FATAL_FAILURE(
8973 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
8974
8975 // FINGER 1 UP
8976 processId(mapper, -1);
8977 processKey(mapper, BTN_TOUCH, 0);
8978 processSync(mapper);
8979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8980 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
8981
8982 // non captured touchpad should be a mouse source
8983 mFakePolicy->setPointerCapture(false);
8984 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
8985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
8986 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
8987}
8988
8989TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
8990 std::shared_ptr<FakePointerController> fakePointerController =
8991 std::make_shared<FakePointerController>();
8992 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
8993 fakePointerController->setPosition(0, 0);
8994 fakePointerController->setButtonState(0);
8995
8996 // prepare device and capture
8997 prepareDisplay(DISPLAY_ORIENTATION_0);
8998 prepareAxes(POSITION | ID | SLOT);
8999 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9000 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009001 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009002 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9003 // run uncaptured pointer tests - pushes out generic events
9004 // FINGER 0 DOWN
9005 processId(mapper, 3);
9006 processPosition(mapper, 100, 100);
9007 processKey(mapper, BTN_TOUCH, 1);
9008 processSync(mapper);
9009
9010 // start at (100,100), cursor should be at (0,0) * scale
9011 NotifyMotionArgs args;
9012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9013 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9014 ASSERT_NO_FATAL_FAILURE(
9015 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9016
9017 // FINGER 0 MOVE
9018 processPosition(mapper, 200, 200);
9019 processSync(mapper);
9020
9021 // compute scaling to help with touch position checking
9022 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9023 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9024 float scale =
9025 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9026
9027 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9029 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9030 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9031 0, 0, 0, 0, 0, 0, 0));
9032}
9033
9034TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9035 std::shared_ptr<FakePointerController> fakePointerController =
9036 std::make_shared<FakePointerController>();
9037
9038 prepareDisplay(DISPLAY_ORIENTATION_0);
9039 prepareAxes(POSITION | ID | SLOT);
9040 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009041 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009042 mFakePolicy->setPointerCapture(false);
9043 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9044
9045 // uncaptured touchpad should be a pointer device
9046 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9047
9048 // captured touchpad should be a touchpad device
9049 mFakePolicy->setPointerCapture(true);
9050 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9051 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9052}
9053
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009054// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009055
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009056class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009057protected:
9058 static const char* DEVICE_NAME;
9059 static const char* DEVICE_LOCATION;
9060 static const int32_t DEVICE_ID;
9061 static const int32_t DEVICE_GENERATION;
9062 static const int32_t DEVICE_CONTROLLER_NUMBER;
9063 static const Flags<InputDeviceClass> DEVICE_CLASSES;
9064 static const int32_t EVENTHUB_ID;
9065
9066 std::shared_ptr<FakeEventHub> mFakeEventHub;
9067 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009068 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009069 std::unique_ptr<InstrumentedInputReader> mReader;
9070 std::shared_ptr<InputDevice> mDevice;
9071
9072 virtual void SetUp(Flags<InputDeviceClass> classes) {
9073 mFakeEventHub = std::make_unique<FakeEventHub>();
9074 mFakePolicy = new FakeInputReaderPolicy();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009075 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009076 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009077 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009078 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9079 }
9080
9081 void SetUp() override { SetUp(DEVICE_CLASSES); }
9082
9083 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009084 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009085 mFakePolicy.clear();
9086 }
9087
9088 void configureDevice(uint32_t changes) {
9089 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
9090 mReader->requestRefreshConfiguration(changes);
9091 mReader->loopOnce();
9092 }
9093 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
9094 }
9095
9096 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9097 const std::string& location, int32_t eventHubId,
9098 Flags<InputDeviceClass> classes) {
9099 InputDeviceIdentifier identifier;
9100 identifier.name = name;
9101 identifier.location = location;
9102 std::shared_ptr<InputDevice> device =
9103 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9104 identifier);
9105 mReader->pushNextDevice(device);
9106 mFakeEventHub->addDevice(eventHubId, name, classes);
9107 mReader->loopOnce();
9108 return device;
9109 }
9110
9111 template <class T, typename... Args>
9112 T& addControllerAndConfigure(Args... args) {
9113 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
9114
9115 return controller;
9116 }
9117};
9118
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009119const char* PeripheralControllerTest::DEVICE_NAME = "device";
9120const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
9121const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
9122const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
9123const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
9124const Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
Chris Yee2b1e5c2021-03-10 22:45:12 -08009125 Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009126const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009127
9128// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009129class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009130protected:
9131 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009132 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009133 }
9134};
9135
9136TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009137 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009138
9139 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
9140 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
9141}
9142
9143TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009144 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009145
9146 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
9147 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
9148}
9149
9150// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009151class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009152protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009153 void SetUp() override {
9154 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
9155 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08009156};
9157
Chris Ye85758332021-05-16 23:05:17 -07009158TEST_F(LightControllerTest, MonoLight) {
9159 RawLightInfo infoMono = {.id = 1,
9160 .name = "Mono",
9161 .maxBrightness = 255,
9162 .flags = InputLightClass::BRIGHTNESS,
9163 .path = ""};
9164 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -08009165
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009166 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009167 InputDeviceInfo info;
9168 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009169 std::vector<InputDeviceLightInfo> lights = info.getLights();
9170 ASSERT_EQ(1U, lights.size());
9171 ASSERT_EQ(InputDeviceLightType::MONO, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009172
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009173 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
9174 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009175}
9176
9177TEST_F(LightControllerTest, RGBLight) {
9178 RawLightInfo infoRed = {.id = 1,
9179 .name = "red",
9180 .maxBrightness = 255,
9181 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
9182 .path = ""};
9183 RawLightInfo infoGreen = {.id = 2,
9184 .name = "green",
9185 .maxBrightness = 255,
9186 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
9187 .path = ""};
9188 RawLightInfo infoBlue = {.id = 3,
9189 .name = "blue",
9190 .maxBrightness = 255,
9191 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
9192 .path = ""};
9193 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
9194 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
9195 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
9196
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009197 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009198 InputDeviceInfo info;
9199 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009200 std::vector<InputDeviceLightInfo> lights = info.getLights();
9201 ASSERT_EQ(1U, lights.size());
9202 ASSERT_EQ(InputDeviceLightType::RGB, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009203
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009204 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9205 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009206}
9207
9208TEST_F(LightControllerTest, MultiColorRGBLight) {
9209 RawLightInfo infoColor = {.id = 1,
9210 .name = "red",
9211 .maxBrightness = 255,
9212 .flags = InputLightClass::BRIGHTNESS |
9213 InputLightClass::MULTI_INTENSITY |
9214 InputLightClass::MULTI_INDEX,
9215 .path = ""};
9216
9217 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
9218
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009219 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009220 InputDeviceInfo info;
9221 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009222 std::vector<InputDeviceLightInfo> lights = info.getLights();
9223 ASSERT_EQ(1U, lights.size());
9224 ASSERT_EQ(InputDeviceLightType::MULTI_COLOR, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009225
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009226 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9227 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009228}
9229
9230TEST_F(LightControllerTest, PlayerIdLight) {
9231 RawLightInfo info1 = {.id = 1,
9232 .name = "player1",
9233 .maxBrightness = 255,
9234 .flags = InputLightClass::BRIGHTNESS,
9235 .path = ""};
9236 RawLightInfo info2 = {.id = 2,
9237 .name = "player2",
9238 .maxBrightness = 255,
9239 .flags = InputLightClass::BRIGHTNESS,
9240 .path = ""};
9241 RawLightInfo info3 = {.id = 3,
9242 .name = "player3",
9243 .maxBrightness = 255,
9244 .flags = InputLightClass::BRIGHTNESS,
9245 .path = ""};
9246 RawLightInfo info4 = {.id = 4,
9247 .name = "player4",
9248 .maxBrightness = 255,
9249 .flags = InputLightClass::BRIGHTNESS,
9250 .path = ""};
9251 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
9252 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
9253 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
9254 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
9255
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009256 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009257 InputDeviceInfo info;
9258 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009259 std::vector<InputDeviceLightInfo> lights = info.getLights();
9260 ASSERT_EQ(1U, lights.size());
9261 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009262
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009263 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
9264 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
9265 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009266}
9267
Michael Wrightd02c5b62014-02-10 15:10:22 -08009268} // namespace android