blob: a8b0fe9c9a297e06f3b963291e4a63fd011d4fee [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
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
19
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <CursorInputMapper.h>
21#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080024#include <InputReaderBase.h>
25#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000026#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070029#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070030#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
32#include <SwitchInputMapper.h>
33#include <TestInputListener.h>
Prabir Pradhan739dca42022-09-09 20:12:01 +000034#include <TestInputListenerMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070035#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080036#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000037#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070038#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080039#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050040#include <gui/constants.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070042#include <thread>
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000043#include "android/hardware/input/InputDeviceCountryCode.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000044#include "input/DisplayViewport.h"
45#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010046
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000047using android::hardware::input::InputDeviceCountryCode;
48
Michael Wrightd02c5b62014-02-10 15:10:22 -080049namespace android {
50
Dominik Laskowski2f01d772022-03-23 16:01:29 -070051using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000052using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070053using std::chrono_literals::operator""ms;
54
55// Timeout for waiting for an expected event
56static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
57
Michael Wrightd02c5b62014-02-10 15:10:22 -080058// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000059static constexpr nsecs_t ARBITRARY_TIME = 1234;
60static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080061
62// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080063static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000064static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080065static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000066static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080067static constexpr int32_t DISPLAY_WIDTH = 480;
68static constexpr int32_t DISPLAY_HEIGHT = 800;
69static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
70static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
71static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070072static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070073static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080074
arthurhungcc7f9802020-04-30 17:55:40 +080075static constexpr int32_t FIRST_SLOT = 0;
76static constexpr int32_t SECOND_SLOT = 1;
77static constexpr int32_t THIRD_SLOT = 2;
78static constexpr int32_t INVALID_TRACKING_ID = -1;
79static constexpr int32_t FIRST_TRACKING_ID = 0;
80static constexpr int32_t SECOND_TRACKING_ID = 1;
81static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080082static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080083static constexpr int32_t BATTERY_STATUS = 4;
84static constexpr int32_t BATTERY_CAPACITY = 66;
Prabir Pradhane287ecd2022-09-07 21:18:05 +000085static const std::string BATTERY_DEVPATH = "/sys/devices/mydevice/power_supply/mybattery";
Chris Ye3fdbfef2021-01-06 18:45:18 -080086static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
87static constexpr int32_t LIGHT_COLOR = 0x7F448866;
88static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080089
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080090static constexpr int32_t ACTION_POINTER_0_DOWN =
91 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
92static constexpr int32_t ACTION_POINTER_0_UP =
93 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
94static constexpr int32_t ACTION_POINTER_1_DOWN =
95 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
96static constexpr int32_t ACTION_POINTER_1_UP =
97 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
98
Michael Wrightd02c5b62014-02-10 15:10:22 -080099// Error tolerance for floating point assertions.
100static const float EPSILON = 0.001f;
101
102template<typename T>
103static inline T min(T a, T b) {
104 return a < b ? a : b;
105}
106
107static inline float avg(float x, float y) {
108 return (x + y) / 2;
109}
110
Chris Ye3fdbfef2021-01-06 18:45:18 -0800111// Mapping for light color name and the light color
112const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
113 {"green", LightColor::GREEN},
114 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800115
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700116static int32_t getInverseRotation(int32_t orientation) {
117 switch (orientation) {
118 case DISPLAY_ORIENTATION_90:
119 return DISPLAY_ORIENTATION_270;
120 case DISPLAY_ORIENTATION_270:
121 return DISPLAY_ORIENTATION_90;
122 default:
123 return orientation;
124 }
125}
126
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800127static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
128 InputDeviceInfo info;
129 mapper.populateDeviceInfo(&info);
130
131 const InputDeviceInfo::MotionRange* motionRange =
132 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
133 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
134}
135
136static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
137 InputDeviceInfo info;
138 mapper.populateDeviceInfo(&info);
139
140 const InputDeviceInfo::MotionRange* motionRange =
141 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
142 ASSERT_EQ(nullptr, motionRange);
143}
144
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700145[[maybe_unused]] static void dumpReader(InputReader& reader) {
146 std::string dump;
147 reader.dump(dump);
148 std::istringstream iss(dump);
149 for (std::string line; std::getline(iss, line);) {
150 ALOGE("%s", line.c_str());
151 std::this_thread::sleep_for(std::chrono::milliseconds(1));
152 }
153}
154
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155// --- FakePointerController ---
156
157class FakePointerController : public PointerControllerInterface {
158 bool mHaveBounds;
159 float mMinX, mMinY, mMaxX, mMaxY;
160 float mX, mY;
161 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800162 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164public:
165 FakePointerController() :
166 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800167 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168 }
169
Michael Wright17db18e2020-06-26 20:51:44 +0100170 virtual ~FakePointerController() {}
171
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172 void setBounds(float minX, float minY, float maxX, float maxY) {
173 mHaveBounds = true;
174 mMinX = minX;
175 mMinY = minY;
176 mMaxX = maxX;
177 mMaxY = maxY;
178 }
179
Chris Yea52ade12020-08-27 16:49:20 -0700180 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181 mX = x;
182 mY = y;
183 }
184
Chris Yea52ade12020-08-27 16:49:20 -0700185 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186
Chris Yea52ade12020-08-27 16:49:20 -0700187 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188
Chris Yea52ade12020-08-27 16:49:20 -0700189 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190 *outX = mX;
191 *outY = mY;
192 }
193
Chris Yea52ade12020-08-27 16:49:20 -0700194 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800195
Chris Yea52ade12020-08-27 16:49:20 -0700196 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800197 mDisplayId = viewport.displayId;
198 }
199
Arthur Hung7c645402019-01-25 17:45:42 +0800200 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
201 return mSpotsByDisplay;
202 }
203
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204private:
Chris Yea52ade12020-08-27 16:49:20 -0700205 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206 *outMinX = mMinX;
207 *outMinY = mMinY;
208 *outMaxX = mMaxX;
209 *outMaxY = mMaxY;
210 return mHaveBounds;
211 }
212
Chris Yea52ade12020-08-27 16:49:20 -0700213 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800214 mX += deltaX;
215 if (mX < mMinX) mX = mMinX;
216 if (mX > mMaxX) mX = mMaxX;
217 mY += deltaY;
218 if (mY < mMinY) mY = mMinY;
219 if (mY > mMaxY) mY = mMaxY;
220 }
221
Chris Yea52ade12020-08-27 16:49:20 -0700222 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800223
Chris Yea52ade12020-08-27 16:49:20 -0700224 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225
Chris Yea52ade12020-08-27 16:49:20 -0700226 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800227
Chris Yea52ade12020-08-27 16:49:20 -0700228 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
229 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800230 std::vector<int32_t> newSpots;
231 // Add spots for fingers that are down.
232 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
233 uint32_t id = idBits.clearFirstMarkedBit();
234 newSpots.push_back(id);
235 }
236
237 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800238 }
239
Prabir Pradhan197e0862022-07-01 14:28:00 +0000240 void clearSpots() override { mSpotsByDisplay.clear(); }
Arthur Hung7c645402019-01-25 17:45:42 +0800241
242 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243};
244
245
246// --- FakeInputReaderPolicy ---
247
248class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700249 std::mutex mLock;
250 std::condition_variable mDevicesChangedCondition;
251
Michael Wrightd02c5b62014-02-10 15:10:22 -0800252 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000253 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700254 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
255 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100256 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700257 TouchAffineTransformation transform;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258
259protected:
Chris Yea52ade12020-08-27 16:49:20 -0700260 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800261
262public:
263 FakeInputReaderPolicy() {
264 }
265
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700266 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800267 waitForInputDevices([](bool devicesChanged) {
268 if (!devicesChanged) {
269 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
270 }
271 });
272 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700273
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800274 void assertInputDevicesNotChanged() {
275 waitForInputDevices([](bool devicesChanged) {
276 if (devicesChanged) {
277 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
278 }
279 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700280 }
281
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700282 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100283 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100284 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700285 }
286
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700287 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
288 return mConfig.getDisplayViewportByUniqueId(uniqueId);
289 }
290 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
291 return mConfig.getDisplayViewportByType(type);
292 }
293
294 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
295 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700296 }
297
Prabir Pradhan5632d622021-09-06 07:57:20 -0700298 void addDisplayViewport(DisplayViewport viewport) {
299 mViewports.push_back(std::move(viewport));
300 mConfig.setDisplayViewports(mViewports);
301 }
302
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700303 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000304 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700305 std::optional<uint8_t> physicalPort, ViewportType type) {
306 const bool isRotated =
307 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
308 DisplayViewport v;
309 v.displayId = displayId;
310 v.orientation = orientation;
311 v.logicalLeft = 0;
312 v.logicalTop = 0;
313 v.logicalRight = isRotated ? height : width;
314 v.logicalBottom = isRotated ? width : height;
315 v.physicalLeft = 0;
316 v.physicalTop = 0;
317 v.physicalRight = isRotated ? height : width;
318 v.physicalBottom = isRotated ? width : height;
319 v.deviceWidth = isRotated ? height : width;
320 v.deviceHeight = isRotated ? width : height;
321 v.isActive = isActive;
322 v.uniqueId = uniqueId;
323 v.physicalPort = physicalPort;
324 v.type = type;
325
326 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800327 }
328
Arthur Hung6cd19a42019-08-30 19:04:12 +0800329 bool updateViewport(const DisplayViewport& viewport) {
330 size_t count = mViewports.size();
331 for (size_t i = 0; i < count; i++) {
332 const DisplayViewport& currentViewport = mViewports[i];
333 if (currentViewport.displayId == viewport.displayId) {
334 mViewports[i] = viewport;
335 mConfig.setDisplayViewports(mViewports);
336 return true;
337 }
338 }
339 // no viewport found.
340 return false;
341 }
342
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100343 void addExcludedDeviceName(const std::string& deviceName) {
344 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800345 }
346
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700347 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
348 mConfig.portAssociations.insert({inputPort, displayPort});
349 }
350
Christine Franks1ba71cc2021-04-07 14:37:42 -0700351 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
352 const std::string& displayUniqueId) {
353 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
354 }
355
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000356 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700357
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000358 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700359
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000360 void setPointerController(std::shared_ptr<FakePointerController> controller) {
361 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800362 }
363
364 const InputReaderConfiguration* getReaderConfiguration() const {
365 return &mConfig;
366 }
367
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800368 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800369 return mInputDevices;
370 }
371
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100372 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700373 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700374 return transform;
375 }
376
377 void setTouchAffineTransformation(const TouchAffineTransformation t) {
378 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800379 }
380
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000381 PointerCaptureRequest setPointerCapture(bool enabled) {
382 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
383 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800384 }
385
Arthur Hung7c645402019-01-25 17:45:42 +0800386 void setShowTouches(bool enabled) {
387 mConfig.showTouches = enabled;
388 }
389
Garfield Tan888a6a42020-01-09 11:39:16 -0800390 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
391 mConfig.defaultPointerDisplayId = pointerDisplayId;
392 }
393
HQ Liue6983c72022-04-19 22:14:56 +0000394 void setPointerGestureEnabled(bool enabled) { mConfig.pointerGesturesEnabled = enabled; }
395
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800396 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
397
HQ Liue6983c72022-04-19 22:14:56 +0000398 float getPointerGestureZoomSpeedRatio() { return mConfig.pointerGestureZoomSpeedRatio; }
399
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000400 void setVelocityControlParams(const VelocityControlParameters& params) {
401 mConfig.pointerVelocityControlParameters = params;
402 mConfig.wheelVelocityControlParameters = params;
403 }
404
Michael Wrightd02c5b62014-02-10 15:10:22 -0800405private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000406 uint32_t mNextPointerCaptureSequenceNumber = 0;
407
Chris Yea52ade12020-08-27 16:49:20 -0700408 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800409 *outConfig = mConfig;
410 }
411
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000412 std::shared_ptr<PointerControllerInterface> obtainPointerController(
413 int32_t /*deviceId*/) override {
414 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800415 }
416
Chris Yea52ade12020-08-27 16:49:20 -0700417 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700418 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800419 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700420 mInputDevicesChanged = true;
421 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800422 }
423
Chris Yea52ade12020-08-27 16:49:20 -0700424 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
425 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700426 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800427 }
428
Chris Yea52ade12020-08-27 16:49:20 -0700429 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800430
431 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
432 std::unique_lock<std::mutex> lock(mLock);
433 base::ScopedLockAssertion assumeLocked(mLock);
434
435 const bool devicesChanged =
436 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
437 return mInputDevicesChanged;
438 });
439 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
440 mInputDevicesChanged = false;
441 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800442};
443
Michael Wrightd02c5b62014-02-10 15:10:22 -0800444// --- FakeEventHub ---
445
446class FakeEventHub : public EventHubInterface {
447 struct KeyInfo {
448 int32_t keyCode;
449 uint32_t flags;
450 };
451
Chris Yef59a2f42020-10-16 12:55:26 -0700452 struct SensorInfo {
453 InputDeviceSensorType sensorType;
454 int32_t sensorDataIndex;
455 };
456
Michael Wrightd02c5b62014-02-10 15:10:22 -0800457 struct Device {
458 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700459 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800460 PropertyMap configuration;
461 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
462 KeyedVector<int, bool> relativeAxes;
463 KeyedVector<int32_t, int32_t> keyCodeStates;
464 KeyedVector<int32_t, int32_t> scanCodeStates;
465 KeyedVector<int32_t, int32_t> switchStates;
466 KeyedVector<int32_t, int32_t> absoluteAxisValue;
467 KeyedVector<int32_t, KeyInfo> keysByScanCode;
468 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
469 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100470 // fake mapping which would normally come from keyCharacterMap
471 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700472 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
473 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800474 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700475 bool enabled;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000476 InputDeviceCountryCode countryCode;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700477
478 status_t enable() {
479 enabled = true;
480 return OK;
481 }
482
483 status_t disable() {
484 enabled = false;
485 return OK;
486 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800487
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700488 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800489 };
490
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700491 std::mutex mLock;
492 std::condition_variable mEventsCondition;
493
Michael Wrightd02c5b62014-02-10 15:10:22 -0800494 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100495 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000496 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600497 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000498 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800499 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
500 // Simulates a device light brightness, from light id to light brightness.
501 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
502 // Simulates a device light intensities, from light id to light intensities map.
503 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
504 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800505
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700506public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800507 virtual ~FakeEventHub() {
508 for (size_t i = 0; i < mDevices.size(); i++) {
509 delete mDevices.valueAt(i);
510 }
511 }
512
Michael Wrightd02c5b62014-02-10 15:10:22 -0800513 FakeEventHub() { }
514
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700515 void addDevice(int32_t deviceId, const std::string& name,
516 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800517 Device* device = new Device(classes);
518 device->identifier.name = name;
519 mDevices.add(deviceId, device);
520
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000521 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800522 }
523
524 void removeDevice(int32_t deviceId) {
525 delete mDevices.valueFor(deviceId);
526 mDevices.removeItem(deviceId);
527
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000528 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800529 }
530
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000531 bool isDeviceEnabled(int32_t deviceId) const override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700532 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700533 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700534 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
535 return false;
536 }
537 return device->enabled;
538 }
539
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000540 status_t enableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700541 status_t result;
542 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700543 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700544 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
545 return BAD_VALUE;
546 }
547 if (device->enabled) {
548 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
549 return OK;
550 }
551 result = device->enable();
552 return result;
553 }
554
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000555 status_t disableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700556 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700557 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700558 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
559 return BAD_VALUE;
560 }
561 if (!device->enabled) {
562 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
563 return OK;
564 }
565 return device->disable();
566 }
567
Michael Wrightd02c5b62014-02-10 15:10:22 -0800568 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000569 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800570 }
571
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700572 void addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800573 Device* device = getDevice(deviceId);
574 device->configuration.addProperty(key, value);
575 }
576
577 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
578 Device* device = getDevice(deviceId);
579 device->configuration.addAll(configuration);
580 }
581
582 void addAbsoluteAxis(int32_t deviceId, int axis,
583 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
584 Device* device = getDevice(deviceId);
585
586 RawAbsoluteAxisInfo info;
587 info.valid = true;
588 info.minValue = minValue;
589 info.maxValue = maxValue;
590 info.flat = flat;
591 info.fuzz = fuzz;
592 info.resolution = resolution;
593 device->absoluteAxes.add(axis, info);
594 }
595
596 void addRelativeAxis(int32_t deviceId, int32_t axis) {
597 Device* device = getDevice(deviceId);
598 device->relativeAxes.add(axis, true);
599 }
600
601 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
602 Device* device = getDevice(deviceId);
603 device->keyCodeStates.replaceValueFor(keyCode, state);
604 }
605
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000606 void setCountryCode(int32_t deviceId, InputDeviceCountryCode countryCode) {
607 Device* device = getDevice(deviceId);
608 device->countryCode = countryCode;
609 }
610
Michael Wrightd02c5b62014-02-10 15:10:22 -0800611 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
612 Device* device = getDevice(deviceId);
613 device->scanCodeStates.replaceValueFor(scanCode, state);
614 }
615
616 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
617 Device* device = getDevice(deviceId);
618 device->switchStates.replaceValueFor(switchCode, state);
619 }
620
621 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
622 Device* device = getDevice(deviceId);
623 device->absoluteAxisValue.replaceValueFor(axis, value);
624 }
625
626 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
627 int32_t keyCode, uint32_t flags) {
628 Device* device = getDevice(deviceId);
629 KeyInfo info;
630 info.keyCode = keyCode;
631 info.flags = flags;
632 if (scanCode) {
633 device->keysByScanCode.add(scanCode, info);
634 }
635 if (usageCode) {
636 device->keysByUsageCode.add(usageCode, info);
637 }
638 }
639
Philip Junker4af3b3d2021-12-14 10:36:55 +0100640 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
641 Device* device = getDevice(deviceId);
642 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
643 }
644
Michael Wrightd02c5b62014-02-10 15:10:22 -0800645 void addLed(int32_t deviceId, int32_t led, bool initialState) {
646 Device* device = getDevice(deviceId);
647 device->leds.add(led, initialState);
648 }
649
Chris Yef59a2f42020-10-16 12:55:26 -0700650 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
651 int32_t sensorDataIndex) {
652 Device* device = getDevice(deviceId);
653 SensorInfo info;
654 info.sensorType = sensorType;
655 info.sensorDataIndex = sensorDataIndex;
656 device->sensorsByAbsCode.emplace(absCode, info);
657 }
658
659 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
660 Device* device = getDevice(deviceId);
661 typename BitArray<MSC_MAX>::Buffer buffer;
662 buffer[mscEvent / 32] = 1 << mscEvent % 32;
663 device->mscBitmask.loadFromBuffer(buffer);
664 }
665
Chris Ye3fdbfef2021-01-06 18:45:18 -0800666 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
667 mRawLightInfos.emplace(rawId, std::move(info));
668 }
669
670 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
671 mLightBrightness.emplace(rawId, brightness);
672 }
673
674 void fakeLightIntensities(int32_t rawId,
675 const std::unordered_map<LightColor, int32_t> intensities) {
676 mLightIntensities.emplace(rawId, std::move(intensities));
677 }
678
Michael Wrightd02c5b62014-02-10 15:10:22 -0800679 bool getLedState(int32_t deviceId, int32_t led) {
680 Device* device = getDevice(deviceId);
681 return device->leds.valueFor(led);
682 }
683
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100684 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800685 return mExcludedDevices;
686 }
687
688 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
689 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800690 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800691 }
692
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000693 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
694 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700695 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800696 RawEvent event;
697 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000698 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800699 event.deviceId = deviceId;
700 event.type = type;
701 event.code = code;
702 event.value = value;
703 mEvents.push_back(event);
704
705 if (type == EV_ABS) {
706 setAbsoluteAxisValue(deviceId, code, value);
707 }
708 }
709
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600710 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
711 std::vector<TouchVideoFrame>> videoFrames) {
712 mVideoFrames = std::move(videoFrames);
713 }
714
Michael Wrightd02c5b62014-02-10 15:10:22 -0800715 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700716 std::unique_lock<std::mutex> lock(mLock);
717 base::ScopedLockAssertion assumeLocked(mLock);
718 const bool queueIsEmpty =
719 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
720 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
721 if (!queueIsEmpty) {
722 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
723 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800724 }
725
726private:
727 Device* getDevice(int32_t deviceId) const {
728 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100729 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800730 }
731
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700732 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800733 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700734 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800735 }
736
Chris Yea52ade12020-08-27 16:49:20 -0700737 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800738 Device* device = getDevice(deviceId);
739 return device ? device->identifier : InputDeviceIdentifier();
740 }
741
Chris Yea52ade12020-08-27 16:49:20 -0700742 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800743
Chris Yea52ade12020-08-27 16:49:20 -0700744 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800745 Device* device = getDevice(deviceId);
746 if (device) {
747 *outConfiguration = device->configuration;
748 }
749 }
750
Chris Yea52ade12020-08-27 16:49:20 -0700751 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
752 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800753 Device* device = getDevice(deviceId);
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700754 if (device) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800755 ssize_t index = device->absoluteAxes.indexOfKey(axis);
756 if (index >= 0) {
757 *outAxisInfo = device->absoluteAxes.valueAt(index);
758 return OK;
759 }
760 }
761 outAxisInfo->clear();
762 return -1;
763 }
764
Chris Yea52ade12020-08-27 16:49:20 -0700765 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800766 Device* device = getDevice(deviceId);
767 if (device) {
768 return device->relativeAxes.indexOfKey(axis) >= 0;
769 }
770 return false;
771 }
772
Chris Yea52ade12020-08-27 16:49:20 -0700773 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800774
Chris Yef59a2f42020-10-16 12:55:26 -0700775 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
776 Device* device = getDevice(deviceId);
777 if (device) {
778 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
779 }
780 return false;
781 }
782
Chris Yea52ade12020-08-27 16:49:20 -0700783 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
784 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800785 Device* device = getDevice(deviceId);
786 if (device) {
787 const KeyInfo* key = getKey(device, scanCode, usageCode);
788 if (key) {
789 if (outKeycode) {
790 *outKeycode = key->keyCode;
791 }
792 if (outFlags) {
793 *outFlags = key->flags;
794 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700795 if (outMetaState) {
796 *outMetaState = metaState;
797 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800798 return OK;
799 }
800 }
801 return NAME_NOT_FOUND;
802 }
803
804 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
805 if (usageCode) {
806 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
807 if (index >= 0) {
808 return &device->keysByUsageCode.valueAt(index);
809 }
810 }
811 if (scanCode) {
812 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
813 if (index >= 0) {
814 return &device->keysByScanCode.valueAt(index);
815 }
816 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700817 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800818 }
819
Chris Yea52ade12020-08-27 16:49:20 -0700820 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800821
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000822 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
823 int32_t deviceId, int32_t absCode) const override {
Chris Yef59a2f42020-10-16 12:55:26 -0700824 Device* device = getDevice(deviceId);
825 if (!device) {
826 return Errorf("Sensor device not found.");
827 }
828 auto it = device->sensorsByAbsCode.find(absCode);
829 if (it == device->sensorsByAbsCode.end()) {
830 return Errorf("Sensor map not found.");
831 }
832 const SensorInfo& info = it->second;
833 return std::make_pair(info.sensorType, info.sensorDataIndex);
834 }
835
Chris Yea52ade12020-08-27 16:49:20 -0700836 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800837 mExcludedDevices = devices;
838 }
839
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700840 std::vector<RawEvent> getEvents(int) override {
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000841 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800842
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700843 std::vector<RawEvent> buffer;
844 std::swap(buffer, mEvents);
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000845
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700846 mEventsCondition.notify_all();
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700847 return buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800848 }
849
Chris Yea52ade12020-08-27 16:49:20 -0700850 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600851 auto it = mVideoFrames.find(deviceId);
852 if (it != mVideoFrames.end()) {
853 std::vector<TouchVideoFrame> frames = std::move(it->second);
854 mVideoFrames.erase(deviceId);
855 return frames;
856 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800857 return {};
858 }
859
Chris Yea52ade12020-08-27 16:49:20 -0700860 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861 Device* device = getDevice(deviceId);
862 if (device) {
863 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
864 if (index >= 0) {
865 return device->scanCodeStates.valueAt(index);
866 }
867 }
868 return AKEY_STATE_UNKNOWN;
869 }
870
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000871 InputDeviceCountryCode getCountryCode(int32_t deviceId) const override {
872 Device* device = getDevice(deviceId);
873 if (device) {
874 return device->countryCode;
875 }
876 return InputDeviceCountryCode::INVALID;
877 }
878
Chris Yea52ade12020-08-27 16:49:20 -0700879 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800880 Device* device = getDevice(deviceId);
881 if (device) {
882 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
883 if (index >= 0) {
884 return device->keyCodeStates.valueAt(index);
885 }
886 }
887 return AKEY_STATE_UNKNOWN;
888 }
889
Chris Yea52ade12020-08-27 16:49:20 -0700890 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800891 Device* device = getDevice(deviceId);
892 if (device) {
893 ssize_t index = device->switchStates.indexOfKey(sw);
894 if (index >= 0) {
895 return device->switchStates.valueAt(index);
896 }
897 }
898 return AKEY_STATE_UNKNOWN;
899 }
900
Chris Yea52ade12020-08-27 16:49:20 -0700901 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
902 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800903 Device* device = getDevice(deviceId);
904 if (device) {
905 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
906 if (index >= 0) {
907 *outValue = device->absoluteAxisValue.valueAt(index);
908 return OK;
909 }
910 }
911 *outValue = 0;
912 return -1;
913 }
914
Philip Junker4af3b3d2021-12-14 10:36:55 +0100915 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
916 Device* device = getDevice(deviceId);
917 if (!device) {
918 return AKEYCODE_UNKNOWN;
919 }
920 auto it = device->keyCodeMapping.find(locationKeyCode);
921 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
922 }
923
Chris Yea52ade12020-08-27 16:49:20 -0700924 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700925 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700926 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927 bool result = false;
928 Device* device = getDevice(deviceId);
929 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700930 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700931 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800932 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
933 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
934 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800935 }
936 }
937 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
938 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
939 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940 }
941 }
942 }
943 }
944 return result;
945 }
946
Chris Yea52ade12020-08-27 16:49:20 -0700947 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800948 Device* device = getDevice(deviceId);
949 if (device) {
950 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
951 return index >= 0;
952 }
953 return false;
954 }
955
Arthur Hungcb40a002021-08-03 14:31:01 +0000956 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
957 Device* device = getDevice(deviceId);
958 if (!device) {
959 return false;
960 }
961 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
962 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
963 return true;
964 }
965 }
966 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
967 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
968 return true;
969 }
970 }
971 return false;
972 }
973
Chris Yea52ade12020-08-27 16:49:20 -0700974 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800975 Device* device = getDevice(deviceId);
976 return device && device->leds.indexOfKey(led) >= 0;
977 }
978
Chris Yea52ade12020-08-27 16:49:20 -0700979 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800980 Device* device = getDevice(deviceId);
981 if (device) {
982 ssize_t index = device->leds.indexOfKey(led);
983 if (index >= 0) {
984 device->leds.replaceValueAt(led, on);
985 } else {
986 ADD_FAILURE()
987 << "Attempted to set the state of an LED that the EventHub declared "
988 "was not present. led=" << led;
989 }
990 }
991 }
992
Chris Yea52ade12020-08-27 16:49:20 -0700993 void getVirtualKeyDefinitions(
994 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800995 outVirtualKeys.clear();
996
997 Device* device = getDevice(deviceId);
998 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800999 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001000 }
1001 }
1002
Chris Yea52ade12020-08-27 16:49:20 -07001003 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -07001004 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001005 }
1006
Chris Yea52ade12020-08-27 16:49:20 -07001007 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001008 return false;
1009 }
1010
Chris Yea52ade12020-08-27 16:49:20 -07001011 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001012
Chris Yea52ade12020-08-27 16:49:20 -07001013 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001014
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001015 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return mVibrators; };
Chris Ye87143712020-11-10 05:05:58 +00001016
Chris Yee2b1e5c2021-03-10 22:45:12 -08001017 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
1018 return BATTERY_CAPACITY;
1019 }
Kim Low03ea0352020-11-06 12:45:07 -08001020
Chris Yee2b1e5c2021-03-10 22:45:12 -08001021 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
1022 return BATTERY_STATUS;
1023 }
1024
Andy Chenf9f1a022022-08-29 20:07:10 -04001025 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override {
1026 return {DEFAULT_BATTERY};
1027 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001028
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001029 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
1030 int32_t batteryId) const override {
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001031 if (batteryId != DEFAULT_BATTERY) return {};
1032 static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY,
1033 .name = "default battery",
1034 .flags = InputBatteryClass::CAPACITY,
1035 .path = BATTERY_DEVPATH};
1036 return BATTERY_INFO;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001037 }
Kim Low03ea0352020-11-06 12:45:07 -08001038
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001039 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001040 std::vector<int32_t> ids;
1041 for (const auto& [rawId, info] : mRawLightInfos) {
1042 ids.push_back(rawId);
1043 }
1044 return ids;
1045 }
1046
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001047 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001048 auto it = mRawLightInfos.find(lightId);
1049 if (it == mRawLightInfos.end()) {
1050 return std::nullopt;
1051 }
1052 return it->second;
1053 }
1054
1055 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1056 mLightBrightness.emplace(lightId, brightness);
1057 }
1058
1059 void setLightIntensities(int32_t deviceId, int32_t lightId,
1060 std::unordered_map<LightColor, int32_t> intensities) override {
1061 mLightIntensities.emplace(lightId, intensities);
1062 };
1063
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001064 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001065 auto lightIt = mLightBrightness.find(lightId);
1066 if (lightIt == mLightBrightness.end()) {
1067 return std::nullopt;
1068 }
1069 return lightIt->second;
1070 }
1071
1072 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001073 int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001074 auto lightIt = mLightIntensities.find(lightId);
1075 if (lightIt == mLightIntensities.end()) {
1076 return std::nullopt;
1077 }
1078 return lightIt->second;
1079 };
1080
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001081 void dump(std::string&) const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001082
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001083 void monitor() const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001084
Chris Yea52ade12020-08-27 16:49:20 -07001085 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001086
Chris Yea52ade12020-08-27 16:49:20 -07001087 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001088};
1089
Michael Wrightd02c5b62014-02-10 15:10:22 -08001090// --- FakeInputMapper ---
1091
1092class FakeInputMapper : public InputMapper {
1093 uint32_t mSources;
1094 int32_t mKeyboardType;
1095 int32_t mMetaState;
1096 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1097 KeyedVector<int32_t, int32_t> mScanCodeStates;
1098 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001099 // fake mapping which would normally come from keyCharacterMap
1100 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001101 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001103 std::mutex mLock;
1104 std::condition_variable mStateChangedCondition;
1105 bool mConfigureWasCalled GUARDED_BY(mLock);
1106 bool mResetWasCalled GUARDED_BY(mLock);
1107 bool mProcessWasCalled GUARDED_BY(mLock);
1108 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001109
Arthur Hungc23540e2018-11-29 20:42:11 +08001110 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001111public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001112 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1113 : InputMapper(deviceContext),
1114 mSources(sources),
1115 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001116 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001117 mConfigureWasCalled(false),
1118 mResetWasCalled(false),
1119 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001120
Chris Yea52ade12020-08-27 16:49:20 -07001121 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001122
1123 void setKeyboardType(int32_t keyboardType) {
1124 mKeyboardType = keyboardType;
1125 }
1126
1127 void setMetaState(int32_t metaState) {
1128 mMetaState = metaState;
1129 }
1130
1131 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001132 std::unique_lock<std::mutex> lock(mLock);
1133 base::ScopedLockAssertion assumeLocked(mLock);
1134 const bool configureCalled =
1135 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1136 return mConfigureWasCalled;
1137 });
1138 if (!configureCalled) {
1139 FAIL() << "Expected configure() to have been called.";
1140 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001141 mConfigureWasCalled = false;
1142 }
1143
1144 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001145 std::unique_lock<std::mutex> lock(mLock);
1146 base::ScopedLockAssertion assumeLocked(mLock);
1147 const bool resetCalled =
1148 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1149 return mResetWasCalled;
1150 });
1151 if (!resetCalled) {
1152 FAIL() << "Expected reset() to have been called.";
1153 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001154 mResetWasCalled = false;
1155 }
1156
Yi Kong9b14ac62018-07-17 13:48:38 -07001157 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001158 std::unique_lock<std::mutex> lock(mLock);
1159 base::ScopedLockAssertion assumeLocked(mLock);
1160 const bool processCalled =
1161 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1162 return mProcessWasCalled;
1163 });
1164 if (!processCalled) {
1165 FAIL() << "Expected process() to have been called.";
1166 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001167 if (outLastEvent) {
1168 *outLastEvent = mLastEvent;
1169 }
1170 mProcessWasCalled = false;
1171 }
1172
1173 void setKeyCodeState(int32_t keyCode, int32_t state) {
1174 mKeyCodeStates.replaceValueFor(keyCode, state);
1175 }
1176
1177 void setScanCodeState(int32_t scanCode, int32_t state) {
1178 mScanCodeStates.replaceValueFor(scanCode, state);
1179 }
1180
1181 void setSwitchState(int32_t switchCode, int32_t state) {
1182 mSwitchStates.replaceValueFor(switchCode, state);
1183 }
1184
1185 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001186 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001187 }
1188
Philip Junker4af3b3d2021-12-14 10:36:55 +01001189 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1190 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1191 }
1192
Michael Wrightd02c5b62014-02-10 15:10:22 -08001193private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001194 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001195
Chris Yea52ade12020-08-27 16:49:20 -07001196 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001197 InputMapper::populateDeviceInfo(deviceInfo);
1198
1199 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1200 deviceInfo->setKeyboardType(mKeyboardType);
1201 }
1202 }
1203
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001204 std::list<NotifyArgs> configure(nsecs_t, const InputReaderConfiguration* config,
1205 uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001206 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001207 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001208
1209 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001210 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001211 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1212 mViewport = config->getDisplayViewportByPort(*displayPort);
1213 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001214
1215 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001216 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001217 }
1218
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001219 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001220 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001221 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001222 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001223 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001224 }
1225
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001226 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001227 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001228 mLastEvent = *rawEvent;
1229 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001230 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001231 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001232 }
1233
Chris Yea52ade12020-08-27 16:49:20 -07001234 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001235 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1236 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1237 }
1238
Philip Junker4af3b3d2021-12-14 10:36:55 +01001239 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1240 auto it = mKeyCodeMapping.find(locationKeyCode);
1241 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1242 }
1243
Chris Yea52ade12020-08-27 16:49:20 -07001244 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001245 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1246 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1247 }
1248
Chris Yea52ade12020-08-27 16:49:20 -07001249 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001250 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1251 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1252 }
1253
Chris Yea52ade12020-08-27 16:49:20 -07001254 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001255 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001256 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001257 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001258 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1259 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1260 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001261 }
1262 }
1263 }
Chris Yea52ade12020-08-27 16:49:20 -07001264 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001265 return result;
1266 }
1267
1268 virtual int32_t getMetaState() {
1269 return mMetaState;
1270 }
1271
1272 virtual void fadePointer() {
1273 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001274
1275 virtual std::optional<int32_t> getAssociatedDisplay() {
1276 if (mViewport) {
1277 return std::make_optional(mViewport->displayId);
1278 }
1279 return std::nullopt;
1280 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001281};
1282
1283
1284// --- InstrumentedInputReader ---
1285
1286class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001287 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001288
1289public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001290 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1291 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001292 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001293 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001294
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001295 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001296
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001297 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001298
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001299 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001300 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001301 InputDeviceIdentifier identifier;
1302 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001303 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001304 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001305 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001306 }
1307
Prabir Pradhan28efc192019-11-05 01:10:04 +00001308 // Make the protected loopOnce method accessible to tests.
1309 using InputReader::loopOnce;
1310
Michael Wrightd02c5b62014-02-10 15:10:22 -08001311protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001312 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1313 const InputDeviceIdentifier& identifier)
1314 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001315 if (!mNextDevices.empty()) {
1316 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1317 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001318 return device;
1319 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001320 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001321 }
1322
arthurhungdcef2dc2020-08-11 14:47:50 +08001323 // --- FakeInputReaderContext ---
1324 class FakeInputReaderContext : public ContextImpl {
1325 int32_t mGlobalMetaState;
1326 bool mUpdateGlobalMetaStateWasCalled;
1327 int32_t mGeneration;
1328
1329 public:
1330 FakeInputReaderContext(InputReader* reader)
1331 : ContextImpl(reader),
1332 mGlobalMetaState(0),
1333 mUpdateGlobalMetaStateWasCalled(false),
1334 mGeneration(1) {}
1335
1336 virtual ~FakeInputReaderContext() {}
1337
1338 void assertUpdateGlobalMetaStateWasCalled() {
1339 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1340 << "Expected updateGlobalMetaState() to have been called.";
1341 mUpdateGlobalMetaStateWasCalled = false;
1342 }
1343
1344 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1345
1346 uint32_t getGeneration() { return mGeneration; }
1347
1348 void updateGlobalMetaState() override {
1349 mUpdateGlobalMetaStateWasCalled = true;
1350 ContextImpl::updateGlobalMetaState();
1351 }
1352
1353 int32_t getGlobalMetaState() override {
1354 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1355 }
1356
1357 int32_t bumpGeneration() override {
1358 mGeneration = ContextImpl::bumpGeneration();
1359 return mGeneration;
1360 }
1361 } mFakeContext;
1362
Michael Wrightd02c5b62014-02-10 15:10:22 -08001363 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001364
1365public:
1366 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001367};
1368
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001369// --- InputReaderPolicyTest ---
1370class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001371protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001372 sp<FakeInputReaderPolicy> mFakePolicy;
1373
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001374 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001375 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001376};
1377
1378/**
1379 * Check that empty set of viewports is an acceptable configuration.
1380 * Also try to get internal viewport two different ways - by type and by uniqueId.
1381 *
1382 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1383 * Such configuration is not currently allowed.
1384 */
1385TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001386 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001387
1388 // We didn't add any viewports yet, so there shouldn't be any.
1389 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001390 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001391 ASSERT_FALSE(internalViewport);
1392
1393 // Add an internal viewport, then clear it
1394 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001395 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001396 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001397
1398 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001399 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001400 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001401 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001402
1403 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001404 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001405 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001406 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001407
1408 mFakePolicy->clearViewports();
1409 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001410 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001411 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001412 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001413 ASSERT_FALSE(internalViewport);
1414}
1415
1416TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1417 const std::string internalUniqueId = "local:0";
1418 const std::string externalUniqueId = "local:1";
1419 const std::string virtualUniqueId1 = "virtual:2";
1420 const std::string virtualUniqueId2 = "virtual:3";
1421 constexpr int32_t virtualDisplayId1 = 2;
1422 constexpr int32_t virtualDisplayId2 = 3;
1423
1424 // Add an internal viewport
1425 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001426 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1427 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001428 // Add an external viewport
1429 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001430 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1431 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001432 // Add an virtual viewport
1433 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001434 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1435 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001436 // Add another virtual viewport
1437 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001438 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1439 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001440
1441 // Check matching by type for internal
1442 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001443 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001444 ASSERT_TRUE(internalViewport);
1445 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1446
1447 // Check matching by type for external
1448 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001449 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001450 ASSERT_TRUE(externalViewport);
1451 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1452
1453 // Check matching by uniqueId for virtual viewport #1
1454 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001455 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001456 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001457 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001458 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1459 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1460
1461 // Check matching by uniqueId for virtual viewport #2
1462 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001463 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001464 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001465 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001466 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1467 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1468}
1469
1470
1471/**
1472 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1473 * that lookup works by checking display id.
1474 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1475 */
1476TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1477 const std::string uniqueId1 = "uniqueId1";
1478 const std::string uniqueId2 = "uniqueId2";
1479 constexpr int32_t displayId1 = 2;
1480 constexpr int32_t displayId2 = 3;
1481
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001482 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1483 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001484 for (const ViewportType& type : types) {
1485 mFakePolicy->clearViewports();
1486 // Add a viewport
1487 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001488 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1489 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001490 // Add another viewport
1491 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001492 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1493 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001494
1495 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001496 std::optional<DisplayViewport> viewport1 =
1497 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001498 ASSERT_TRUE(viewport1);
1499 ASSERT_EQ(displayId1, viewport1->displayId);
1500 ASSERT_EQ(type, viewport1->type);
1501
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001502 std::optional<DisplayViewport> viewport2 =
1503 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001504 ASSERT_TRUE(viewport2);
1505 ASSERT_EQ(displayId2, viewport2->displayId);
1506 ASSERT_EQ(type, viewport2->type);
1507
1508 // When there are multiple viewports of the same kind, and uniqueId is not specified
1509 // in the call to getDisplayViewport, then that situation is not supported.
1510 // The viewports can be stored in any order, so we cannot rely on the order, since that
1511 // is just implementation detail.
1512 // However, we can check that it still returns *a* viewport, we just cannot assert
1513 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001514 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001515 ASSERT_TRUE(someViewport);
1516 }
1517}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001518
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001519/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001520 * When we have multiple internal displays make sure we always return the default display when
1521 * querying by type.
1522 */
1523TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1524 const std::string uniqueId1 = "uniqueId1";
1525 const std::string uniqueId2 = "uniqueId2";
1526 constexpr int32_t nonDefaultDisplayId = 2;
1527 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1528 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1529
1530 // Add the default display first and ensure it gets returned.
1531 mFakePolicy->clearViewports();
1532 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001533 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001534 ViewportType::INTERNAL);
1535 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001536 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001537 ViewportType::INTERNAL);
1538
1539 std::optional<DisplayViewport> viewport =
1540 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1541 ASSERT_TRUE(viewport);
1542 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1543 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1544
1545 // Add the default display second to make sure order doesn't matter.
1546 mFakePolicy->clearViewports();
1547 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001548 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001549 ViewportType::INTERNAL);
1550 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001551 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001552 ViewportType::INTERNAL);
1553
1554 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1555 ASSERT_TRUE(viewport);
1556 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1557 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1558}
1559
1560/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001561 * Check getDisplayViewportByPort
1562 */
1563TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001564 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001565 const std::string uniqueId1 = "uniqueId1";
1566 const std::string uniqueId2 = "uniqueId2";
1567 constexpr int32_t displayId1 = 1;
1568 constexpr int32_t displayId2 = 2;
1569 const uint8_t hdmi1 = 0;
1570 const uint8_t hdmi2 = 1;
1571 const uint8_t hdmi3 = 2;
1572
1573 mFakePolicy->clearViewports();
1574 // Add a viewport that's associated with some display port that's not of interest.
1575 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001576 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1577 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001578 // Add another viewport, connected to HDMI1 port
1579 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001580 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1581 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001582
1583 // Check that correct display viewport was returned by comparing the display ports.
1584 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1585 ASSERT_TRUE(hdmi1Viewport);
1586 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1587 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1588
1589 // Check that we can still get the same viewport using the uniqueId
1590 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1591 ASSERT_TRUE(hdmi1Viewport);
1592 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1593 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1594 ASSERT_EQ(type, hdmi1Viewport->type);
1595
1596 // Check that we cannot find a port with "HDMI2", because we never added one
1597 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1598 ASSERT_FALSE(hdmi2Viewport);
1599}
1600
Michael Wrightd02c5b62014-02-10 15:10:22 -08001601// --- InputReaderTest ---
1602
1603class InputReaderTest : public testing::Test {
1604protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001605 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001606 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001607 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001608 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001609
Chris Yea52ade12020-08-27 16:49:20 -07001610 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001611 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001612 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001613 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001614
Prabir Pradhan28efc192019-11-05 01:10:04 +00001615 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001616 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001617 }
1618
Chris Yea52ade12020-08-27 16:49:20 -07001619 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001620 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001621 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001622 }
1623
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001624 void addDevice(int32_t eventHubId, const std::string& name,
1625 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001626 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001627
1628 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001629 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001630 }
1631 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001632 mReader->loopOnce();
1633 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001634 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1635 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001636 }
1637
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001638 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001639 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001640 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001641 }
1642
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001643 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001644 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001645 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001646 }
1647
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001648 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001649 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001650 ftl::Flags<InputDeviceClass> classes,
1651 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001652 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001653 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1654 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001655 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001656 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001657 return mapper;
1658 }
1659};
1660
Chris Ye98d3f532020-10-01 21:48:59 -07001661TEST_F(InputReaderTest, PolicyGetInputDevices) {
1662 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001663 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001664 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001665
1666 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001667 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001668 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001669 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001670 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001671 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1672 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001673 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001674}
1675
Chris Yee7310032020-09-22 15:36:28 -07001676TEST_F(InputReaderTest, GetMergedInputDevices) {
1677 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1678 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1679 // Add two subdevices to device
1680 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1681 // Must add at least one mapper or the device will be ignored!
1682 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1683 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1684
1685 // Push same device instance for next device to be added, so they'll have same identifier.
1686 mReader->pushNextDevice(device);
1687 mReader->pushNextDevice(device);
1688 ASSERT_NO_FATAL_FAILURE(
1689 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1690 ASSERT_NO_FATAL_FAILURE(
1691 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1692
1693 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001694 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001695}
1696
Chris Yee14523a2020-12-19 13:46:00 -08001697TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1698 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1699 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1700 // Add two subdevices to device
1701 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1702 // Must add at least one mapper or the device will be ignored!
1703 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1704 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1705
1706 // Push same device instance for next device to be added, so they'll have same identifier.
1707 mReader->pushNextDevice(device);
1708 mReader->pushNextDevice(device);
1709 // Sensor device is initially disabled
1710 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1711 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1712 nullptr));
1713 // Device is disabled because the only sub device is a sensor device and disabled initially.
1714 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1715 ASSERT_FALSE(device->isEnabled());
1716 ASSERT_NO_FATAL_FAILURE(
1717 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1718 // The merged device is enabled if any sub device is enabled
1719 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1720 ASSERT_TRUE(device->isEnabled());
1721}
1722
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001723TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001724 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001725 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001726 constexpr int32_t eventHubId = 1;
1727 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001728 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001729 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001730 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001731 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001732
Yi Kong9b14ac62018-07-17 13:48:38 -07001733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001734
1735 NotifyDeviceResetArgs resetArgs;
1736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001737 ASSERT_EQ(deviceId, resetArgs.deviceId);
1738
1739 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001740 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001741 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001742
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001744 ASSERT_EQ(deviceId, resetArgs.deviceId);
1745 ASSERT_EQ(device->isEnabled(), false);
1746
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001747 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001748 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001751 ASSERT_EQ(device->isEnabled(), false);
1752
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001753 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001754 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001756 ASSERT_EQ(deviceId, resetArgs.deviceId);
1757 ASSERT_EQ(device->isEnabled(), true);
1758}
1759
Michael Wrightd02c5b62014-02-10 15:10:22 -08001760TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001761 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001762 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001763 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001764 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001765 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001766 AINPUT_SOURCE_KEYBOARD, nullptr);
1767 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001768
1769 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1770 AINPUT_SOURCE_ANY, AKEYCODE_A))
1771 << "Should return unknown when the device id is >= 0 but unknown.";
1772
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001773 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1774 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1775 << "Should return unknown when the device id is valid but the sources are not "
1776 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001777
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001778 ASSERT_EQ(AKEY_STATE_DOWN,
1779 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1780 AKEYCODE_A))
1781 << "Should return value provided by mapper when device id is valid and the device "
1782 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001783
1784 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1785 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1786 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1787
1788 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1789 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1790 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1791}
1792
Philip Junker4af3b3d2021-12-14 10:36:55 +01001793TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1794 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1795 constexpr int32_t eventHubId = 1;
1796 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1797 InputDeviceClass::KEYBOARD,
1798 AINPUT_SOURCE_KEYBOARD, nullptr);
1799 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1800
1801 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1802 << "Should return unknown when the device with the specified id is not found.";
1803
1804 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1805 << "Should return correct mapping when device id is valid and mapping exists.";
1806
1807 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1808 << "Should return the location key code when device id is valid and there's no "
1809 "mapping.";
1810}
1811
1812TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1813 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1814 constexpr int32_t eventHubId = 1;
1815 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1816 InputDeviceClass::JOYSTICK,
1817 AINPUT_SOURCE_GAMEPAD, nullptr);
1818 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1819
1820 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1821 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1822}
1823
Michael Wrightd02c5b62014-02-10 15:10:22 -08001824TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001825 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001826 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001827 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001828 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001829 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001830 AINPUT_SOURCE_KEYBOARD, nullptr);
1831 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001832
1833 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1834 AINPUT_SOURCE_ANY, KEY_A))
1835 << "Should return unknown when the device id is >= 0 but unknown.";
1836
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001837 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1838 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1839 << "Should return unknown when the device id is valid but the sources are not "
1840 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001841
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001842 ASSERT_EQ(AKEY_STATE_DOWN,
1843 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1844 KEY_A))
1845 << "Should return value provided by mapper when device id is valid and the device "
1846 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001847
1848 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1849 AINPUT_SOURCE_TRACKBALL, KEY_A))
1850 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1851
1852 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1853 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1854 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1855}
1856
1857TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001858 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001859 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001860 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001861 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001862 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001863 AINPUT_SOURCE_KEYBOARD, nullptr);
1864 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001865
1866 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1867 AINPUT_SOURCE_ANY, SW_LID))
1868 << "Should return unknown when the device id is >= 0 but unknown.";
1869
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001870 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1871 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1872 << "Should return unknown when the device id is valid but the sources are not "
1873 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001874
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001875 ASSERT_EQ(AKEY_STATE_DOWN,
1876 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1877 SW_LID))
1878 << "Should return value provided by mapper when device id is valid and the device "
1879 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001880
1881 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1882 AINPUT_SOURCE_TRACKBALL, SW_LID))
1883 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1884
1885 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1886 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1887 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1888}
1889
1890TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001891 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001892 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001893 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001894 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001895 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001896 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001897
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001898 mapper.addSupportedKeyCode(AKEYCODE_A);
1899 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001900
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001901 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001902 uint8_t flags[4] = { 0, 0, 0, 1 };
1903
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001904 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001905 << "Should return false when device id is >= 0 but unknown.";
1906 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1907
1908 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001909 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001910 << "Should return false when device id is valid but the sources are not supported by "
1911 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001912 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1913
1914 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001915 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001916 keyCodes, flags))
1917 << "Should return value provided by mapper when device id is valid and the device "
1918 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001919 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1920
1921 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001922 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1923 << "Should return false when the device id is < 0 but the sources are not supported by "
1924 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001925 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1926
1927 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001928 ASSERT_TRUE(
1929 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1930 << "Should return value provided by mapper when device id is < 0 and one of the "
1931 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001932 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1933}
1934
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001935TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001936 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001937 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001938
1939 NotifyConfigurationChangedArgs args;
1940
1941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1942 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1943}
1944
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001945TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001946 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001947 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001948 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001949 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001950 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001951 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001952 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001953 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001954
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001955 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001956 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001957 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1958
1959 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001960 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001961 ASSERT_EQ(when, event.when);
1962 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001963 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001964 ASSERT_EQ(EV_KEY, event.type);
1965 ASSERT_EQ(KEY_A, event.code);
1966 ASSERT_EQ(1, event.value);
1967}
1968
Garfield Tan1c7bc862020-01-28 13:24:04 -08001969TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001970 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001971 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001972 constexpr int32_t eventHubId = 1;
1973 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001974 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001975 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001976 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001977 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001978
1979 NotifyDeviceResetArgs resetArgs;
1980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001981 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001982
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001983 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001984 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001986 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001987 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001988
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001989 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001990 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001992 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001993 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001994
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001995 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001996 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001998 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001999 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002000}
2001
Garfield Tan1c7bc862020-01-28 13:24:04 -08002002TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
2003 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002004 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002005 constexpr int32_t eventHubId = 1;
2006 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2007 // Must add at least one mapper or the device will be ignored!
2008 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002009 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002010 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
2011
2012 NotifyDeviceResetArgs resetArgs;
2013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2014 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
2015}
2016
Arthur Hungc23540e2018-11-29 20:42:11 +08002017TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002018 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002019 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002020 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08002021 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002022 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2023 FakeInputMapper& mapper =
2024 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002025 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002026
2027 const uint8_t hdmi1 = 1;
2028
2029 // Associated touch screen with second display.
2030 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2031
2032 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002033 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002034 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002035 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002036 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002037 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002038 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002039 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002040 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002041 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002042
2043 // Add the device, and make sure all of the callbacks are triggered.
2044 // The device is added after the input port associations are processed since
2045 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002046 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002049 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002050
Arthur Hung2c9a3342019-07-23 14:18:59 +08002051 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002052 ASSERT_EQ(deviceId, device->getId());
2053 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2054 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002055
2056 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002057 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002058 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002059 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002060}
2061
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002062TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2063 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002064 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002065 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2066 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2067 // Must add at least one mapper or the device will be ignored!
2068 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2069 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2070 mReader->pushNextDevice(device);
2071 mReader->pushNextDevice(device);
2072 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2073 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2074
2075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2076
2077 NotifyDeviceResetArgs resetArgs;
2078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2079 ASSERT_EQ(deviceId, resetArgs.deviceId);
2080 ASSERT_TRUE(device->isEnabled());
2081 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2082 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2083
2084 disableDevice(deviceId);
2085 mReader->loopOnce();
2086
2087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2088 ASSERT_EQ(deviceId, resetArgs.deviceId);
2089 ASSERT_FALSE(device->isEnabled());
2090 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2091 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2092
2093 enableDevice(deviceId);
2094 mReader->loopOnce();
2095
2096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2097 ASSERT_EQ(deviceId, resetArgs.deviceId);
2098 ASSERT_TRUE(device->isEnabled());
2099 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2100 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2101}
2102
2103TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2104 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002105 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002106 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2107 // Add two subdevices to device
2108 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2109 FakeInputMapper& mapperDevice1 =
2110 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2111 FakeInputMapper& mapperDevice2 =
2112 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2113 mReader->pushNextDevice(device);
2114 mReader->pushNextDevice(device);
2115 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2116 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2117
2118 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2119 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2120
2121 ASSERT_EQ(AKEY_STATE_DOWN,
2122 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2123 ASSERT_EQ(AKEY_STATE_DOWN,
2124 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2125 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2126 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2127}
2128
Prabir Pradhan7e186182020-11-10 13:56:45 -08002129TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2130 NotifyPointerCaptureChangedArgs args;
2131
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002132 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002133 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2134 mReader->loopOnce();
2135 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002136 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2137 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002138
2139 mFakePolicy->setPointerCapture(false);
2140 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2141 mReader->loopOnce();
2142 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002143 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002144
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002145 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002146 // does not change.
2147 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2148 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002149 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002150}
2151
Chris Ye87143712020-11-10 05:05:58 +00002152class FakeVibratorInputMapper : public FakeInputMapper {
2153public:
2154 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2155 : FakeInputMapper(deviceContext, sources) {}
2156
2157 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2158};
2159
2160TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2161 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002162 ftl::Flags<InputDeviceClass> deviceClass =
2163 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002164 constexpr int32_t eventHubId = 1;
2165 const char* DEVICE_LOCATION = "BLUETOOTH";
2166 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2167 FakeVibratorInputMapper& mapper =
2168 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2169 mReader->pushNextDevice(device);
2170
2171 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2172 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2173
2174 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2175 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2176}
2177
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002178// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002179
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002180class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002181public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002182 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002183
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002184 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002185
Andy Chenf9f1a022022-08-29 20:07:10 -04002186 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2187
Chris Yee2b1e5c2021-03-10 22:45:12 -08002188 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2189
2190 void dump(std::string& dump) override {}
2191
2192 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2193 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002194 }
2195
Chris Yee2b1e5c2021-03-10 22:45:12 -08002196 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2197 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002198 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002199
2200 bool setLightColor(int32_t lightId, int32_t color) override {
2201 getDeviceContext().setLightBrightness(lightId, color >> 24);
2202 return true;
2203 }
2204
2205 std::optional<int32_t> getLightColor(int32_t lightId) override {
2206 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2207 if (!result.has_value()) {
2208 return std::nullopt;
2209 }
2210 return result.value() << 24;
2211 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002212
2213 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2214
2215 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2216
2217private:
2218 InputDeviceContext& mDeviceContext;
2219 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2220 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04002221 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002222};
2223
Chris Yee2b1e5c2021-03-10 22:45:12 -08002224TEST_F(InputReaderTest, BatteryGetCapacity) {
2225 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002226 ftl::Flags<InputDeviceClass> deviceClass =
2227 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002228 constexpr int32_t eventHubId = 1;
2229 const char* DEVICE_LOCATION = "BLUETOOTH";
2230 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002231 FakePeripheralController& controller =
2232 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002233 mReader->pushNextDevice(device);
2234
2235 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2236
2237 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2238 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2239}
2240
2241TEST_F(InputReaderTest, BatteryGetStatus) {
2242 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002243 ftl::Flags<InputDeviceClass> deviceClass =
2244 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002245 constexpr int32_t eventHubId = 1;
2246 const char* DEVICE_LOCATION = "BLUETOOTH";
2247 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002248 FakePeripheralController& controller =
2249 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002250 mReader->pushNextDevice(device);
2251
2252 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2253
2254 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2255 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2256}
2257
Prabir Pradhane287ecd2022-09-07 21:18:05 +00002258TEST_F(InputReaderTest, BatteryGetDevicePath) {
2259 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2260 ftl::Flags<InputDeviceClass> deviceClass =
2261 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2262 constexpr int32_t eventHubId = 1;
2263 const char* DEVICE_LOCATION = "BLUETOOTH";
2264 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2265 device->addController<FakePeripheralController>(eventHubId);
2266 mReader->pushNextDevice(device);
2267
2268 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2269
2270 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), BATTERY_DEVPATH);
2271}
2272
Chris Ye3fdbfef2021-01-06 18:45:18 -08002273TEST_F(InputReaderTest, LightGetColor) {
2274 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002275 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002276 constexpr int32_t eventHubId = 1;
2277 const char* DEVICE_LOCATION = "BLUETOOTH";
2278 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002279 FakePeripheralController& controller =
2280 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002281 mReader->pushNextDevice(device);
2282 RawLightInfo info = {.id = 1,
2283 .name = "Mono",
2284 .maxBrightness = 255,
2285 .flags = InputLightClass::BRIGHTNESS,
2286 .path = ""};
2287 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2288 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2289
2290 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002291
Chris Yee2b1e5c2021-03-10 22:45:12 -08002292 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2293 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002294 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2295 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2296}
2297
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002298// --- InputReaderIntegrationTest ---
2299
2300// These tests create and interact with the InputReader only through its interface.
2301// The InputReader is started during SetUp(), which starts its processing in its own
2302// thread. The tests use linux uinput to emulate input devices.
2303// NOTE: Interacting with the physical device while these tests are running may cause
2304// the tests to fail.
2305class InputReaderIntegrationTest : public testing::Test {
2306protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002307 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002308 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002309 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002310
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002311 std::shared_ptr<FakePointerController> mFakePointerController;
2312
Chris Yea52ade12020-08-27 16:49:20 -07002313 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002314#if !defined(__ANDROID__)
2315 GTEST_SKIP();
2316#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002317 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002318 mFakePointerController = std::make_shared<FakePointerController>();
2319 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002320 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2321 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002322
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002323 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2324 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002325 ASSERT_EQ(mReader->start(), OK);
2326
2327 // Since this test is run on a real device, all the input devices connected
2328 // to the test device will show up in mReader. We wait for those input devices to
2329 // show up before beginning the tests.
2330 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2331 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2332 }
2333
Chris Yea52ade12020-08-27 16:49:20 -07002334 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002335#if !defined(__ANDROID__)
2336 return;
2337#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002338 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002339 mReader.reset();
2340 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002341 mFakePolicy.clear();
2342 }
2343};
2344
2345TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2346 // An invalid input device that is only used for this test.
2347 class InvalidUinputDevice : public UinputDevice {
2348 public:
2349 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2350
2351 private:
2352 void configureDevice(int fd, uinput_user_dev* device) override {}
2353 };
2354
2355 const size_t numDevices = mFakePolicy->getInputDevices().size();
2356
2357 // UinputDevice does not set any event or key bits, so InputReader should not
2358 // consider it as a valid device.
2359 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2360 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2361 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2362 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2363
2364 invalidDevice.reset();
2365 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2366 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2367 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2368}
2369
2370TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2371 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2372
2373 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2374 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2375 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2376 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2377
2378 // Find the test device by its name.
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002379 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
Chris Ye98d3f532020-10-01 21:48:59 -07002380 const auto& it =
2381 std::find_if(inputDevices.begin(), inputDevices.end(),
2382 [&keyboard](const InputDeviceInfo& info) {
2383 return info.getIdentifier().name == keyboard->getName();
2384 });
2385
2386 ASSERT_NE(it, inputDevices.end());
2387 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, it->getKeyboardType());
2388 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, it->getSources());
2389 ASSERT_EQ(0U, it->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002390
2391 keyboard.reset();
2392 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2393 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2394 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2395}
2396
2397TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2398 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2399 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2400
2401 NotifyConfigurationChangedArgs configChangedArgs;
2402 ASSERT_NO_FATAL_FAILURE(
2403 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002404 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002405 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2406
2407 NotifyKeyArgs keyArgs;
2408 keyboard->pressAndReleaseHomeKey();
2409 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2410 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002411 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002412 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002413 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002414 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002415 prevTimestamp = keyArgs.eventTime;
2416
2417 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2418 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002419 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002420 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002421 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002422}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002423
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002424/**
2425 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2426 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2427 * are passed to the listener.
2428 */
2429static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2430TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2431 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2432 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2433 NotifyKeyArgs keyArgs;
2434
2435 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2436 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2437 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2438 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2439
2440 controller->pressAndReleaseKey(BTN_GEAR_UP);
2441 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2442 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2443 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2444}
2445
Arthur Hungaab25622020-01-16 11:22:11 +08002446// --- TouchProcessTest ---
2447class TouchIntegrationTest : public InputReaderIntegrationTest {
2448protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002449 const std::string UNIQUE_ID = "local:0";
2450
Chris Yea52ade12020-08-27 16:49:20 -07002451 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002452#if !defined(__ANDROID__)
2453 GTEST_SKIP();
2454#endif
Arthur Hungaab25622020-01-16 11:22:11 +08002455 InputReaderIntegrationTest::SetUp();
2456 // At least add an internal display.
2457 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2458 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002459 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002460
2461 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2462 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2463 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2464 }
2465
2466 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2467 int32_t orientation, const std::string& uniqueId,
2468 std::optional<uint8_t> physicalPort,
2469 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002470 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2471 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002472 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2473 }
2474
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002475 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2476 NotifyMotionArgs args;
2477 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2478 EXPECT_EQ(action, args.action);
2479 ASSERT_EQ(points.size(), args.pointerCount);
2480 for (size_t i = 0; i < args.pointerCount; i++) {
2481 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2482 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2483 }
2484 }
2485
Arthur Hungaab25622020-01-16 11:22:11 +08002486 std::unique_ptr<UinputTouchScreen> mDevice;
2487};
2488
2489TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2490 NotifyMotionArgs args;
2491 const Point centerPoint = mDevice->getCenterPoint();
2492
2493 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002494 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002495 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002496 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002497 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2498 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2499
2500 // ACTION_MOVE
2501 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002502 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002503 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2504 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2505
2506 // ACTION_UP
2507 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002508 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002509 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2510 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2511}
2512
2513TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2514 NotifyMotionArgs args;
2515 const Point centerPoint = mDevice->getCenterPoint();
2516
2517 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002518 mDevice->sendSlot(FIRST_SLOT);
2519 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002520 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002521 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002522 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2523 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2524
2525 // ACTION_POINTER_DOWN (Second slot)
2526 const Point secondPoint = centerPoint + Point(100, 100);
2527 mDevice->sendSlot(SECOND_SLOT);
2528 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002529 mDevice->sendDown(secondPoint);
2530 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002531 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002532 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002533
2534 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002535 mDevice->sendMove(secondPoint + Point(1, 1));
2536 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002537 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2538 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2539
2540 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002541 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002542 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002543 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002544 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002545
2546 // ACTION_UP
2547 mDevice->sendSlot(FIRST_SLOT);
2548 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002549 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002550 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2551 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2552}
2553
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002554/**
2555 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2556 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2557 * data?
2558 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2559 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2560 * for Pointer 0 only is generated after.
2561 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2562 * events, we will not miss any information.
2563 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2564 * event generated afterwards that contains the newest movement of pointer 0.
2565 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2566 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2567 * losing information about non-palm pointers.
2568 */
2569TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2570 NotifyMotionArgs args;
2571 const Point centerPoint = mDevice->getCenterPoint();
2572
2573 // ACTION_DOWN
2574 mDevice->sendSlot(FIRST_SLOT);
2575 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2576 mDevice->sendDown(centerPoint);
2577 mDevice->sendSync();
2578 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2579
2580 // ACTION_POINTER_DOWN (Second slot)
2581 const Point secondPoint = centerPoint + Point(100, 100);
2582 mDevice->sendSlot(SECOND_SLOT);
2583 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2584 mDevice->sendDown(secondPoint);
2585 mDevice->sendSync();
2586 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2587
2588 // ACTION_MOVE (First slot)
2589 mDevice->sendSlot(FIRST_SLOT);
2590 mDevice->sendMove(centerPoint + Point(5, 5));
2591 // ACTION_POINTER_UP (Second slot)
2592 mDevice->sendSlot(SECOND_SLOT);
2593 mDevice->sendPointerUp();
2594 // Send a single sync for the above 2 pointer updates
2595 mDevice->sendSync();
2596
2597 // First, we should get POINTER_UP for the second pointer
2598 assertReceivedMotion(ACTION_POINTER_1_UP,
2599 {/*first pointer */ centerPoint + Point(5, 5),
2600 /*second pointer*/ secondPoint});
2601
2602 // Next, the MOVE event for the first pointer
2603 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2604}
2605
2606/**
2607 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2608 * move, and then it will go up, all in the same frame.
2609 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2610 * gets sent to the listener.
2611 */
2612TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2613 NotifyMotionArgs args;
2614 const Point centerPoint = mDevice->getCenterPoint();
2615
2616 // ACTION_DOWN
2617 mDevice->sendSlot(FIRST_SLOT);
2618 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2619 mDevice->sendDown(centerPoint);
2620 mDevice->sendSync();
2621 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2622
2623 // ACTION_POINTER_DOWN (Second slot)
2624 const Point secondPoint = centerPoint + Point(100, 100);
2625 mDevice->sendSlot(SECOND_SLOT);
2626 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2627 mDevice->sendDown(secondPoint);
2628 mDevice->sendSync();
2629 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2630
2631 // ACTION_MOVE (First slot)
2632 mDevice->sendSlot(FIRST_SLOT);
2633 mDevice->sendMove(centerPoint + Point(5, 5));
2634 // ACTION_POINTER_UP (Second slot)
2635 mDevice->sendSlot(SECOND_SLOT);
2636 mDevice->sendMove(secondPoint + Point(6, 6));
2637 mDevice->sendPointerUp();
2638 // Send a single sync for the above 2 pointer updates
2639 mDevice->sendSync();
2640
2641 // First, we should get POINTER_UP for the second pointer
2642 // The movement of the second pointer during the liftoff frame is ignored.
2643 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2644 assertReceivedMotion(ACTION_POINTER_1_UP,
2645 {/*first pointer */ centerPoint + Point(5, 5),
2646 /*second pointer*/ secondPoint});
2647
2648 // Next, the MOVE event for the first pointer
2649 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2650}
2651
Arthur Hungaab25622020-01-16 11:22:11 +08002652TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2653 NotifyMotionArgs args;
2654 const Point centerPoint = mDevice->getCenterPoint();
2655
2656 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002657 mDevice->sendSlot(FIRST_SLOT);
2658 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002659 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002660 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002661 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2662 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2663
arthurhungcc7f9802020-04-30 17:55:40 +08002664 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002665 const Point secondPoint = centerPoint + Point(100, 100);
2666 mDevice->sendSlot(SECOND_SLOT);
2667 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2668 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002669 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002670 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002671 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002672
arthurhungcc7f9802020-04-30 17:55:40 +08002673 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002674 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002675 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002676 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2677 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2678
arthurhungcc7f9802020-04-30 17:55:40 +08002679 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2680 // a palm event.
2681 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002682 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002683 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002684 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002685 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002686 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002687
arthurhungcc7f9802020-04-30 17:55:40 +08002688 // Send up to second slot, expect first slot send moving.
2689 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002690 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002691 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2692 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002693
arthurhungcc7f9802020-04-30 17:55:40 +08002694 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002695 mDevice->sendSlot(FIRST_SLOT);
2696 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002697 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002698
arthurhungcc7f9802020-04-30 17:55:40 +08002699 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2700 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002701}
2702
Michael Wrightd02c5b62014-02-10 15:10:22 -08002703// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002704class InputDeviceTest : public testing::Test {
2705protected:
2706 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002707 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708 static const int32_t DEVICE_ID;
2709 static const int32_t DEVICE_GENERATION;
2710 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002711 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002712 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002713
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002714 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002715 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002716 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002717 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002718 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002719
Chris Yea52ade12020-08-27 16:49:20 -07002720 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002721 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002722 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002723 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002724 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002725 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002726 InputDeviceIdentifier identifier;
2727 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002728 identifier.location = DEVICE_LOCATION;
arthurhungdcef2dc2020-08-11 14:47:50 +08002729 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002730 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002731 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002732 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002733 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002734 }
2735
Chris Yea52ade12020-08-27 16:49:20 -07002736 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002737 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002738 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002739 }
2740};
2741
2742const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002743const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002744const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002745const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2746const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002747const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002748 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002749const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002750
2751TEST_F(InputDeviceTest, ImmutableProperties) {
2752 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002753 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002754 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002755}
2756
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002757TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
2758 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2759
2760 // Configuration
2761 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2762 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002763 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002764
2765 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2766}
2767
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002768TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2769 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002770}
2771
Michael Wrightd02c5b62014-02-10 15:10:22 -08002772TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2773 // Configuration.
2774 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002775 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002776
2777 // Reset.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002778 unused += mDevice->reset(ARBITRARY_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002779
2780 NotifyDeviceResetArgs resetArgs;
2781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2782 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2783 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2784
2785 // Metadata.
2786 ASSERT_TRUE(mDevice->isIgnored());
2787 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2788
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002789 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002790 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002791 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002792 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2793 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2794
2795 // State queries.
2796 ASSERT_EQ(0, mDevice->getMetaState());
2797
2798 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2799 << "Ignored device should return unknown key code state.";
2800 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2801 << "Ignored device should return unknown scan code state.";
2802 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2803 << "Ignored device should return unknown switch state.";
2804
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002805 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002807 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808 << "Ignored device should never mark any key codes.";
2809 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2810 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2811}
2812
2813TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2814 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002815 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002816
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002817 FakeInputMapper& mapper1 =
2818 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002819 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2820 mapper1.setMetaState(AMETA_ALT_ON);
2821 mapper1.addSupportedKeyCode(AKEYCODE_A);
2822 mapper1.addSupportedKeyCode(AKEYCODE_B);
2823 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2824 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2825 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2826 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2827 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002828
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002829 FakeInputMapper& mapper2 =
2830 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002831 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002832
2833 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002834 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002835
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002836 std::string propertyValue;
2837 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002838 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002839 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002840
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002841 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2842 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002843
2844 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002845 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002846 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2847 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002848
2849 NotifyDeviceResetArgs resetArgs;
2850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2851 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2852 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2853
2854 // Metadata.
2855 ASSERT_FALSE(mDevice->isIgnored());
2856 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2857
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002858 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002859 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002860 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002861 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2862 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2863
2864 // State queries.
2865 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2866 << "Should query mappers and combine meta states.";
2867
2868 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2869 << "Should return unknown key code state when source not supported.";
2870 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2871 << "Should return unknown scan code state when source not supported.";
2872 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2873 << "Should return unknown switch state when source not supported.";
2874
2875 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2876 << "Should query mapper when source is supported.";
2877 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2878 << "Should query mapper when source is supported.";
2879 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2880 << "Should query mapper when source is supported.";
2881
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002882 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002883 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002884 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002885 << "Should do nothing when source is unsupported.";
2886 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2887 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2888 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2889 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2890
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002891 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002892 << "Should query mapper when source is supported.";
2893 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2894 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2895 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2896 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2897
2898 // Event handling.
2899 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002900 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002901 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002902
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002903 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2904 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002905}
2906
Arthur Hung2c9a3342019-07-23 14:18:59 +08002907// A single input device is associated with a specific display. Check that:
2908// 1. Device is disabled if the viewport corresponding to the associated display is not found
2909// 2. Device is disabled when setEnabled API is called
2910TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002911 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002912
2913 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002914 std::list<NotifyArgs> unused =
2915 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002916
2917 // Device should be enabled by default.
2918 ASSERT_TRUE(mDevice->isEnabled());
2919
2920 // Prepare associated info.
2921 constexpr uint8_t hdmi = 1;
2922 const std::string UNIQUE_ID = "local:1";
2923
2924 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002925 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2926 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002927 // Device should be disabled because it is associated with a specific display via
2928 // input port <-> display port association, but the corresponding display is not found
2929 ASSERT_FALSE(mDevice->isEnabled());
2930
2931 // Prepare displays.
2932 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002933 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2934 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002935 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2936 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002937 ASSERT_TRUE(mDevice->isEnabled());
2938
2939 // Device should be disabled after set disable.
2940 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002941 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2942 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002943 ASSERT_FALSE(mDevice->isEnabled());
2944
2945 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002946 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2947 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002948 ASSERT_FALSE(mDevice->isEnabled());
2949}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002950
Christine Franks1ba71cc2021-04-07 14:37:42 -07002951TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2952 // Device should be enabled by default.
2953 mFakePolicy->clearViewports();
2954 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002955 std::list<NotifyArgs> unused =
2956 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002957 ASSERT_TRUE(mDevice->isEnabled());
2958
2959 // Device should be disabled because it is associated with a specific display, but the
2960 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002961 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002962 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2963 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002964 ASSERT_FALSE(mDevice->isEnabled());
2965
2966 // Device should be enabled when a display is found.
2967 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2968 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2969 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002970 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2971 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002972 ASSERT_TRUE(mDevice->isEnabled());
2973
2974 // Device should be disabled after set disable.
2975 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002976 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2977 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002978 ASSERT_FALSE(mDevice->isEnabled());
2979
2980 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002981 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2982 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002983 ASSERT_FALSE(mDevice->isEnabled());
2984}
2985
Christine Franks2a2293c2022-01-18 11:51:16 -08002986TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2987 mFakePolicy->clearViewports();
2988 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002989 std::list<NotifyArgs> unused =
2990 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08002991
Christine Franks2a2293c2022-01-18 11:51:16 -08002992 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2993 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2994 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2995 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002996 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2997 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002998 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2999}
3000
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07003001/**
3002 * This test reproduces a crash caused by a dangling reference that remains after device is added
3003 * and removed. The reference is accessed in InputDevice::dump(..);
3004 */
3005TEST_F(InputDeviceTest, DumpDoesNotCrash) {
3006 constexpr int32_t TEST_EVENTHUB_ID = 10;
3007 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
3008
3009 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
3010 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
3011 device.removeEventHubDevice(TEST_EVENTHUB_ID);
3012 std::string dumpStr, eventHubDevStr;
3013 device.dump(dumpStr, eventHubDevStr);
3014}
3015
Michael Wrightd02c5b62014-02-10 15:10:22 -08003016// --- InputMapperTest ---
3017
3018class InputMapperTest : public testing::Test {
3019protected:
3020 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003021 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003022 static const int32_t DEVICE_ID;
3023 static const int32_t DEVICE_GENERATION;
3024 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003025 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003026 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003027
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003028 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003029 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003030 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08003031 std::unique_ptr<InstrumentedInputReader> mReader;
3032 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003033
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003034 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003035 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003036 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003037 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08003038 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003039 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08003040 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003041 // Consume the device reset notification generated when adding a new device.
3042 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003043 }
3044
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003045 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003046 SetUp(DEVICE_CLASSES);
3047 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003048
Chris Yea52ade12020-08-27 16:49:20 -07003049 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003050 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003051 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003052 }
3053
3054 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003055 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003056 }
3057
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003058 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003059 if (!changes ||
3060 (changes &
3061 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3062 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003063 mReader->requestRefreshConfiguration(changes);
3064 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003065 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003066 std::list<NotifyArgs> out =
3067 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003068 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003069 for (const NotifyArgs& args : out) {
3070 mFakeListener->notify(args);
3071 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003072 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003073 return out;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003074 }
3075
arthurhungdcef2dc2020-08-11 14:47:50 +08003076 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3077 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003078 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003079 InputDeviceIdentifier identifier;
3080 identifier.name = name;
3081 identifier.location = location;
3082 std::shared_ptr<InputDevice> device =
3083 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3084 identifier);
3085 mReader->pushNextDevice(device);
3086 mFakeEventHub->addDevice(eventHubId, name, classes);
3087 mReader->loopOnce();
3088 return device;
3089 }
3090
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003091 template <class T, typename... Args>
3092 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003093 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003094 configureDevice(0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003095 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
3096 resetArgList += mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003097 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003098 for (const NotifyArgs& loopArgs : resetArgList) {
3099 mFakeListener->notify(loopArgs);
3100 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003101 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003102 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103 }
3104
3105 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003106 int32_t orientation, const std::string& uniqueId,
3107 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003108 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3109 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003110 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3111 }
3112
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003113 void clearViewports() {
3114 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003115 }
3116
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003117 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
3118 int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003119 RawEvent event;
3120 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003121 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003122 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003123 event.type = type;
3124 event.code = code;
3125 event.value = value;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003126 std::list<NotifyArgs> processArgList = mapper.process(&event);
3127 for (const NotifyArgs& args : processArgList) {
3128 mFakeListener->notify(args);
3129 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003130 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003131 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003132 return processArgList;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003133 }
3134
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00003135 void resetMapper(InputMapper& mapper, nsecs_t when) {
3136 const auto resetArgs = mapper.reset(when);
3137 for (const auto args : resetArgs) {
3138 mFakeListener->notify(args);
3139 }
3140 // Loop the reader to flush the input listener queue.
3141 mReader->loopOnce();
3142 }
3143
Michael Wrightd02c5b62014-02-10 15:10:22 -08003144 static void assertMotionRange(const InputDeviceInfo& info,
3145 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3146 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003147 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003148 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3149 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3150 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3151 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3152 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3153 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3154 }
3155
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003156 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3157 float size, float touchMajor, float touchMinor, float toolMajor,
3158 float toolMinor, float orientation, float distance,
3159 float scaledAxisEpsilon = 1.f) {
3160 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3161 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003162 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3163 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003164 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3165 scaledAxisEpsilon);
3166 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3167 scaledAxisEpsilon);
3168 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3169 scaledAxisEpsilon);
3170 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3171 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003172 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3173 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3174 }
3175
Michael Wright17db18e2020-06-26 20:51:44 +01003176 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003177 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003178 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003179 ASSERT_NEAR(x, actualX, 1);
3180 ASSERT_NEAR(y, actualY, 1);
3181 }
3182};
3183
3184const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003185const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003186const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003187const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3188const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003189const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3190 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003191const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003192
3193// --- SwitchInputMapperTest ---
3194
3195class SwitchInputMapperTest : public InputMapperTest {
3196protected:
3197};
3198
3199TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003200 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003201
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003202 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003203}
3204
3205TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003206 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003207
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003208 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003209 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003210
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003211 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003212 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003213}
3214
3215TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003216 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003217 std::list<NotifyArgs> out;
3218 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3219 ASSERT_TRUE(out.empty());
3220 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3221 ASSERT_TRUE(out.empty());
3222 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3223 ASSERT_TRUE(out.empty());
3224 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003225
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003226 ASSERT_EQ(1u, out.size());
3227 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003228 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003229 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3230 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003231 args.switchMask);
3232 ASSERT_EQ(uint32_t(0), args.policyFlags);
3233}
3234
Chris Ye87143712020-11-10 05:05:58 +00003235// --- VibratorInputMapperTest ---
3236class VibratorInputMapperTest : public InputMapperTest {
3237protected:
3238 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3239};
3240
3241TEST_F(VibratorInputMapperTest, GetSources) {
3242 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3243
3244 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3245}
3246
3247TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3248 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3249
3250 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3251}
3252
3253TEST_F(VibratorInputMapperTest, Vibrate) {
3254 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003255 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003256 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3257
3258 VibrationElement pattern(2);
3259 VibrationSequence sequence(2);
3260 pattern.duration = std::chrono::milliseconds(200);
3261 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3262 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3263 sequence.addElement(pattern);
3264 pattern.duration = std::chrono::milliseconds(500);
3265 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3266 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3267 sequence.addElement(pattern);
3268
3269 std::vector<int64_t> timings = {0, 1};
3270 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3271
3272 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003273 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003274 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003275 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003276 // Verify vibrator state listener was notified.
3277 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003278 ASSERT_EQ(1u, out.size());
3279 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3280 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3281 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003282 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003283 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003284 ASSERT_FALSE(mapper.isVibrating());
3285 // Verify vibrator state listener was notified.
3286 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003287 ASSERT_EQ(1u, out.size());
3288 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3289 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3290 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003291}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003292
Chris Yef59a2f42020-10-16 12:55:26 -07003293// --- SensorInputMapperTest ---
3294
3295class SensorInputMapperTest : public InputMapperTest {
3296protected:
3297 static const int32_t ACCEL_RAW_MIN;
3298 static const int32_t ACCEL_RAW_MAX;
3299 static const int32_t ACCEL_RAW_FUZZ;
3300 static const int32_t ACCEL_RAW_FLAT;
3301 static const int32_t ACCEL_RAW_RESOLUTION;
3302
3303 static const int32_t GYRO_RAW_MIN;
3304 static const int32_t GYRO_RAW_MAX;
3305 static const int32_t GYRO_RAW_FUZZ;
3306 static const int32_t GYRO_RAW_FLAT;
3307 static const int32_t GYRO_RAW_RESOLUTION;
3308
3309 static const float GRAVITY_MS2_UNIT;
3310 static const float DEGREE_RADIAN_UNIT;
3311
3312 void prepareAccelAxes();
3313 void prepareGyroAxes();
3314 void setAccelProperties();
3315 void setGyroProperties();
3316 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3317};
3318
3319const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3320const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3321const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3322const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3323const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3324
3325const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3326const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3327const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3328const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3329const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3330
3331const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3332const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3333
3334void SensorInputMapperTest::prepareAccelAxes() {
3335 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3336 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3337 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3338 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3339 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3340 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3341}
3342
3343void SensorInputMapperTest::prepareGyroAxes() {
3344 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3345 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3346 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3347 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3348 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3349 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3350}
3351
3352void SensorInputMapperTest::setAccelProperties() {
3353 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3354 /* sensorDataIndex */ 0);
3355 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3356 /* sensorDataIndex */ 1);
3357 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3358 /* sensorDataIndex */ 2);
3359 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3360 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3361 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3362 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3363 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3364}
3365
3366void SensorInputMapperTest::setGyroProperties() {
3367 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3368 /* sensorDataIndex */ 0);
3369 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3370 /* sensorDataIndex */ 1);
3371 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3372 /* sensorDataIndex */ 2);
3373 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3374 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3375 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3376 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3377 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3378}
3379
3380TEST_F(SensorInputMapperTest, GetSources) {
3381 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3382
3383 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3384}
3385
3386TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3387 setAccelProperties();
3388 prepareAccelAxes();
3389 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3390
3391 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3392 std::chrono::microseconds(10000),
3393 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003394 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003395 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3396 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3397 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3398 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003400
3401 NotifySensorArgs args;
3402 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3403 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3404 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3405
3406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3407 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3408 ASSERT_EQ(args.deviceId, DEVICE_ID);
3409 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3410 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3411 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3412 ASSERT_EQ(args.values, values);
3413 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3414}
3415
3416TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3417 setGyroProperties();
3418 prepareGyroAxes();
3419 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3420
3421 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3422 std::chrono::microseconds(10000),
3423 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003424 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3427 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003430
3431 NotifySensorArgs args;
3432 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3433 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3434 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3435
3436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3437 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3438 ASSERT_EQ(args.deviceId, DEVICE_ID);
3439 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3440 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3441 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3442 ASSERT_EQ(args.values, values);
3443 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3444}
3445
Michael Wrightd02c5b62014-02-10 15:10:22 -08003446// --- KeyboardInputMapperTest ---
3447
3448class KeyboardInputMapperTest : public InputMapperTest {
3449protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003450 const std::string UNIQUE_ID = "local:0";
3451
3452 void prepareDisplay(int32_t orientation);
3453
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003454 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003455 int32_t originalKeyCode, int32_t rotatedKeyCode,
3456 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003457};
3458
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003459/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3460 * orientation.
3461 */
3462void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003463 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3464 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003465}
3466
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003467void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003468 int32_t originalScanCode, int32_t originalKeyCode,
3469 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003470 NotifyKeyArgs args;
3471
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003472 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3474 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3475 ASSERT_EQ(originalScanCode, args.scanCode);
3476 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003477 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003478
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003479 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3481 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3482 ASSERT_EQ(originalScanCode, args.scanCode);
3483 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003484 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003485}
3486
Michael Wrightd02c5b62014-02-10 15:10:22 -08003487TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003488 KeyboardInputMapper& mapper =
3489 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3490 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003491
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003492 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003493}
3494
3495TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3496 const int32_t USAGE_A = 0x070004;
3497 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003498 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3499 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003500 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3501 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3502 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003504 KeyboardInputMapper& mapper =
3505 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3506 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003507 // Initial metastate is AMETA_NONE.
3508 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003509
3510 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003511 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003512 NotifyKeyArgs args;
3513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3514 ASSERT_EQ(DEVICE_ID, args.deviceId);
3515 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3516 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3517 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3518 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3519 ASSERT_EQ(KEY_HOME, args.scanCode);
3520 ASSERT_EQ(AMETA_NONE, args.metaState);
3521 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3522 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3523 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3524
3525 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003526 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3528 ASSERT_EQ(DEVICE_ID, args.deviceId);
3529 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3530 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3531 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3532 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3533 ASSERT_EQ(KEY_HOME, args.scanCode);
3534 ASSERT_EQ(AMETA_NONE, args.metaState);
3535 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3536 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3537 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3538
3539 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3543 ASSERT_EQ(DEVICE_ID, args.deviceId);
3544 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3545 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3546 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3547 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3548 ASSERT_EQ(0, args.scanCode);
3549 ASSERT_EQ(AMETA_NONE, args.metaState);
3550 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3551 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3552 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3553
3554 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003555 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3556 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3558 ASSERT_EQ(DEVICE_ID, args.deviceId);
3559 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3560 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3561 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3562 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3563 ASSERT_EQ(0, args.scanCode);
3564 ASSERT_EQ(AMETA_NONE, args.metaState);
3565 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3566 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3567 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3568
3569 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003570 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3573 ASSERT_EQ(DEVICE_ID, args.deviceId);
3574 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3575 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3576 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3577 ASSERT_EQ(0, args.keyCode);
3578 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3579 ASSERT_EQ(AMETA_NONE, args.metaState);
3580 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3581 ASSERT_EQ(0U, args.policyFlags);
3582 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3583
3584 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3586 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3588 ASSERT_EQ(DEVICE_ID, args.deviceId);
3589 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3590 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3591 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3592 ASSERT_EQ(0, args.keyCode);
3593 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3594 ASSERT_EQ(AMETA_NONE, args.metaState);
3595 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3596 ASSERT_EQ(0U, args.policyFlags);
3597 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3598}
3599
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003600/**
3601 * Ensure that the readTime is set to the time when the EV_KEY is received.
3602 */
3603TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3604 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3605
3606 KeyboardInputMapper& mapper =
3607 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3608 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3609 NotifyKeyArgs args;
3610
3611 // Key down
3612 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3614 ASSERT_EQ(12, args.readTime);
3615
3616 // Key up
3617 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3619 ASSERT_EQ(15, args.readTime);
3620}
3621
Michael Wrightd02c5b62014-02-10 15:10:22 -08003622TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003623 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3624 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003625 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3626 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3627 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003628
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003629 KeyboardInputMapper& mapper =
3630 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3631 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003632
Arthur Hung95f68612022-04-07 14:08:22 +08003633 // Initial metastate is AMETA_NONE.
3634 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003635
3636 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003637 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003638 NotifyKeyArgs args;
3639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3640 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003641 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003642 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003643
3644 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003645 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3647 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003648 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003649
3650 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003651 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3653 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003654 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003655
3656 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003657 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3659 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003660 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003661 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003662}
3663
3664TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003665 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3666 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3667 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3668 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003669
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003670 KeyboardInputMapper& mapper =
3671 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3672 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003673
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003674 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003675 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3676 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3677 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3678 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3679 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3680 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3681 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3682 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3683}
3684
3685TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003686 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3687 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3688 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3689 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003690
Michael Wrightd02c5b62014-02-10 15:10:22 -08003691 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003692 KeyboardInputMapper& mapper =
3693 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3694 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003695
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003696 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003697 ASSERT_NO_FATAL_FAILURE(
3698 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3699 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3700 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3701 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3702 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3703 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3704 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003705
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003706 clearViewports();
3707 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003708 ASSERT_NO_FATAL_FAILURE(
3709 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3710 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3711 AKEYCODE_DPAD_UP, DISPLAY_ID));
3712 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3713 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3714 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3715 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003716
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003717 clearViewports();
3718 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003719 ASSERT_NO_FATAL_FAILURE(
3720 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3721 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3722 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3723 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3724 AKEYCODE_DPAD_UP, DISPLAY_ID));
3725 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3726 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003727
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003728 clearViewports();
3729 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003730 ASSERT_NO_FATAL_FAILURE(
3731 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3732 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3733 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3734 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3735 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3736 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3737 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003738
3739 // Special case: if orientation changes while key is down, we still emit the same keycode
3740 // in the key up as we did in the key down.
3741 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003742 clearViewports();
3743 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003744 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3746 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3747 ASSERT_EQ(KEY_UP, args.scanCode);
3748 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3749
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003750 clearViewports();
3751 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003752 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3754 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3755 ASSERT_EQ(KEY_UP, args.scanCode);
3756 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3757}
3758
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003759TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3760 // If the keyboard is not orientation aware,
3761 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003762 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003763
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003764 KeyboardInputMapper& mapper =
3765 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3766 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003767 NotifyKeyArgs args;
3768
3769 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003772 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3774 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3775
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003776 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3781 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3782}
3783
3784TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3785 // If the keyboard is orientation aware,
3786 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003787 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003788
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003789 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003790 KeyboardInputMapper& mapper =
3791 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3792 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003793 NotifyKeyArgs args;
3794
3795 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3796 // ^--- already checked by the previous test
3797
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003798 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003799 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3804 ASSERT_EQ(DISPLAY_ID, args.displayId);
3805
3806 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003807 clearViewports();
3808 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003809 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003810 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003812 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3814 ASSERT_EQ(newDisplayId, args.displayId);
3815}
3816
Michael Wrightd02c5b62014-02-10 15:10:22 -08003817TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003818 KeyboardInputMapper& mapper =
3819 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3820 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003821
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003822 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003823 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003824
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003825 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003826 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003827}
3828
Philip Junker4af3b3d2021-12-14 10:36:55 +01003829TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3830 KeyboardInputMapper& mapper =
3831 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3832 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3833
3834 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3835 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3836 << "If a mapping is available, the result is equal to the mapping";
3837
3838 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3839 << "If no mapping is available, the result is the key location";
3840}
3841
Michael Wrightd02c5b62014-02-10 15:10:22 -08003842TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003843 KeyboardInputMapper& mapper =
3844 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3845 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003846
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003847 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003848 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003849
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003850 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003851 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003852}
3853
3854TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003855 KeyboardInputMapper& mapper =
3856 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3857 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003859 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003860
Michael Wrightd02c5b62014-02-10 15:10:22 -08003861 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003862 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003863 ASSERT_TRUE(flags[0]);
3864 ASSERT_FALSE(flags[1]);
3865}
3866
3867TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003868 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3869 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3870 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3871 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3872 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3873 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003874
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003875 KeyboardInputMapper& mapper =
3876 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3877 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003878 // Initial metastate is AMETA_NONE.
3879 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003880
3881 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003882 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3883 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3884 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003885
3886 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003887 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3888 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003889 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3890 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3891 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003892 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003893
3894 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003895 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3896 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003897 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3898 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3899 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003900 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003901
3902 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003903 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3904 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003905 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3906 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3907 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003908 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003909
3910 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003911 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3912 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003913 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3914 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3915 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003916 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003917
3918 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003919 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3920 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003921 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3922 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3923 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003924 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003925
3926 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003927 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3928 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003929 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3930 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3931 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003932 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003933}
3934
Chris Yea52ade12020-08-27 16:49:20 -07003935TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3936 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3937 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3938 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3939 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3940
3941 KeyboardInputMapper& mapper =
3942 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3943 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3944
Chris Yea52ade12020-08-27 16:49:20 -07003945 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003946 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003947 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3948 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3949 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3950 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3951
3952 NotifyKeyArgs args;
3953 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003954 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3956 ASSERT_EQ(AMETA_NONE, args.metaState);
3957 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3958 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3959 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3960
3961 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003962 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3964 ASSERT_EQ(AMETA_NONE, args.metaState);
3965 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3966 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3967 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3968}
3969
Arthur Hung2c9a3342019-07-23 14:18:59 +08003970TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3971 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003972 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3973 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3974 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3975 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003976
3977 // keyboard 2.
3978 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003979 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003980 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003981 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003982 std::shared_ptr<InputDevice> device2 =
3983 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003984 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003985
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003986 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3987 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3988 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3989 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003990
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003991 KeyboardInputMapper& mapper =
3992 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3993 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003994
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003995 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003996 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003997 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003998 std::list<NotifyArgs> unused =
3999 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4000 0 /*changes*/);
4001 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004002
4003 // Prepared displays and associated info.
4004 constexpr uint8_t hdmi1 = 0;
4005 constexpr uint8_t hdmi2 = 1;
4006 const std::string SECONDARY_UNIQUE_ID = "local:1";
4007
4008 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
4009 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
4010
4011 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004012 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4013 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004014 ASSERT_FALSE(device2->isEnabled());
4015
4016 // Prepare second display.
4017 constexpr int32_t newDisplayId = 2;
4018 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004019 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004020 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004021 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004022 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004023 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4024 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004025
4026 // Device should be enabled after the associated display is found.
4027 ASSERT_TRUE(mDevice->isEnabled());
4028 ASSERT_TRUE(device2->isEnabled());
4029
4030 // Test pad key events
4031 ASSERT_NO_FATAL_FAILURE(
4032 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4033 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4034 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4035 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4036 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4037 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4038 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4039
4040 ASSERT_NO_FATAL_FAILURE(
4041 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
4042 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4043 AKEYCODE_DPAD_RIGHT, newDisplayId));
4044 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4045 AKEYCODE_DPAD_DOWN, newDisplayId));
4046 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4047 AKEYCODE_DPAD_LEFT, newDisplayId));
4048}
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049
arthurhungc903df12020-08-11 15:08:42 +08004050TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
4051 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4052 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4053 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4054 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4055 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4056 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4057
4058 KeyboardInputMapper& mapper =
4059 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4060 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004061 // Initial metastate is AMETA_NONE.
4062 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004063
4064 // Initialization should have turned all of the lights off.
4065 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4066 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4067 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4068
4069 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004070 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004072 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4073 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4074
4075 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004076 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004078 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4079 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
4080
4081 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004082 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004084 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4085 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4086
4087 mFakeEventHub->removeDevice(EVENTHUB_ID);
4088 mReader->loopOnce();
4089
4090 // keyboard 2 should default toggle keys.
4091 const std::string USB2 = "USB2";
4092 const std::string DEVICE_NAME2 = "KEYBOARD2";
4093 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4094 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4095 std::shared_ptr<InputDevice> device2 =
4096 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004097 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004098 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4099 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4100 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4101 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4102 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4103 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4104
arthurhung6fe95782020-10-05 22:41:16 +08004105 KeyboardInputMapper& mapper2 =
4106 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4107 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004108 std::list<NotifyArgs> unused =
4109 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4110 0 /*changes*/);
4111 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08004112
4113 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4114 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4115 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004116 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4117 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004118}
4119
Arthur Hungcb40a002021-08-03 14:31:01 +00004120TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4121 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4122 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4123 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4124
4125 // Suppose we have two mappers. (DPAD + KEYBOARD)
4126 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4127 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4128 KeyboardInputMapper& mapper =
4129 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4130 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004131 // Initial metastate is AMETA_NONE.
4132 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004133
4134 mReader->toggleCapsLockState(DEVICE_ID);
4135 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4136}
4137
Arthur Hungfb3cc112022-04-13 07:39:50 +00004138TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4139 // keyboard 1.
4140 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4141 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4142 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4143 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4144 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4145 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4146
4147 KeyboardInputMapper& mapper1 =
4148 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4149 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4150
4151 // keyboard 2.
4152 const std::string USB2 = "USB2";
4153 const std::string DEVICE_NAME2 = "KEYBOARD2";
4154 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4155 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4156 std::shared_ptr<InputDevice> device2 =
4157 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4158 ftl::Flags<InputDeviceClass>(0));
4159 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4160 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4161 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4162 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4163 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4164 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4165
4166 KeyboardInputMapper& mapper2 =
4167 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4168 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004169 std::list<NotifyArgs> unused =
4170 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4171 0 /*changes*/);
4172 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004173
Arthur Hung95f68612022-04-07 14:08:22 +08004174 // Initial metastate is AMETA_NONE.
4175 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4176 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4177
4178 // Toggle num lock on and off.
4179 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4180 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004181 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4182 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4183 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4184
4185 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4186 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4187 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4188 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4189 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4190
4191 // Toggle caps lock on and off.
4192 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4193 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4194 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4195 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4196 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4197
4198 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4199 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4200 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4201 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4202 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4203
4204 // Toggle scroll lock on and off.
4205 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4206 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4207 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4208 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4209 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4210
4211 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4212 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4213 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4214 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4215 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4216}
4217
Arthur Hung2141d542022-08-23 07:45:21 +00004218TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4219 const int32_t USAGE_A = 0x070004;
4220 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4221 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4222
4223 KeyboardInputMapper& mapper =
4224 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4225 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4226 // Key down by scan code.
4227 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4228 NotifyKeyArgs args;
4229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4230 ASSERT_EQ(DEVICE_ID, args.deviceId);
4231 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4232 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4233 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4234 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4235 ASSERT_EQ(KEY_HOME, args.scanCode);
4236 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4237
4238 // Disable device, it should synthesize cancellation events for down events.
4239 mFakePolicy->addDisabledDevice(DEVICE_ID);
4240 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4241
4242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4243 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4244 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4245 ASSERT_EQ(KEY_HOME, args.scanCode);
4246 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4247}
4248
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004249// --- KeyboardInputMapperTest_ExternalDevice ---
4250
4251class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4252protected:
Chris Yea52ade12020-08-27 16:49:20 -07004253 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004254};
4255
4256TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004257 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4258 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004259
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004260 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4261 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4262 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4263 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004264
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004265 KeyboardInputMapper& mapper =
4266 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4267 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004268
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004269 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004270 NotifyKeyArgs args;
4271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4272 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4273
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004274 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4276 ASSERT_EQ(uint32_t(0), args.policyFlags);
4277
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004278 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4280 ASSERT_EQ(uint32_t(0), args.policyFlags);
4281
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004282 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4284 ASSERT_EQ(uint32_t(0), args.policyFlags);
4285
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004286 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4288 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4289
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004290 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4292 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4293}
4294
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004295TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004296 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004297
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004298 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4299 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4300 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004301
Powei Fengd041c5d2019-05-03 17:11:33 -07004302 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004303 KeyboardInputMapper& mapper =
4304 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4305 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004306
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004308 NotifyKeyArgs args;
4309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4310 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4311
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004312 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4314 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4315
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4318 ASSERT_EQ(uint32_t(0), args.policyFlags);
4319
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004320 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4322 ASSERT_EQ(uint32_t(0), args.policyFlags);
4323
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004324 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4326 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4327
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004328 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4330 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4331}
4332
Michael Wrightd02c5b62014-02-10 15:10:22 -08004333// --- CursorInputMapperTest ---
4334
4335class CursorInputMapperTest : public InputMapperTest {
4336protected:
4337 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4338
Michael Wright17db18e2020-06-26 20:51:44 +01004339 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004340
Chris Yea52ade12020-08-27 16:49:20 -07004341 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004342 InputMapperTest::SetUp();
4343
Michael Wright17db18e2020-06-26 20:51:44 +01004344 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004345 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004346 }
4347
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004348 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4349 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004350
4351 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004352 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4353 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4354 }
4355
4356 void prepareSecondaryDisplay() {
4357 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4358 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4359 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004360 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004361
4362 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4363 float pressure) {
4364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4365 0.0f, 0.0f, 0.0f, EPSILON));
4366 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004367};
4368
4369const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4370
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004371void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4372 int32_t originalY, int32_t rotatedX,
4373 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374 NotifyMotionArgs args;
4375
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
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(&args));
4380 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004381 ASSERT_NO_FATAL_FAILURE(
4382 assertCursorPointerCoords(args.pointerCoords[0],
4383 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4384 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004385}
4386
4387TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004388 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004389 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004390
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004391 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392}
4393
4394TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004395 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004396 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004397
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004398 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004399}
4400
4401TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004402 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004403 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004404
4405 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004406 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407
4408 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004409 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4410 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4412 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4413
4414 // When the bounds are set, then there should be a valid motion range.
4415 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4416
4417 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004418 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004419
4420 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4421 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4422 1, 800 - 1, 0.0f, 0.0f));
4423 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4424 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4425 2, 480 - 1, 0.0f, 0.0f));
4426 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4427 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4428 0.0f, 1.0f, 0.0f, 0.0f));
4429}
4430
4431TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004433 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434
4435 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004436 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004437
4438 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4439 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4440 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4441 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4442 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4443 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4444 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4445 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4446 0.0f, 1.0f, 0.0f, 0.0f));
4447}
4448
4449TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004451 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452
arthurhungdcef2dc2020-08-11 14:47:50 +08004453 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454
4455 NotifyMotionArgs args;
4456
4457 // Button press.
4458 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
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(&args));
4462 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4463 ASSERT_EQ(DEVICE_ID, args.deviceId);
4464 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4465 ASSERT_EQ(uint32_t(0), args.policyFlags);
4466 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4467 ASSERT_EQ(0, args.flags);
4468 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4469 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4470 ASSERT_EQ(0, args.edgeFlags);
4471 ASSERT_EQ(uint32_t(1), args.pointerCount);
4472 ASSERT_EQ(0, args.pointerProperties[0].id);
4473 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004474 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4476 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4477 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4478
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4480 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4481 ASSERT_EQ(DEVICE_ID, args.deviceId);
4482 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4483 ASSERT_EQ(uint32_t(0), args.policyFlags);
4484 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4485 ASSERT_EQ(0, args.flags);
4486 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4487 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4488 ASSERT_EQ(0, args.edgeFlags);
4489 ASSERT_EQ(uint32_t(1), args.pointerCount);
4490 ASSERT_EQ(0, args.pointerProperties[0].id);
4491 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004492 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004493 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4494 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4495 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4496
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004498 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4499 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4501 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4502 ASSERT_EQ(DEVICE_ID, args.deviceId);
4503 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4504 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004505 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4506 ASSERT_EQ(0, args.flags);
4507 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4508 ASSERT_EQ(0, args.buttonState);
4509 ASSERT_EQ(0, args.edgeFlags);
4510 ASSERT_EQ(uint32_t(1), args.pointerCount);
4511 ASSERT_EQ(0, args.pointerProperties[0].id);
4512 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004513 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004514 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4515 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4516 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4517
4518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4519 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4520 ASSERT_EQ(DEVICE_ID, args.deviceId);
4521 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4522 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4524 ASSERT_EQ(0, args.flags);
4525 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4526 ASSERT_EQ(0, args.buttonState);
4527 ASSERT_EQ(0, args.edgeFlags);
4528 ASSERT_EQ(uint32_t(1), args.pointerCount);
4529 ASSERT_EQ(0, args.pointerProperties[0].id);
4530 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004531 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004532 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4533 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4534 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4535}
4536
4537TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004538 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004539 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004540
4541 NotifyMotionArgs args;
4542
4543 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4547 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004548 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4549 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4550 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004551
4552 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004553 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4554 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4556 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004557 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4558 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004559}
4560
4561TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004562 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004563 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564
4565 NotifyMotionArgs args;
4566
4567 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004568 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4571 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004572 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4575 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004576 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004577
Michael Wrightd02c5b62014-02-10 15:10:22 -08004578 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004579 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4580 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004582 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004583 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004584
4585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004586 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004587 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588}
4589
4590TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004592 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004593
4594 NotifyMotionArgs args;
4595
4596 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004597 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4598 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4599 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4600 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4602 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004603 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4604 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4605 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004606
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4608 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004609 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4610 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4611 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004612
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4615 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4616 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004619 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4620 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4621 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004622
4623 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004624 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4625 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004627 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004628 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004629
4630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004631 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004632 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004633}
4634
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004635TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004636 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004637 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004638 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4639 // need to be rotated.
4640 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004641 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004642
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004643 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004644 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4645 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4646 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4647 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4648 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4649 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4650 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4651 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4652}
4653
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004654TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004655 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004656 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004657 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4658 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004659 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004660
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004661 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004662 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4664 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4665 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4666 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4667 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4668 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4669 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4670 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4671
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004672 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004673 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004674 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4675 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4676 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4677 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4678 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4679 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4680 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4681 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004682
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004683 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004684 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004685 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4686 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4687 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4688 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4689 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4690 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4691 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4692 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4693
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004694 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004695 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004696 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4697 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4698 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4699 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4700 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4701 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4702 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4703 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004704}
4705
4706TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004707 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004708 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004709
4710 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4711 mFakePointerController->setPosition(100, 200);
4712 mFakePointerController->setButtonState(0);
4713
4714 NotifyMotionArgs motionArgs;
4715 NotifyKeyArgs keyArgs;
4716
4717 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004718 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4719 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4721 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4722 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4723 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004724 ASSERT_NO_FATAL_FAILURE(
4725 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004726
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4728 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4729 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4730 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004731 ASSERT_NO_FATAL_FAILURE(
4732 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004733
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004734 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4735 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004737 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004738 ASSERT_EQ(0, motionArgs.buttonState);
4739 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004740 ASSERT_NO_FATAL_FAILURE(
4741 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004742
4743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004744 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004745 ASSERT_EQ(0, motionArgs.buttonState);
4746 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004747 ASSERT_NO_FATAL_FAILURE(
4748 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004749
4750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004751 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004752 ASSERT_EQ(0, motionArgs.buttonState);
4753 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004754 ASSERT_NO_FATAL_FAILURE(
4755 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004756
4757 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004758 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4760 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4762 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4763 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4764 motionArgs.buttonState);
4765 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4766 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004767 ASSERT_NO_FATAL_FAILURE(
4768 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4771 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4772 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4773 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4774 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004775 ASSERT_NO_FATAL_FAILURE(
4776 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004777
4778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4779 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4780 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4781 motionArgs.buttonState);
4782 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4783 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004784 ASSERT_NO_FATAL_FAILURE(
4785 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004786
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004790 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004791 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4792 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004793 ASSERT_NO_FATAL_FAILURE(
4794 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004795
4796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004797 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004798 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4799 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004800 ASSERT_NO_FATAL_FAILURE(
4801 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004802
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4804 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004806 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4807 ASSERT_EQ(0, motionArgs.buttonState);
4808 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004809 ASSERT_NO_FATAL_FAILURE(
4810 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004811 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4812 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004813
4814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004815 ASSERT_EQ(0, motionArgs.buttonState);
4816 ASSERT_EQ(0, mFakePointerController->getButtonState());
4817 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004818 ASSERT_NO_FATAL_FAILURE(
4819 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004820
Michael Wrightd02c5b62014-02-10 15:10:22 -08004821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4822 ASSERT_EQ(0, motionArgs.buttonState);
4823 ASSERT_EQ(0, mFakePointerController->getButtonState());
4824 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004825 ASSERT_NO_FATAL_FAILURE(
4826 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004827
4828 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4832 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4833 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004834
Michael Wrightd02c5b62014-02-10 15:10:22 -08004835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004836 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004837 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4838 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004839 ASSERT_NO_FATAL_FAILURE(
4840 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004841
4842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4843 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4844 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4845 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004846 ASSERT_NO_FATAL_FAILURE(
4847 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004849 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4850 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004852 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853 ASSERT_EQ(0, motionArgs.buttonState);
4854 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004855 ASSERT_NO_FATAL_FAILURE(
4856 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004857
4858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004859 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004860 ASSERT_EQ(0, motionArgs.buttonState);
4861 ASSERT_EQ(0, mFakePointerController->getButtonState());
4862
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004863 ASSERT_NO_FATAL_FAILURE(
4864 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4866 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4867 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4868
4869 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004870 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4871 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4873 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4874 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004875
Michael Wrightd02c5b62014-02-10 15:10:22 -08004876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004877 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004878 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4879 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004880 ASSERT_NO_FATAL_FAILURE(
4881 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004882
4883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4884 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4885 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4886 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004887 ASSERT_NO_FATAL_FAILURE(
4888 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004889
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004890 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4891 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004893 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004894 ASSERT_EQ(0, motionArgs.buttonState);
4895 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004896 ASSERT_NO_FATAL_FAILURE(
4897 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004898
4899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4900 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4901 ASSERT_EQ(0, motionArgs.buttonState);
4902 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004903 ASSERT_NO_FATAL_FAILURE(
4904 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004905
Michael Wrightd02c5b62014-02-10 15:10:22 -08004906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4907 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4908 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4909
4910 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004911 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4912 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4914 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4915 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004916
Michael Wrightd02c5b62014-02-10 15:10:22 -08004917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004918 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4920 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004921 ASSERT_NO_FATAL_FAILURE(
4922 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004923
4924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4925 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4926 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4927 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004928 ASSERT_NO_FATAL_FAILURE(
4929 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004931 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4932 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004934 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935 ASSERT_EQ(0, motionArgs.buttonState);
4936 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004937 ASSERT_NO_FATAL_FAILURE(
4938 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004939
4940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4941 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4942 ASSERT_EQ(0, motionArgs.buttonState);
4943 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004944 ASSERT_NO_FATAL_FAILURE(
4945 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004946
Michael Wrightd02c5b62014-02-10 15:10:22 -08004947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4948 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4949 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4950
4951 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004952 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4955 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4956 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004957
Michael Wrightd02c5b62014-02-10 15:10:22 -08004958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004959 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004960 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4961 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004962 ASSERT_NO_FATAL_FAILURE(
4963 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004964
4965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4966 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4967 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4968 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004969 ASSERT_NO_FATAL_FAILURE(
4970 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004971
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004972 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4973 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004975 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976 ASSERT_EQ(0, motionArgs.buttonState);
4977 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004978 ASSERT_NO_FATAL_FAILURE(
4979 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004980
4981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4982 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4983 ASSERT_EQ(0, motionArgs.buttonState);
4984 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004985 ASSERT_NO_FATAL_FAILURE(
4986 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004987
Michael Wrightd02c5b62014-02-10 15:10:22 -08004988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4989 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4990 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4991}
4992
4993TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004994 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004995 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004996
4997 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4998 mFakePointerController->setPosition(100, 200);
4999 mFakePointerController->setButtonState(0);
5000
5001 NotifyMotionArgs args;
5002
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5005 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005007 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5008 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5010 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 +01005011 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005012}
5013
5014TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005015 addConfigurationProperty("cursor.mode", "pointer");
5016 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005017 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005018
5019 NotifyDeviceResetArgs resetArgs;
5020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5021 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5022 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5023
5024 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5025 mFakePointerController->setPosition(100, 200);
5026 mFakePointerController->setButtonState(0);
5027
5028 NotifyMotionArgs args;
5029
5030 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5032 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5033 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5035 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5036 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5037 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5038 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 +01005039 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005040
5041 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005042 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5045 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5046 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5047 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5048 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5050 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5051 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
5052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5053 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5054
5055 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005056 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5057 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5059 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5060 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
5061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5062 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5064 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5065 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
5066 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5067 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5068
5069 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005070 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
5071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
5072 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5074 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5075 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5076 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5077 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 +01005078 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005079
5080 // Disable pointer capture and check that the device generation got bumped
5081 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08005082 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005083 mFakePolicy->setPointerCapture(false);
5084 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08005085 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005086
5087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005088 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5089
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005090 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5091 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5094 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005095 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5096 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5097 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 +01005098 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005099}
5100
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00005101/**
5102 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
5103 * pointer acceleration or speed processing should not be applied.
5104 */
5105TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5106 addConfigurationProperty("cursor.mode", "pointer");
5107 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5108 100.f /*high threshold*/, 10.f /*acceleration*/);
5109 mFakePolicy->setVelocityControlParams(testParams);
5110 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5111
5112 NotifyDeviceResetArgs resetArgs;
5113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5114 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5115 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5116
5117 NotifyMotionArgs args;
5118
5119 // Move and verify scale is applied.
5120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5124 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5125 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5126 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5127 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5128 ASSERT_GT(relX, 10);
5129 ASSERT_GT(relY, 20);
5130
5131 // Enable Pointer Capture
5132 mFakePolicy->setPointerCapture(true);
5133 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5134 NotifyPointerCaptureChangedArgs captureArgs;
5135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5136 ASSERT_TRUE(captureArgs.request.enable);
5137
5138 // Move and verify scale is not applied.
5139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5143 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5145 ASSERT_EQ(10, args.pointerCoords[0].getX());
5146 ASSERT_EQ(20, args.pointerCoords[0].getY());
5147}
5148
Prabir Pradhan208360b2022-06-24 18:37:04 +00005149TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5150 addConfigurationProperty("cursor.mode", "pointer");
5151 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5152
5153 NotifyDeviceResetArgs resetArgs;
5154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5155 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5156 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5157
5158 // Ensure the display is rotated.
5159 prepareDisplay(DISPLAY_ORIENTATION_90);
5160
5161 NotifyMotionArgs args;
5162
5163 // Verify that the coordinates are rotated.
5164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5168 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5169 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5170 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5171 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5172
5173 // Enable Pointer Capture.
5174 mFakePolicy->setPointerCapture(true);
5175 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5176 NotifyPointerCaptureChangedArgs captureArgs;
5177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5178 ASSERT_TRUE(captureArgs.request.enable);
5179
5180 // Move and verify rotation is not applied.
5181 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5182 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5183 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5185 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5186 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5187 ASSERT_EQ(10, args.pointerCoords[0].getX());
5188 ASSERT_EQ(20, args.pointerCoords[0].getY());
5189}
5190
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005191TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005192 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005193
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005194 // Set up the default display.
5195 prepareDisplay(DISPLAY_ORIENTATION_90);
5196
5197 // Set up the secondary display as the display on which the pointer should be shown.
5198 // The InputDevice is not associated with any display.
5199 prepareSecondaryDisplay();
5200 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005201 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5202
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005203 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005204 mFakePointerController->setPosition(100, 200);
5205 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005206
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005207 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5209 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5210 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005212 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5213 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5214 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005215 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005216}
5217
5218TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5219 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5220
5221 // Set up the default display.
5222 prepareDisplay(DISPLAY_ORIENTATION_90);
5223
5224 // Set up the secondary display as the display on which the pointer should be shown,
5225 // and associate the InputDevice with the secondary display.
5226 prepareSecondaryDisplay();
5227 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5228 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5229 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5230
5231 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5232 mFakePointerController->setPosition(100, 200);
5233 mFakePointerController->setButtonState(0);
5234
5235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5236 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5237 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005239 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5240 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5241 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005242 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5243}
5244
5245TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5246 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5247
5248 // Set up the default display as the display on which the pointer should be shown.
5249 prepareDisplay(DISPLAY_ORIENTATION_90);
5250 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5251
5252 // Associate the InputDevice with the secondary display.
5253 prepareSecondaryDisplay();
5254 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5255 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5256
5257 // The mapper should not generate any events because it is associated with a display that is
5258 // different from the pointer display.
5259 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5260 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5261 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005263}
5264
Michael Wrightd02c5b62014-02-10 15:10:22 -08005265// --- TouchInputMapperTest ---
5266
5267class TouchInputMapperTest : public InputMapperTest {
5268protected:
5269 static const int32_t RAW_X_MIN;
5270 static const int32_t RAW_X_MAX;
5271 static const int32_t RAW_Y_MIN;
5272 static const int32_t RAW_Y_MAX;
5273 static const int32_t RAW_TOUCH_MIN;
5274 static const int32_t RAW_TOUCH_MAX;
5275 static const int32_t RAW_TOOL_MIN;
5276 static const int32_t RAW_TOOL_MAX;
5277 static const int32_t RAW_PRESSURE_MIN;
5278 static const int32_t RAW_PRESSURE_MAX;
5279 static const int32_t RAW_ORIENTATION_MIN;
5280 static const int32_t RAW_ORIENTATION_MAX;
5281 static const int32_t RAW_DISTANCE_MIN;
5282 static const int32_t RAW_DISTANCE_MAX;
5283 static const int32_t RAW_TILT_MIN;
5284 static const int32_t RAW_TILT_MAX;
5285 static const int32_t RAW_ID_MIN;
5286 static const int32_t RAW_ID_MAX;
5287 static const int32_t RAW_SLOT_MIN;
5288 static const int32_t RAW_SLOT_MAX;
5289 static const float X_PRECISION;
5290 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005291 static const float X_PRECISION_VIRTUAL;
5292 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005293
5294 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005295 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005296
5297 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5298
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005299 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005300 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005301
Michael Wrightd02c5b62014-02-10 15:10:22 -08005302 enum Axes {
5303 POSITION = 1 << 0,
5304 TOUCH = 1 << 1,
5305 TOOL = 1 << 2,
5306 PRESSURE = 1 << 3,
5307 ORIENTATION = 1 << 4,
5308 MINOR = 1 << 5,
5309 ID = 1 << 6,
5310 DISTANCE = 1 << 7,
5311 TILT = 1 << 8,
5312 SLOT = 1 << 9,
5313 TOOL_TYPE = 1 << 10,
5314 };
5315
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005316 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5317 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005318 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005319 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005320 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005321 int32_t toRawX(float displayX);
5322 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005323 int32_t toRotatedRawX(float displayX);
5324 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005325 float toCookedX(float rawX, float rawY);
5326 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005327 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005328 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005329 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005330 float toDisplayY(int32_t rawY, int32_t displayHeight);
5331
Michael Wrightd02c5b62014-02-10 15:10:22 -08005332};
5333
5334const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5335const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5336const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5337const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5338const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5339const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5340const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5341const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005342const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5343const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005344const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5345const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5346const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5347const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5348const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5349const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5350const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5351const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5352const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5353const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5354const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5355const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005356const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5357 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5358const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5359 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005360const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5361 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005362
5363const float TouchInputMapperTest::GEOMETRIC_SCALE =
5364 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5365 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5366
5367const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5368 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5369 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5370};
5371
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005372void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005373 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5374 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005375}
5376
5377void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5378 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5379 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005380}
5381
Santos Cordonfa5cf462017-04-05 10:37:00 -07005382void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005383 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5384 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5385 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005386}
5387
Michael Wrightd02c5b62014-02-10 15:10:22 -08005388void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005389 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5390 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5391 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5392 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005393}
5394
Jason Gerecke489fda82012-09-07 17:19:40 -07005395void TouchInputMapperTest::prepareLocationCalibration() {
5396 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5397}
5398
Michael Wrightd02c5b62014-02-10 15:10:22 -08005399int32_t TouchInputMapperTest::toRawX(float displayX) {
5400 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5401}
5402
5403int32_t TouchInputMapperTest::toRawY(float displayY) {
5404 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5405}
5406
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005407int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5408 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5409}
5410
5411int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5412 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5413}
5414
Jason Gerecke489fda82012-09-07 17:19:40 -07005415float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5416 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5417 return rawX;
5418}
5419
5420float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5421 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5422 return rawY;
5423}
5424
Michael Wrightd02c5b62014-02-10 15:10:22 -08005425float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005426 return toDisplayX(rawX, DISPLAY_WIDTH);
5427}
5428
5429float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5430 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005431}
5432
5433float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005434 return toDisplayY(rawY, DISPLAY_HEIGHT);
5435}
5436
5437float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5438 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005439}
5440
5441
5442// --- SingleTouchInputMapperTest ---
5443
5444class SingleTouchInputMapperTest : public TouchInputMapperTest {
5445protected:
5446 void prepareButtons();
5447 void prepareAxes(int axes);
5448
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005449 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5450 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5451 void processUp(SingleTouchInputMapper& mappery);
5452 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5453 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5454 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5455 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5456 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5457 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005458};
5459
5460void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005461 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005462}
5463
5464void SingleTouchInputMapperTest::prepareAxes(int axes) {
5465 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005466 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5467 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005468 }
5469 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005470 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5471 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005472 }
5473 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005474 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5475 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005476 }
5477 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005478 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5479 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005480 }
5481 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005482 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5483 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005484 }
5485}
5486
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005487void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005488 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5489 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005491}
5492
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005493void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005494 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5495 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005496}
5497
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005498void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005499 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005500}
5501
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005502void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005503 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005504}
5505
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005506void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5507 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005509}
5510
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005511void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005512 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005513}
5514
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005515void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5516 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005519}
5520
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005521void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5522 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005523 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005524}
5525
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005526void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005527 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005528}
5529
Michael Wrightd02c5b62014-02-10 15:10:22 -08005530TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005531 prepareButtons();
5532 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005533 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005534
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005535 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005536}
5537
Michael Wrightd02c5b62014-02-10 15:10:22 -08005538TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005539 prepareButtons();
5540 prepareAxes(POSITION);
5541 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005542 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005543
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005544 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005545}
5546
5547TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005548 addConfigurationProperty("touch.deviceType", "touchScreen");
5549 prepareDisplay(DISPLAY_ORIENTATION_0);
5550 prepareButtons();
5551 prepareAxes(POSITION);
5552 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005553 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005554
5555 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005556 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005557
5558 // Virtual key is down.
5559 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5560 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5561 processDown(mapper, x, y);
5562 processSync(mapper);
5563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5564
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005565 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005566
5567 // Virtual key is up.
5568 processUp(mapper);
5569 processSync(mapper);
5570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5571
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005572 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005573}
5574
5575TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005576 addConfigurationProperty("touch.deviceType", "touchScreen");
5577 prepareDisplay(DISPLAY_ORIENTATION_0);
5578 prepareButtons();
5579 prepareAxes(POSITION);
5580 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005581 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005582
5583 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005584 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005585
5586 // Virtual key is down.
5587 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5588 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5589 processDown(mapper, x, y);
5590 processSync(mapper);
5591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5592
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005593 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005594
5595 // Virtual key is up.
5596 processUp(mapper);
5597 processSync(mapper);
5598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5599
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005600 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005601}
5602
5603TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005604 addConfigurationProperty("touch.deviceType", "touchScreen");
5605 prepareDisplay(DISPLAY_ORIENTATION_0);
5606 prepareButtons();
5607 prepareAxes(POSITION);
5608 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005609 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005610
Michael Wrightd02c5b62014-02-10 15:10:22 -08005611 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005612 ASSERT_TRUE(
5613 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005614 ASSERT_TRUE(flags[0]);
5615 ASSERT_FALSE(flags[1]);
5616}
5617
5618TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005619 addConfigurationProperty("touch.deviceType", "touchScreen");
5620 prepareDisplay(DISPLAY_ORIENTATION_0);
5621 prepareButtons();
5622 prepareAxes(POSITION);
5623 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005624 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005625
arthurhungdcef2dc2020-08-11 14:47:50 +08005626 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005627
5628 NotifyKeyArgs args;
5629
5630 // Press virtual key.
5631 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5632 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5633 processDown(mapper, x, y);
5634 processSync(mapper);
5635
5636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5637 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5638 ASSERT_EQ(DEVICE_ID, args.deviceId);
5639 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5640 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5641 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5642 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5643 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5644 ASSERT_EQ(KEY_HOME, args.scanCode);
5645 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5646 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5647
5648 // Release virtual key.
5649 processUp(mapper);
5650 processSync(mapper);
5651
5652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5653 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5654 ASSERT_EQ(DEVICE_ID, args.deviceId);
5655 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5656 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5657 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5658 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5659 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5660 ASSERT_EQ(KEY_HOME, args.scanCode);
5661 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5662 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5663
5664 // Should not have sent any motions.
5665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5666}
5667
5668TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005669 addConfigurationProperty("touch.deviceType", "touchScreen");
5670 prepareDisplay(DISPLAY_ORIENTATION_0);
5671 prepareButtons();
5672 prepareAxes(POSITION);
5673 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005674 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005675
arthurhungdcef2dc2020-08-11 14:47:50 +08005676 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005677
5678 NotifyKeyArgs keyArgs;
5679
5680 // Press virtual key.
5681 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5682 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5683 processDown(mapper, x, y);
5684 processSync(mapper);
5685
5686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5687 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5688 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5689 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5690 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5691 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5692 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5693 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5694 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5695 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5696 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5697
5698 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5699 // into the display area.
5700 y -= 100;
5701 processMove(mapper, x, y);
5702 processSync(mapper);
5703
5704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5705 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5706 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5707 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5708 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5709 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5710 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5711 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5712 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5713 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5714 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5715 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5716
5717 NotifyMotionArgs motionArgs;
5718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5719 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5720 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5721 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5722 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5723 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5724 ASSERT_EQ(0, motionArgs.flags);
5725 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5726 ASSERT_EQ(0, motionArgs.buttonState);
5727 ASSERT_EQ(0, motionArgs.edgeFlags);
5728 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5729 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5730 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5732 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5733 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5734 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5735 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5736
5737 // Keep moving out of bounds. Should generate a pointer move.
5738 y -= 50;
5739 processMove(mapper, x, y);
5740 processSync(mapper);
5741
5742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5743 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5744 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5745 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5746 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5747 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5748 ASSERT_EQ(0, motionArgs.flags);
5749 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5750 ASSERT_EQ(0, motionArgs.buttonState);
5751 ASSERT_EQ(0, motionArgs.edgeFlags);
5752 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5753 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5754 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5756 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5757 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5758 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5759 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5760
5761 // Release out of bounds. Should generate a pointer up.
5762 processUp(mapper);
5763 processSync(mapper);
5764
5765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5766 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5767 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5768 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5769 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5770 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5771 ASSERT_EQ(0, motionArgs.flags);
5772 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5773 ASSERT_EQ(0, motionArgs.buttonState);
5774 ASSERT_EQ(0, motionArgs.edgeFlags);
5775 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5776 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5777 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5779 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5780 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5781 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5782 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5783
5784 // Should not have sent any more keys or motions.
5785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5787}
5788
5789TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005790 addConfigurationProperty("touch.deviceType", "touchScreen");
5791 prepareDisplay(DISPLAY_ORIENTATION_0);
5792 prepareButtons();
5793 prepareAxes(POSITION);
5794 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005795 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005796
arthurhungdcef2dc2020-08-11 14:47:50 +08005797 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005798
5799 NotifyMotionArgs motionArgs;
5800
5801 // Initially go down out of bounds.
5802 int32_t x = -10;
5803 int32_t y = -10;
5804 processDown(mapper, x, y);
5805 processSync(mapper);
5806
5807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5808
5809 // Move into the display area. Should generate a pointer down.
5810 x = 50;
5811 y = 75;
5812 processMove(mapper, x, y);
5813 processSync(mapper);
5814
5815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5816 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5817 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5818 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5819 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5820 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5821 ASSERT_EQ(0, motionArgs.flags);
5822 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5823 ASSERT_EQ(0, motionArgs.buttonState);
5824 ASSERT_EQ(0, motionArgs.edgeFlags);
5825 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5826 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5827 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5828 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5829 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5830 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5831 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5832 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5833
5834 // Release. Should generate a pointer up.
5835 processUp(mapper);
5836 processSync(mapper);
5837
5838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5839 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5840 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5841 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5842 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5843 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5844 ASSERT_EQ(0, motionArgs.flags);
5845 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5846 ASSERT_EQ(0, motionArgs.buttonState);
5847 ASSERT_EQ(0, motionArgs.edgeFlags);
5848 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5849 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5850 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5852 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5853 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5854 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5855 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5856
5857 // Should not have sent any more keys or motions.
5858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5860}
5861
Santos Cordonfa5cf462017-04-05 10:37:00 -07005862TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005863 addConfigurationProperty("touch.deviceType", "touchScreen");
5864 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5865
5866 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5867 prepareButtons();
5868 prepareAxes(POSITION);
5869 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005870 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005871
arthurhungdcef2dc2020-08-11 14:47:50 +08005872 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005873
5874 NotifyMotionArgs motionArgs;
5875
5876 // Down.
5877 int32_t x = 100;
5878 int32_t y = 125;
5879 processDown(mapper, x, y);
5880 processSync(mapper);
5881
5882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5883 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5884 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5885 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5886 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5887 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5888 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5889 ASSERT_EQ(0, motionArgs.flags);
5890 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5891 ASSERT_EQ(0, motionArgs.buttonState);
5892 ASSERT_EQ(0, motionArgs.edgeFlags);
5893 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5894 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5895 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5897 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5898 1, 0, 0, 0, 0, 0, 0, 0));
5899 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5900 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5901 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5902
5903 // Move.
5904 x += 50;
5905 y += 75;
5906 processMove(mapper, x, y);
5907 processSync(mapper);
5908
5909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5910 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5911 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5912 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5913 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5914 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5915 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5916 ASSERT_EQ(0, motionArgs.flags);
5917 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5918 ASSERT_EQ(0, motionArgs.buttonState);
5919 ASSERT_EQ(0, motionArgs.edgeFlags);
5920 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5921 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5922 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5923 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5924 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5925 1, 0, 0, 0, 0, 0, 0, 0));
5926 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5927 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5928 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5929
5930 // Up.
5931 processUp(mapper);
5932 processSync(mapper);
5933
5934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5935 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5936 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5937 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5938 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5939 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5940 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5941 ASSERT_EQ(0, motionArgs.flags);
5942 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5943 ASSERT_EQ(0, motionArgs.buttonState);
5944 ASSERT_EQ(0, motionArgs.edgeFlags);
5945 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5946 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5947 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5949 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5950 1, 0, 0, 0, 0, 0, 0, 0));
5951 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5952 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5953 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5954
5955 // Should not have sent any more keys or motions.
5956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5958}
5959
Michael Wrightd02c5b62014-02-10 15:10:22 -08005960TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005961 addConfigurationProperty("touch.deviceType", "touchScreen");
5962 prepareDisplay(DISPLAY_ORIENTATION_0);
5963 prepareButtons();
5964 prepareAxes(POSITION);
5965 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005966 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005967
arthurhungdcef2dc2020-08-11 14:47:50 +08005968 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005969
5970 NotifyMotionArgs motionArgs;
5971
5972 // Down.
5973 int32_t x = 100;
5974 int32_t y = 125;
5975 processDown(mapper, x, y);
5976 processSync(mapper);
5977
5978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5979 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5980 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5981 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5982 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5983 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5984 ASSERT_EQ(0, motionArgs.flags);
5985 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5986 ASSERT_EQ(0, motionArgs.buttonState);
5987 ASSERT_EQ(0, motionArgs.edgeFlags);
5988 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5989 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5990 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5992 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5993 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5994 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5995 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5996
5997 // Move.
5998 x += 50;
5999 y += 75;
6000 processMove(mapper, x, y);
6001 processSync(mapper);
6002
6003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6004 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6005 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6006 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6007 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6008 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6009 ASSERT_EQ(0, motionArgs.flags);
6010 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6011 ASSERT_EQ(0, motionArgs.buttonState);
6012 ASSERT_EQ(0, motionArgs.edgeFlags);
6013 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6014 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6015 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6017 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6018 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6019 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6020 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6021
6022 // Up.
6023 processUp(mapper);
6024 processSync(mapper);
6025
6026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6027 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6028 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6029 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6030 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6031 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6032 ASSERT_EQ(0, motionArgs.flags);
6033 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6034 ASSERT_EQ(0, motionArgs.buttonState);
6035 ASSERT_EQ(0, motionArgs.edgeFlags);
6036 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6037 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6038 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6039 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6040 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6041 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6042 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6043 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6044
6045 // Should not have sent any more keys or motions.
6046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6048}
6049
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006050TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006051 addConfigurationProperty("touch.deviceType", "touchScreen");
6052 prepareButtons();
6053 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006054 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
6055 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006056 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006057
6058 NotifyMotionArgs args;
6059
6060 // Rotation 90.
6061 prepareDisplay(DISPLAY_ORIENTATION_90);
6062 processDown(mapper, toRawX(50), toRawY(75));
6063 processSync(mapper);
6064
6065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6066 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6067 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6068
6069 processUp(mapper);
6070 processSync(mapper);
6071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6072}
6073
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006074TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006075 addConfigurationProperty("touch.deviceType", "touchScreen");
6076 prepareButtons();
6077 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006078 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6079 // orientation-aware are affected by display rotation.
6080 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006081 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006082
6083 NotifyMotionArgs args;
6084
6085 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006086 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006087 prepareDisplay(DISPLAY_ORIENTATION_0);
6088 processDown(mapper, toRawX(50), toRawY(75));
6089 processSync(mapper);
6090
6091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6092 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6093 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6094
6095 processUp(mapper);
6096 processSync(mapper);
6097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6098
6099 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006100 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006101 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006102 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006103 processSync(mapper);
6104
6105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6106 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6107 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6108
6109 processUp(mapper);
6110 processSync(mapper);
6111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6112
6113 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006114 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006115 prepareDisplay(DISPLAY_ORIENTATION_180);
6116 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6117 processSync(mapper);
6118
6119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6120 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6121 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6122
6123 processUp(mapper);
6124 processSync(mapper);
6125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6126
6127 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006128 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006130 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006131 processSync(mapper);
6132
6133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6134 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6135 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6136
6137 processUp(mapper);
6138 processSync(mapper);
6139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6140}
6141
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006142TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6143 addConfigurationProperty("touch.deviceType", "touchScreen");
6144 prepareButtons();
6145 prepareAxes(POSITION);
6146 addConfigurationProperty("touch.orientationAware", "1");
6147 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6148 clearViewports();
6149 prepareDisplay(DISPLAY_ORIENTATION_0);
6150 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6151 NotifyMotionArgs args;
6152
6153 // Orientation 0.
6154 processDown(mapper, toRawX(50), toRawY(75));
6155 processSync(mapper);
6156
6157 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6158 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6159 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6160
6161 processUp(mapper);
6162 processSync(mapper);
6163 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6164}
6165
6166TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6167 addConfigurationProperty("touch.deviceType", "touchScreen");
6168 prepareButtons();
6169 prepareAxes(POSITION);
6170 addConfigurationProperty("touch.orientationAware", "1");
6171 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6172 clearViewports();
6173 prepareDisplay(DISPLAY_ORIENTATION_0);
6174 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6175 NotifyMotionArgs args;
6176
6177 // Orientation 90.
6178 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6179 processSync(mapper);
6180
6181 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6182 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6183 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6184
6185 processUp(mapper);
6186 processSync(mapper);
6187 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6188}
6189
6190TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6191 addConfigurationProperty("touch.deviceType", "touchScreen");
6192 prepareButtons();
6193 prepareAxes(POSITION);
6194 addConfigurationProperty("touch.orientationAware", "1");
6195 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6196 clearViewports();
6197 prepareDisplay(DISPLAY_ORIENTATION_0);
6198 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6199 NotifyMotionArgs args;
6200
6201 // Orientation 180.
6202 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6203 processSync(mapper);
6204
6205 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6206 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6207 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6208
6209 processUp(mapper);
6210 processSync(mapper);
6211 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6212}
6213
6214TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6215 addConfigurationProperty("touch.deviceType", "touchScreen");
6216 prepareButtons();
6217 prepareAxes(POSITION);
6218 addConfigurationProperty("touch.orientationAware", "1");
6219 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6220 clearViewports();
6221 prepareDisplay(DISPLAY_ORIENTATION_0);
6222 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6223 NotifyMotionArgs args;
6224
6225 // Orientation 270.
6226 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6227 processSync(mapper);
6228
6229 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6230 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6231 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6232
6233 processUp(mapper);
6234 processSync(mapper);
6235 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6236}
6237
6238TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6239 addConfigurationProperty("touch.deviceType", "touchScreen");
6240 prepareButtons();
6241 prepareAxes(POSITION);
6242 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6243 // orientation-aware are affected by display rotation.
6244 addConfigurationProperty("touch.orientationAware", "0");
6245 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6246 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6247
6248 NotifyMotionArgs args;
6249
6250 // Orientation 90, Rotation 0.
6251 clearViewports();
6252 prepareDisplay(DISPLAY_ORIENTATION_0);
6253 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6254 processSync(mapper);
6255
6256 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6257 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6258 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6259
6260 processUp(mapper);
6261 processSync(mapper);
6262 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6263
6264 // Orientation 90, Rotation 90.
6265 clearViewports();
6266 prepareDisplay(DISPLAY_ORIENTATION_90);
6267 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6268 processSync(mapper);
6269
6270 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6271 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6272 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6273
6274 processUp(mapper);
6275 processSync(mapper);
6276 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6277
6278 // Orientation 90, Rotation 180.
6279 clearViewports();
6280 prepareDisplay(DISPLAY_ORIENTATION_180);
6281 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6282 processSync(mapper);
6283
6284 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6285 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6286 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6287
6288 processUp(mapper);
6289 processSync(mapper);
6290 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6291
6292 // Orientation 90, Rotation 270.
6293 clearViewports();
6294 prepareDisplay(DISPLAY_ORIENTATION_270);
6295 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6296 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6297 processSync(mapper);
6298
6299 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6300 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6301 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6302
6303 processUp(mapper);
6304 processSync(mapper);
6305 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6306}
6307
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006309 addConfigurationProperty("touch.deviceType", "touchScreen");
6310 prepareDisplay(DISPLAY_ORIENTATION_0);
6311 prepareButtons();
6312 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006313 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006314
6315 // These calculations are based on the input device calibration documentation.
6316 int32_t rawX = 100;
6317 int32_t rawY = 200;
6318 int32_t rawPressure = 10;
6319 int32_t rawToolMajor = 12;
6320 int32_t rawDistance = 2;
6321 int32_t rawTiltX = 30;
6322 int32_t rawTiltY = 110;
6323
6324 float x = toDisplayX(rawX);
6325 float y = toDisplayY(rawY);
6326 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6327 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6328 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6329 float distance = float(rawDistance);
6330
6331 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6332 float tiltScale = M_PI / 180;
6333 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6334 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6335 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6336 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6337
6338 processDown(mapper, rawX, rawY);
6339 processPressure(mapper, rawPressure);
6340 processToolMajor(mapper, rawToolMajor);
6341 processDistance(mapper, rawDistance);
6342 processTilt(mapper, rawTiltX, rawTiltY);
6343 processSync(mapper);
6344
6345 NotifyMotionArgs args;
6346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6347 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6348 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6349 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6350}
6351
Jason Gerecke489fda82012-09-07 17:19:40 -07006352TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006353 addConfigurationProperty("touch.deviceType", "touchScreen");
6354 prepareDisplay(DISPLAY_ORIENTATION_0);
6355 prepareLocationCalibration();
6356 prepareButtons();
6357 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006358 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006359
6360 int32_t rawX = 100;
6361 int32_t rawY = 200;
6362
6363 float x = toDisplayX(toCookedX(rawX, rawY));
6364 float y = toDisplayY(toCookedY(rawX, rawY));
6365
6366 processDown(mapper, rawX, rawY);
6367 processSync(mapper);
6368
6369 NotifyMotionArgs args;
6370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6372 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6373}
6374
Michael Wrightd02c5b62014-02-10 15:10:22 -08006375TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006376 addConfigurationProperty("touch.deviceType", "touchScreen");
6377 prepareDisplay(DISPLAY_ORIENTATION_0);
6378 prepareButtons();
6379 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006380 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006381
6382 NotifyMotionArgs motionArgs;
6383 NotifyKeyArgs keyArgs;
6384
6385 processDown(mapper, 100, 200);
6386 processSync(mapper);
6387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6388 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6389 ASSERT_EQ(0, motionArgs.buttonState);
6390
6391 // press BTN_LEFT, release BTN_LEFT
6392 processKey(mapper, BTN_LEFT, 1);
6393 processSync(mapper);
6394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6395 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6396 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6397
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6399 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6400 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6401
Michael Wrightd02c5b62014-02-10 15:10:22 -08006402 processKey(mapper, BTN_LEFT, 0);
6403 processSync(mapper);
6404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006405 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006406 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006407
6408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006409 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006410 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006411
6412 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6413 processKey(mapper, BTN_RIGHT, 1);
6414 processKey(mapper, BTN_MIDDLE, 1);
6415 processSync(mapper);
6416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6417 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6418 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6419 motionArgs.buttonState);
6420
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6422 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6423 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6424
6425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6426 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6427 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6428 motionArgs.buttonState);
6429
Michael Wrightd02c5b62014-02-10 15:10:22 -08006430 processKey(mapper, BTN_RIGHT, 0);
6431 processSync(mapper);
6432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006433 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006434 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006435
6436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006437 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006438 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006439
6440 processKey(mapper, BTN_MIDDLE, 0);
6441 processSync(mapper);
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006443 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006444 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006445
6446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006448 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006449
6450 // press BTN_BACK, release BTN_BACK
6451 processKey(mapper, BTN_BACK, 1);
6452 processSync(mapper);
6453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6454 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6455 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006456
Michael Wrightd02c5b62014-02-10 15:10:22 -08006457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006458 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006459 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6460
6461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6462 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6463 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006464
6465 processKey(mapper, BTN_BACK, 0);
6466 processSync(mapper);
6467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006468 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006469 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006470
6471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006473 ASSERT_EQ(0, motionArgs.buttonState);
6474
Michael Wrightd02c5b62014-02-10 15:10:22 -08006475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6476 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6477 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6478
6479 // press BTN_SIDE, release BTN_SIDE
6480 processKey(mapper, BTN_SIDE, 1);
6481 processSync(mapper);
6482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6483 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6484 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006485
Michael Wrightd02c5b62014-02-10 15:10:22 -08006486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006488 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6489
6490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6491 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6492 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006493
6494 processKey(mapper, BTN_SIDE, 0);
6495 processSync(mapper);
6496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006498 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006499
6500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006502 ASSERT_EQ(0, motionArgs.buttonState);
6503
Michael Wrightd02c5b62014-02-10 15:10:22 -08006504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6505 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6506 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6507
6508 // press BTN_FORWARD, release BTN_FORWARD
6509 processKey(mapper, BTN_FORWARD, 1);
6510 processSync(mapper);
6511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6512 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6513 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006514
Michael Wrightd02c5b62014-02-10 15:10:22 -08006515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006516 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006517 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6518
6519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6520 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6521 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006522
6523 processKey(mapper, BTN_FORWARD, 0);
6524 processSync(mapper);
6525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006526 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006527 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006528
6529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006530 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006531 ASSERT_EQ(0, motionArgs.buttonState);
6532
Michael Wrightd02c5b62014-02-10 15:10:22 -08006533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6534 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6535 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6536
6537 // press BTN_EXTRA, release BTN_EXTRA
6538 processKey(mapper, BTN_EXTRA, 1);
6539 processSync(mapper);
6540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6541 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6542 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006543
Michael Wrightd02c5b62014-02-10 15:10:22 -08006544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006545 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006546 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6547
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6549 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6550 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006551
6552 processKey(mapper, BTN_EXTRA, 0);
6553 processSync(mapper);
6554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006555 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006556 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006557
6558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006560 ASSERT_EQ(0, motionArgs.buttonState);
6561
Michael Wrightd02c5b62014-02-10 15:10:22 -08006562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6563 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6564 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6565
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6567
Michael Wrightd02c5b62014-02-10 15:10:22 -08006568 // press BTN_STYLUS, release BTN_STYLUS
6569 processKey(mapper, BTN_STYLUS, 1);
6570 processSync(mapper);
6571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6572 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006573 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6574
6575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6576 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6577 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006578
6579 processKey(mapper, BTN_STYLUS, 0);
6580 processSync(mapper);
6581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006582 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006583 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006584
6585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006586 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006587 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006588
6589 // press BTN_STYLUS2, release BTN_STYLUS2
6590 processKey(mapper, BTN_STYLUS2, 1);
6591 processSync(mapper);
6592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6593 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006594 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6595
6596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6597 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6598 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006599
6600 processKey(mapper, BTN_STYLUS2, 0);
6601 processSync(mapper);
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006603 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006604 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006605
6606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006607 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006608 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006609
6610 // release touch
6611 processUp(mapper);
6612 processSync(mapper);
6613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6614 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6615 ASSERT_EQ(0, motionArgs.buttonState);
6616}
6617
6618TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006619 addConfigurationProperty("touch.deviceType", "touchScreen");
6620 prepareDisplay(DISPLAY_ORIENTATION_0);
6621 prepareButtons();
6622 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006623 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006624
6625 NotifyMotionArgs motionArgs;
6626
6627 // default tool type is finger
6628 processDown(mapper, 100, 200);
6629 processSync(mapper);
6630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6631 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6633
6634 // eraser
6635 processKey(mapper, BTN_TOOL_RUBBER, 1);
6636 processSync(mapper);
6637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6638 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6639 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6640
6641 // stylus
6642 processKey(mapper, BTN_TOOL_RUBBER, 0);
6643 processKey(mapper, BTN_TOOL_PEN, 1);
6644 processSync(mapper);
6645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6647 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6648
6649 // brush
6650 processKey(mapper, BTN_TOOL_PEN, 0);
6651 processKey(mapper, BTN_TOOL_BRUSH, 1);
6652 processSync(mapper);
6653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6655 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6656
6657 // pencil
6658 processKey(mapper, BTN_TOOL_BRUSH, 0);
6659 processKey(mapper, BTN_TOOL_PENCIL, 1);
6660 processSync(mapper);
6661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6662 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6663 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6664
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006665 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006666 processKey(mapper, BTN_TOOL_PENCIL, 0);
6667 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6668 processSync(mapper);
6669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6671 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6672
6673 // mouse
6674 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6675 processKey(mapper, BTN_TOOL_MOUSE, 1);
6676 processSync(mapper);
6677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6678 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6679 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6680
6681 // lens
6682 processKey(mapper, BTN_TOOL_MOUSE, 0);
6683 processKey(mapper, BTN_TOOL_LENS, 1);
6684 processSync(mapper);
6685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6686 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6687 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6688
6689 // double-tap
6690 processKey(mapper, BTN_TOOL_LENS, 0);
6691 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6692 processSync(mapper);
6693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6694 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6695 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6696
6697 // triple-tap
6698 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6699 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6700 processSync(mapper);
6701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6702 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6703 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6704
6705 // quad-tap
6706 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6707 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6708 processSync(mapper);
6709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6710 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6711 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6712
6713 // finger
6714 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6715 processKey(mapper, BTN_TOOL_FINGER, 1);
6716 processSync(mapper);
6717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6718 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6719 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6720
6721 // stylus trumps finger
6722 processKey(mapper, BTN_TOOL_PEN, 1);
6723 processSync(mapper);
6724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6726 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6727
6728 // eraser trumps stylus
6729 processKey(mapper, BTN_TOOL_RUBBER, 1);
6730 processSync(mapper);
6731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6732 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6733 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6734
6735 // mouse trumps eraser
6736 processKey(mapper, BTN_TOOL_MOUSE, 1);
6737 processSync(mapper);
6738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6739 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6740 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6741
6742 // back to default tool type
6743 processKey(mapper, BTN_TOOL_MOUSE, 0);
6744 processKey(mapper, BTN_TOOL_RUBBER, 0);
6745 processKey(mapper, BTN_TOOL_PEN, 0);
6746 processKey(mapper, BTN_TOOL_FINGER, 0);
6747 processSync(mapper);
6748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6749 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6750 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6751}
6752
6753TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006754 addConfigurationProperty("touch.deviceType", "touchScreen");
6755 prepareDisplay(DISPLAY_ORIENTATION_0);
6756 prepareButtons();
6757 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006758 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006759 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006760
6761 NotifyMotionArgs motionArgs;
6762
6763 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6764 processKey(mapper, BTN_TOOL_FINGER, 1);
6765 processMove(mapper, 100, 200);
6766 processSync(mapper);
6767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6768 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6770 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6771
6772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6773 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6774 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6775 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6776
6777 // move a little
6778 processMove(mapper, 150, 250);
6779 processSync(mapper);
6780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6781 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6783 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6784
6785 // down when BTN_TOUCH is pressed, pressure defaults to 1
6786 processKey(mapper, BTN_TOUCH, 1);
6787 processSync(mapper);
6788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6789 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6790 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6791 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6792
6793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6794 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6796 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6797
6798 // up when BTN_TOUCH is released, hover restored
6799 processKey(mapper, BTN_TOUCH, 0);
6800 processSync(mapper);
6801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6802 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6803 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6804 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6805
6806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6807 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6808 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6809 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6810
6811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6812 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6814 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6815
6816 // exit hover when pointer goes away
6817 processKey(mapper, BTN_TOOL_FINGER, 0);
6818 processSync(mapper);
6819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6820 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6822 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6823}
6824
6825TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006826 addConfigurationProperty("touch.deviceType", "touchScreen");
6827 prepareDisplay(DISPLAY_ORIENTATION_0);
6828 prepareButtons();
6829 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006830 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006831
6832 NotifyMotionArgs motionArgs;
6833
6834 // initially hovering because pressure is 0
6835 processDown(mapper, 100, 200);
6836 processPressure(mapper, 0);
6837 processSync(mapper);
6838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6839 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6840 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6841 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6842
6843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6844 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6845 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6846 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6847
6848 // move a little
6849 processMove(mapper, 150, 250);
6850 processSync(mapper);
6851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6852 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6854 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6855
6856 // down when pressure is non-zero
6857 processPressure(mapper, RAW_PRESSURE_MAX);
6858 processSync(mapper);
6859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6860 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6862 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6863
6864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6865 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6867 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6868
6869 // up when pressure becomes 0, hover restored
6870 processPressure(mapper, 0);
6871 processSync(mapper);
6872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6873 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6875 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6876
6877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6878 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6880 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6881
6882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6883 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6884 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6885 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6886
6887 // exit hover when pointer goes away
6888 processUp(mapper);
6889 processSync(mapper);
6890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6891 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6892 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6893 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6894}
6895
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006896TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6897 addConfigurationProperty("touch.deviceType", "touchScreen");
6898 prepareDisplay(DISPLAY_ORIENTATION_0);
6899 prepareButtons();
6900 prepareAxes(POSITION | PRESSURE);
6901 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6902
6903 // Touch down.
6904 processDown(mapper, 100, 200);
6905 processPressure(mapper, 1);
6906 processSync(mapper);
6907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6908 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6909
6910 // Reset the mapper. This should cancel the ongoing gesture.
6911 resetMapper(mapper, ARBITRARY_TIME);
6912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6913 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6914
6915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6916}
6917
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006918TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6919 addConfigurationProperty("touch.deviceType", "touchScreen");
6920 prepareDisplay(DISPLAY_ORIENTATION_0);
6921 prepareButtons();
6922 prepareAxes(POSITION | PRESSURE);
6923 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6924
6925 // Set the initial state for the touch pointer.
6926 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6927 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6928 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6929 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6930
6931 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006932 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6933 // does not generate any events.
6934 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006935
6936 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6937 // the recreated touch state to generate a down event.
6938 processSync(mapper);
6939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6940 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6941
6942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6943}
6944
lilinnan687e58f2022-07-19 16:00:50 +08006945TEST_F(SingleTouchInputMapperTest,
6946 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6947 addConfigurationProperty("touch.deviceType", "touchScreen");
6948 prepareDisplay(DISPLAY_ORIENTATION_0);
6949 prepareButtons();
6950 prepareAxes(POSITION);
6951 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6952 NotifyMotionArgs motionArgs;
6953
6954 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006955 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006956 processSync(mapper);
6957
6958 // We should receive a down event
6959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6960 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6961
6962 // Change display id
6963 clearViewports();
6964 prepareSecondaryDisplay(ViewportType::INTERNAL);
6965
6966 // We should receive a cancel event
6967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6968 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6969 // Then receive reset called
6970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6971}
6972
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006973TEST_F(SingleTouchInputMapperTest,
6974 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6975 addConfigurationProperty("touch.deviceType", "touchScreen");
6976 prepareDisplay(DISPLAY_ORIENTATION_0);
6977 prepareButtons();
6978 prepareAxes(POSITION);
6979 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6981 NotifyMotionArgs motionArgs;
6982
6983 // Start a new gesture.
6984 processDown(mapper, 100, 200);
6985 processSync(mapper);
6986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6987 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6988
6989 // Make the viewport inactive. This will put the device in disabled mode.
6990 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6991 viewport->isActive = false;
6992 mFakePolicy->updateViewport(*viewport);
6993 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6994
6995 // We should receive a cancel event for the ongoing gesture.
6996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6997 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6998 // Then we should be notified that the device was reset.
6999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7000
7001 // No events are generated while the viewport is inactive.
7002 processMove(mapper, 101, 201);
7003 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007004 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007005 processSync(mapper);
7006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7007
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007008 // Start a new gesture while the viewport is still inactive.
7009 processDown(mapper, 300, 400);
7010 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
7011 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
7012 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7013 processSync(mapper);
7014
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007015 // Make the viewport active again. The device should resume processing events.
7016 viewport->isActive = true;
7017 mFakePolicy->updateViewport(*viewport);
7018 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7019
7020 // The device is reset because it changes back to direct mode, without generating any events.
7021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7023
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007024 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007025 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7027 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007028
7029 // No more events.
7030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
7032}
7033
Prabir Pradhan5632d622021-09-06 07:57:20 -07007034// --- TouchDisplayProjectionTest ---
7035
7036class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7037public:
7038 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7039 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7040 // rotated equivalent of the given un-rotated physical display bounds.
7041 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
7042 uint32_t inverseRotationFlags;
7043 auto width = DISPLAY_WIDTH;
7044 auto height = DISPLAY_HEIGHT;
7045 switch (orientation) {
7046 case DISPLAY_ORIENTATION_90:
7047 inverseRotationFlags = ui::Transform::ROT_270;
7048 std::swap(width, height);
7049 break;
7050 case DISPLAY_ORIENTATION_180:
7051 inverseRotationFlags = ui::Transform::ROT_180;
7052 break;
7053 case DISPLAY_ORIENTATION_270:
7054 inverseRotationFlags = ui::Transform::ROT_90;
7055 std::swap(width, height);
7056 break;
7057 case DISPLAY_ORIENTATION_0:
7058 inverseRotationFlags = ui::Transform::ROT_0;
7059 break;
7060 default:
7061 FAIL() << "Invalid orientation: " << orientation;
7062 }
7063
7064 const ui::Transform rotation(inverseRotationFlags, width, height);
7065 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7066
7067 std::optional<DisplayViewport> internalViewport =
7068 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7069 DisplayViewport& v = *internalViewport;
7070 v.displayId = DISPLAY_ID;
7071 v.orientation = orientation;
7072
7073 v.logicalLeft = 0;
7074 v.logicalTop = 0;
7075 v.logicalRight = 100;
7076 v.logicalBottom = 100;
7077
7078 v.physicalLeft = rotatedPhysicalDisplay.left;
7079 v.physicalTop = rotatedPhysicalDisplay.top;
7080 v.physicalRight = rotatedPhysicalDisplay.right;
7081 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7082
7083 v.deviceWidth = width;
7084 v.deviceHeight = height;
7085
7086 v.isActive = true;
7087 v.uniqueId = UNIQUE_ID;
7088 v.type = ViewportType::INTERNAL;
7089 mFakePolicy->updateViewport(v);
7090 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7091 }
7092
7093 void assertReceivedMove(const Point& point) {
7094 NotifyMotionArgs motionArgs;
7095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7096 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7097 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7098 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7099 1, 0, 0, 0, 0, 0, 0, 0));
7100 }
7101};
7102
7103TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7104 addConfigurationProperty("touch.deviceType", "touchScreen");
7105 prepareDisplay(DISPLAY_ORIENTATION_0);
7106
7107 prepareButtons();
7108 prepareAxes(POSITION);
7109 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7110
7111 NotifyMotionArgs motionArgs;
7112
7113 // Configure the DisplayViewport such that the logical display maps to a subsection of
7114 // the display panel called the physical display. Here, the physical display is bounded by the
7115 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7116 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7117 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7118 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7119
7120 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7121 DISPLAY_ORIENTATION_270}) {
7122 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7123
7124 // Touches outside the physical display should be ignored, and should not generate any
7125 // events. Ensure touches at the following points that lie outside of the physical display
7126 // area do not generate any events.
7127 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7128 processDown(mapper, toRawX(point.x), toRawY(point.y));
7129 processSync(mapper);
7130 processUp(mapper);
7131 processSync(mapper);
7132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7133 << "Unexpected event generated for touch outside physical display at point: "
7134 << point.x << ", " << point.y;
7135 }
7136 }
7137}
7138
7139TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7140 addConfigurationProperty("touch.deviceType", "touchScreen");
7141 prepareDisplay(DISPLAY_ORIENTATION_0);
7142
7143 prepareButtons();
7144 prepareAxes(POSITION);
7145 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7146
7147 NotifyMotionArgs motionArgs;
7148
7149 // Configure the DisplayViewport such that the logical display maps to a subsection of
7150 // the display panel called the physical display. Here, the physical display is bounded by the
7151 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7152 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7153
7154 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7155 DISPLAY_ORIENTATION_270}) {
7156 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7157
7158 // Touches that start outside the physical display should be ignored until it enters the
7159 // physical display bounds, at which point it should generate a down event. Start a touch at
7160 // the point (5, 100), which is outside the physical display bounds.
7161 static const Point kOutsidePoint{5, 100};
7162 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7163 processSync(mapper);
7164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7165
7166 // Move the touch into the physical display area. This should generate a pointer down.
7167 processMove(mapper, toRawX(11), toRawY(21));
7168 processSync(mapper);
7169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7170 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7171 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7172 ASSERT_NO_FATAL_FAILURE(
7173 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7174
7175 // Move the touch inside the physical display area. This should generate a pointer move.
7176 processMove(mapper, toRawX(69), toRawY(159));
7177 processSync(mapper);
7178 assertReceivedMove({69, 159});
7179
7180 // Move outside the physical display area. Since the pointer is already down, this should
7181 // now continue generating events.
7182 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7183 processSync(mapper);
7184 assertReceivedMove(kOutsidePoint);
7185
7186 // Release. This should generate a pointer up.
7187 processUp(mapper);
7188 processSync(mapper);
7189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7190 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7191 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7192 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7193
7194 // Ensure no more events were generated.
7195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7197 }
7198}
7199
Michael Wrightd02c5b62014-02-10 15:10:22 -08007200// --- MultiTouchInputMapperTest ---
7201
7202class MultiTouchInputMapperTest : public TouchInputMapperTest {
7203protected:
7204 void prepareAxes(int axes);
7205
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007206 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7207 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7208 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7209 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7210 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7211 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7212 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7213 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7214 void processId(MultiTouchInputMapper& mapper, int32_t id);
7215 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7216 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7217 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
7218 void processMTSync(MultiTouchInputMapper& mapper);
7219 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007220};
7221
7222void MultiTouchInputMapperTest::prepareAxes(int axes) {
7223 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007224 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7225 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007226 }
7227 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007228 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7229 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007230 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007231 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7232 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007233 }
7234 }
7235 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007236 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7237 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007238 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007239 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007240 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007241 }
7242 }
7243 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007244 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7245 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007246 }
7247 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007248 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7249 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007250 }
7251 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007252 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7253 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007254 }
7255 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007256 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7257 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007258 }
7259 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007260 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7261 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007262 }
7263 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007264 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007265 }
7266}
7267
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007268void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7269 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7271 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007272}
7273
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007274void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7275 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007276 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007277}
7278
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007279void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7280 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007282}
7283
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007284void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007285 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007286}
7287
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007288void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007290}
7291
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007292void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7293 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007295}
7296
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007297void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007299}
7300
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007301void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007303}
7304
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007305void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007306 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007307}
7308
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007309void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007311}
7312
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007313void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007314 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007315}
7316
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007317void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7318 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007319 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007320}
7321
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007322void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007323 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007324}
7325
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007326void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007327 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007328}
7329
Michael Wrightd02c5b62014-02-10 15:10:22 -08007330TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007331 addConfigurationProperty("touch.deviceType", "touchScreen");
7332 prepareDisplay(DISPLAY_ORIENTATION_0);
7333 prepareAxes(POSITION);
7334 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007335 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007336
arthurhungdcef2dc2020-08-11 14:47:50 +08007337 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007338
7339 NotifyMotionArgs motionArgs;
7340
7341 // Two fingers down at once.
7342 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7343 processPosition(mapper, x1, y1);
7344 processMTSync(mapper);
7345 processPosition(mapper, x2, y2);
7346 processMTSync(mapper);
7347 processSync(mapper);
7348
7349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7350 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7351 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7352 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7353 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7354 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7355 ASSERT_EQ(0, motionArgs.flags);
7356 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7357 ASSERT_EQ(0, motionArgs.buttonState);
7358 ASSERT_EQ(0, motionArgs.edgeFlags);
7359 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7360 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7363 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7364 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7365 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7366 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7367
7368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7369 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7370 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7371 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7372 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007373 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007374 ASSERT_EQ(0, motionArgs.flags);
7375 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7376 ASSERT_EQ(0, motionArgs.buttonState);
7377 ASSERT_EQ(0, motionArgs.edgeFlags);
7378 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7379 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7380 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7381 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7382 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7384 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7386 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7387 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7388 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7389 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7390
7391 // Move.
7392 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7393 processPosition(mapper, x1, y1);
7394 processMTSync(mapper);
7395 processPosition(mapper, x2, y2);
7396 processMTSync(mapper);
7397 processSync(mapper);
7398
7399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7400 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7401 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7402 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7403 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7405 ASSERT_EQ(0, motionArgs.flags);
7406 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7407 ASSERT_EQ(0, motionArgs.buttonState);
7408 ASSERT_EQ(0, motionArgs.edgeFlags);
7409 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7410 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7411 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7412 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7413 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7415 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7417 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7418 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7419 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7420 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7421
7422 // First finger up.
7423 x2 += 15; y2 -= 20;
7424 processPosition(mapper, x2, y2);
7425 processMTSync(mapper);
7426 processSync(mapper);
7427
7428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7429 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7430 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7431 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7432 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007433 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007434 ASSERT_EQ(0, motionArgs.flags);
7435 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7436 ASSERT_EQ(0, motionArgs.buttonState);
7437 ASSERT_EQ(0, motionArgs.edgeFlags);
7438 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7439 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7441 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7442 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7444 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7446 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7447 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7448 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7449 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7450
7451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7452 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7453 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7454 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7455 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7457 ASSERT_EQ(0, motionArgs.flags);
7458 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7459 ASSERT_EQ(0, motionArgs.buttonState);
7460 ASSERT_EQ(0, motionArgs.edgeFlags);
7461 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7462 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7463 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7465 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7466 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7467 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7468 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7469
7470 // Move.
7471 x2 += 20; y2 -= 25;
7472 processPosition(mapper, x2, y2);
7473 processMTSync(mapper);
7474 processSync(mapper);
7475
7476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7477 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7478 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7479 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7480 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7482 ASSERT_EQ(0, motionArgs.flags);
7483 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7484 ASSERT_EQ(0, motionArgs.buttonState);
7485 ASSERT_EQ(0, motionArgs.edgeFlags);
7486 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7487 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7488 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7490 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7491 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7492 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7493 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7494
7495 // New finger down.
7496 int32_t x3 = 700, y3 = 300;
7497 processPosition(mapper, x2, y2);
7498 processMTSync(mapper);
7499 processPosition(mapper, x3, y3);
7500 processMTSync(mapper);
7501 processSync(mapper);
7502
7503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7504 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7505 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7506 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7507 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007508 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007509 ASSERT_EQ(0, motionArgs.flags);
7510 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7511 ASSERT_EQ(0, motionArgs.buttonState);
7512 ASSERT_EQ(0, motionArgs.edgeFlags);
7513 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7514 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7515 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7516 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7517 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7519 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7521 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7522 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7523 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7524 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7525
7526 // Second finger up.
7527 x3 += 30; y3 -= 20;
7528 processPosition(mapper, x3, y3);
7529 processMTSync(mapper);
7530 processSync(mapper);
7531
7532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7533 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7534 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7535 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7536 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007537 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007538 ASSERT_EQ(0, motionArgs.flags);
7539 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7540 ASSERT_EQ(0, motionArgs.buttonState);
7541 ASSERT_EQ(0, motionArgs.edgeFlags);
7542 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7543 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7544 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7545 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7546 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7548 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7549 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7550 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7551 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7552 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7553 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7554
7555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7556 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7557 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7558 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7559 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7560 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7561 ASSERT_EQ(0, motionArgs.flags);
7562 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7563 ASSERT_EQ(0, motionArgs.buttonState);
7564 ASSERT_EQ(0, motionArgs.edgeFlags);
7565 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7566 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7567 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7569 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7570 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7571 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7572 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7573
7574 // Last finger up.
7575 processMTSync(mapper);
7576 processSync(mapper);
7577
7578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7579 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7580 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7581 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7582 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7583 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7584 ASSERT_EQ(0, motionArgs.flags);
7585 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7586 ASSERT_EQ(0, motionArgs.buttonState);
7587 ASSERT_EQ(0, motionArgs.edgeFlags);
7588 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7589 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7590 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7592 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7593 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7594 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7595 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7596
7597 // Should not have sent any more keys or motions.
7598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7600}
7601
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007602TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7603 addConfigurationProperty("touch.deviceType", "touchScreen");
7604 prepareDisplay(DISPLAY_ORIENTATION_0);
7605
7606 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7607 /*fuzz*/ 0, /*resolution*/ 10);
7608 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7609 /*fuzz*/ 0, /*resolution*/ 11);
7610 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7611 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7612 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7613 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7614 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7615 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7616 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7617 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7618
7619 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7620
7621 // X and Y axes
7622 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7623 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7624 // Touch major and minor
7625 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7626 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7627 // Tool major and minor
7628 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7629 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7630}
7631
7632TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7633 addConfigurationProperty("touch.deviceType", "touchScreen");
7634 prepareDisplay(DISPLAY_ORIENTATION_0);
7635
7636 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7637 /*fuzz*/ 0, /*resolution*/ 10);
7638 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7639 /*fuzz*/ 0, /*resolution*/ 11);
7640
7641 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7642
7643 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7644
7645 // Touch major and minor
7646 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7647 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7648 // Tool major and minor
7649 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7650 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7651}
7652
Michael Wrightd02c5b62014-02-10 15:10:22 -08007653TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007654 addConfigurationProperty("touch.deviceType", "touchScreen");
7655 prepareDisplay(DISPLAY_ORIENTATION_0);
7656 prepareAxes(POSITION | ID);
7657 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007658 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007659
arthurhungdcef2dc2020-08-11 14:47:50 +08007660 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007661
7662 NotifyMotionArgs motionArgs;
7663
7664 // Two fingers down at once.
7665 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7666 processPosition(mapper, x1, y1);
7667 processId(mapper, 1);
7668 processMTSync(mapper);
7669 processPosition(mapper, x2, y2);
7670 processId(mapper, 2);
7671 processMTSync(mapper);
7672 processSync(mapper);
7673
7674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7675 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7676 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7677 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7678 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7679 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7680 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7681
7682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007683 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007684 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7685 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7686 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7687 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7688 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7689 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7690 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7692 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7693
7694 // Move.
7695 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7696 processPosition(mapper, x1, y1);
7697 processId(mapper, 1);
7698 processMTSync(mapper);
7699 processPosition(mapper, x2, y2);
7700 processId(mapper, 2);
7701 processMTSync(mapper);
7702 processSync(mapper);
7703
7704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7705 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7706 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7707 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7708 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7709 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7712 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7714 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7715
7716 // First finger up.
7717 x2 += 15; y2 -= 20;
7718 processPosition(mapper, x2, y2);
7719 processId(mapper, 2);
7720 processMTSync(mapper);
7721 processSync(mapper);
7722
7723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007724 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007725 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7726 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7727 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7728 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7729 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7730 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7731 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7733 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7734
7735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7736 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7737 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7738 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7739 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7741 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7742
7743 // Move.
7744 x2 += 20; y2 -= 25;
7745 processPosition(mapper, x2, y2);
7746 processId(mapper, 2);
7747 processMTSync(mapper);
7748 processSync(mapper);
7749
7750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7751 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7752 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7753 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7754 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7756 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7757
7758 // New finger down.
7759 int32_t x3 = 700, y3 = 300;
7760 processPosition(mapper, x2, y2);
7761 processId(mapper, 2);
7762 processMTSync(mapper);
7763 processPosition(mapper, x3, y3);
7764 processId(mapper, 3);
7765 processMTSync(mapper);
7766 processSync(mapper);
7767
7768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007769 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007770 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7771 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7772 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7773 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7774 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7776 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7778 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7779
7780 // Second finger up.
7781 x3 += 30; y3 -= 20;
7782 processPosition(mapper, x3, y3);
7783 processId(mapper, 3);
7784 processMTSync(mapper);
7785 processSync(mapper);
7786
7787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007788 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007789 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7790 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7791 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7792 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7793 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7795 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7797 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7798
7799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7800 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7801 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7802 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7803 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7805 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7806
7807 // Last finger up.
7808 processMTSync(mapper);
7809 processSync(mapper);
7810
7811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7812 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7813 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7814 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7815 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7817 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7818
7819 // Should not have sent any more keys or motions.
7820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7822}
7823
7824TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007825 addConfigurationProperty("touch.deviceType", "touchScreen");
7826 prepareDisplay(DISPLAY_ORIENTATION_0);
7827 prepareAxes(POSITION | ID | SLOT);
7828 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007829 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007830
arthurhungdcef2dc2020-08-11 14:47:50 +08007831 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007832
7833 NotifyMotionArgs motionArgs;
7834
7835 // Two fingers down at once.
7836 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7837 processPosition(mapper, x1, y1);
7838 processId(mapper, 1);
7839 processSlot(mapper, 1);
7840 processPosition(mapper, x2, y2);
7841 processId(mapper, 2);
7842 processSync(mapper);
7843
7844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7845 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7846 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7847 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7848 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7849 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7850 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7851
7852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007853 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007854 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7855 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7856 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7857 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7858 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7860 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7862 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7863
7864 // Move.
7865 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7866 processSlot(mapper, 0);
7867 processPosition(mapper, x1, y1);
7868 processSlot(mapper, 1);
7869 processPosition(mapper, x2, y2);
7870 processSync(mapper);
7871
7872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7874 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7875 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7876 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7877 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7878 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7880 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7881 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7882 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7883
7884 // First finger up.
7885 x2 += 15; y2 -= 20;
7886 processSlot(mapper, 0);
7887 processId(mapper, -1);
7888 processSlot(mapper, 1);
7889 processPosition(mapper, x2, y2);
7890 processSync(mapper);
7891
7892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007893 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007894 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7895 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7896 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7897 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7898 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7899 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7900 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7902 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7903
7904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7905 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7906 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7907 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7908 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7909 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7910 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7911
7912 // Move.
7913 x2 += 20; y2 -= 25;
7914 processPosition(mapper, x2, y2);
7915 processSync(mapper);
7916
7917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7918 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7919 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7920 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7921 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7922 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7923 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7924
7925 // New finger down.
7926 int32_t x3 = 700, y3 = 300;
7927 processPosition(mapper, x2, y2);
7928 processSlot(mapper, 0);
7929 processId(mapper, 3);
7930 processPosition(mapper, x3, y3);
7931 processSync(mapper);
7932
7933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007934 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007935 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7936 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7937 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7938 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7939 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7940 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7941 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7942 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7943 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7944
7945 // Second finger up.
7946 x3 += 30; y3 -= 20;
7947 processSlot(mapper, 1);
7948 processId(mapper, -1);
7949 processSlot(mapper, 0);
7950 processPosition(mapper, x3, y3);
7951 processSync(mapper);
7952
7953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007954 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007955 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7956 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7957 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7958 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7959 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7960 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7961 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7963 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7964
7965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7967 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7968 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7969 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7970 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7971 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7972
7973 // Last finger up.
7974 processId(mapper, -1);
7975 processSync(mapper);
7976
7977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7978 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7979 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7980 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7981 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7983 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7984
7985 // Should not have sent any more keys or motions.
7986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7988}
7989
7990TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007991 addConfigurationProperty("touch.deviceType", "touchScreen");
7992 prepareDisplay(DISPLAY_ORIENTATION_0);
7993 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007994 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007995
7996 // These calculations are based on the input device calibration documentation.
7997 int32_t rawX = 100;
7998 int32_t rawY = 200;
7999 int32_t rawTouchMajor = 7;
8000 int32_t rawTouchMinor = 6;
8001 int32_t rawToolMajor = 9;
8002 int32_t rawToolMinor = 8;
8003 int32_t rawPressure = 11;
8004 int32_t rawDistance = 0;
8005 int32_t rawOrientation = 3;
8006 int32_t id = 5;
8007
8008 float x = toDisplayX(rawX);
8009 float y = toDisplayY(rawY);
8010 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8011 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8012 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8013 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8014 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8015 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8016 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8017 float distance = float(rawDistance);
8018
8019 processPosition(mapper, rawX, rawY);
8020 processTouchMajor(mapper, rawTouchMajor);
8021 processTouchMinor(mapper, rawTouchMinor);
8022 processToolMajor(mapper, rawToolMajor);
8023 processToolMinor(mapper, rawToolMinor);
8024 processPressure(mapper, rawPressure);
8025 processOrientation(mapper, rawOrientation);
8026 processDistance(mapper, rawDistance);
8027 processId(mapper, id);
8028 processMTSync(mapper);
8029 processSync(mapper);
8030
8031 NotifyMotionArgs args;
8032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8033 ASSERT_EQ(0, args.pointerProperties[0].id);
8034 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8035 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8036 orientation, distance));
8037}
8038
8039TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008040 addConfigurationProperty("touch.deviceType", "touchScreen");
8041 prepareDisplay(DISPLAY_ORIENTATION_0);
8042 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8043 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008044 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008045
8046 // These calculations are based on the input device calibration documentation.
8047 int32_t rawX = 100;
8048 int32_t rawY = 200;
8049 int32_t rawTouchMajor = 140;
8050 int32_t rawTouchMinor = 120;
8051 int32_t rawToolMajor = 180;
8052 int32_t rawToolMinor = 160;
8053
8054 float x = toDisplayX(rawX);
8055 float y = toDisplayY(rawY);
8056 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8057 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8058 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8059 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8060 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8061
8062 processPosition(mapper, rawX, rawY);
8063 processTouchMajor(mapper, rawTouchMajor);
8064 processTouchMinor(mapper, rawTouchMinor);
8065 processToolMajor(mapper, rawToolMajor);
8066 processToolMinor(mapper, rawToolMinor);
8067 processMTSync(mapper);
8068 processSync(mapper);
8069
8070 NotifyMotionArgs args;
8071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8073 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8074}
8075
8076TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008077 addConfigurationProperty("touch.deviceType", "touchScreen");
8078 prepareDisplay(DISPLAY_ORIENTATION_0);
8079 prepareAxes(POSITION | TOUCH | TOOL);
8080 addConfigurationProperty("touch.size.calibration", "diameter");
8081 addConfigurationProperty("touch.size.scale", "10");
8082 addConfigurationProperty("touch.size.bias", "160");
8083 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008084 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008085
8086 // These calculations are based on the input device calibration documentation.
8087 // Note: We only provide a single common touch/tool value because the device is assumed
8088 // not to emit separate values for each pointer (isSummed = 1).
8089 int32_t rawX = 100;
8090 int32_t rawY = 200;
8091 int32_t rawX2 = 150;
8092 int32_t rawY2 = 250;
8093 int32_t rawTouchMajor = 5;
8094 int32_t rawToolMajor = 8;
8095
8096 float x = toDisplayX(rawX);
8097 float y = toDisplayY(rawY);
8098 float x2 = toDisplayX(rawX2);
8099 float y2 = toDisplayY(rawY2);
8100 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8101 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8102 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8103
8104 processPosition(mapper, rawX, rawY);
8105 processTouchMajor(mapper, rawTouchMajor);
8106 processToolMajor(mapper, rawToolMajor);
8107 processMTSync(mapper);
8108 processPosition(mapper, rawX2, rawY2);
8109 processTouchMajor(mapper, rawTouchMajor);
8110 processToolMajor(mapper, rawToolMajor);
8111 processMTSync(mapper);
8112 processSync(mapper);
8113
8114 NotifyMotionArgs args;
8115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8116 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8117
8118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008119 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008120 ASSERT_EQ(size_t(2), args.pointerCount);
8121 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8122 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8123 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8124 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8125}
8126
8127TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008128 addConfigurationProperty("touch.deviceType", "touchScreen");
8129 prepareDisplay(DISPLAY_ORIENTATION_0);
8130 prepareAxes(POSITION | TOUCH | TOOL);
8131 addConfigurationProperty("touch.size.calibration", "area");
8132 addConfigurationProperty("touch.size.scale", "43");
8133 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008134 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008135
8136 // These calculations are based on the input device calibration documentation.
8137 int32_t rawX = 100;
8138 int32_t rawY = 200;
8139 int32_t rawTouchMajor = 5;
8140 int32_t rawToolMajor = 8;
8141
8142 float x = toDisplayX(rawX);
8143 float y = toDisplayY(rawY);
8144 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8145 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8146 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8147
8148 processPosition(mapper, rawX, rawY);
8149 processTouchMajor(mapper, rawTouchMajor);
8150 processToolMajor(mapper, rawToolMajor);
8151 processMTSync(mapper);
8152 processSync(mapper);
8153
8154 NotifyMotionArgs args;
8155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8156 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8157 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8158}
8159
8160TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008161 addConfigurationProperty("touch.deviceType", "touchScreen");
8162 prepareDisplay(DISPLAY_ORIENTATION_0);
8163 prepareAxes(POSITION | PRESSURE);
8164 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8165 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008166 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008167
Michael Wrightaa449c92017-12-13 21:21:43 +00008168 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008169 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008170 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8171 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8172 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8173
Michael Wrightd02c5b62014-02-10 15:10:22 -08008174 // These calculations are based on the input device calibration documentation.
8175 int32_t rawX = 100;
8176 int32_t rawY = 200;
8177 int32_t rawPressure = 60;
8178
8179 float x = toDisplayX(rawX);
8180 float y = toDisplayY(rawY);
8181 float pressure = float(rawPressure) * 0.01f;
8182
8183 processPosition(mapper, rawX, rawY);
8184 processPressure(mapper, rawPressure);
8185 processMTSync(mapper);
8186 processSync(mapper);
8187
8188 NotifyMotionArgs args;
8189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8190 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8191 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8192}
8193
8194TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008195 addConfigurationProperty("touch.deviceType", "touchScreen");
8196 prepareDisplay(DISPLAY_ORIENTATION_0);
8197 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008198 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008199
8200 NotifyMotionArgs motionArgs;
8201 NotifyKeyArgs keyArgs;
8202
8203 processId(mapper, 1);
8204 processPosition(mapper, 100, 200);
8205 processSync(mapper);
8206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8207 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8208 ASSERT_EQ(0, motionArgs.buttonState);
8209
8210 // press BTN_LEFT, release BTN_LEFT
8211 processKey(mapper, BTN_LEFT, 1);
8212 processSync(mapper);
8213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8215 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8216
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8218 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8219 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8220
Michael Wrightd02c5b62014-02-10 15:10:22 -08008221 processKey(mapper, BTN_LEFT, 0);
8222 processSync(mapper);
8223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008224 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008225 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008226
8227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008229 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008230
8231 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8232 processKey(mapper, BTN_RIGHT, 1);
8233 processKey(mapper, BTN_MIDDLE, 1);
8234 processSync(mapper);
8235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8237 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8238 motionArgs.buttonState);
8239
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8241 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8242 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8243
8244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8245 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8246 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8247 motionArgs.buttonState);
8248
Michael Wrightd02c5b62014-02-10 15:10:22 -08008249 processKey(mapper, BTN_RIGHT, 0);
8250 processSync(mapper);
8251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008252 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008253 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008254
8255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008256 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008257 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008258
8259 processKey(mapper, BTN_MIDDLE, 0);
8260 processSync(mapper);
8261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008262 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008263 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008264
8265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008266 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008267 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008268
8269 // press BTN_BACK, release BTN_BACK
8270 processKey(mapper, BTN_BACK, 1);
8271 processSync(mapper);
8272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8273 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8274 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008275
Michael Wrightd02c5b62014-02-10 15:10:22 -08008276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008278 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8279
8280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8281 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8282 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008283
8284 processKey(mapper, BTN_BACK, 0);
8285 processSync(mapper);
8286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008287 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008288 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008289
8290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008291 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008292 ASSERT_EQ(0, motionArgs.buttonState);
8293
Michael Wrightd02c5b62014-02-10 15:10:22 -08008294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8295 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8296 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8297
8298 // press BTN_SIDE, release BTN_SIDE
8299 processKey(mapper, BTN_SIDE, 1);
8300 processSync(mapper);
8301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8302 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8303 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008304
Michael Wrightd02c5b62014-02-10 15:10:22 -08008305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008306 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008307 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8308
8309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8310 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8311 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008312
8313 processKey(mapper, BTN_SIDE, 0);
8314 processSync(mapper);
8315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008316 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008317 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008318
8319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008320 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008321 ASSERT_EQ(0, motionArgs.buttonState);
8322
Michael Wrightd02c5b62014-02-10 15:10:22 -08008323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8324 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8325 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8326
8327 // press BTN_FORWARD, release BTN_FORWARD
8328 processKey(mapper, BTN_FORWARD, 1);
8329 processSync(mapper);
8330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8331 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8332 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008333
Michael Wrightd02c5b62014-02-10 15:10:22 -08008334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008335 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008336 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8337
8338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8339 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8340 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008341
8342 processKey(mapper, BTN_FORWARD, 0);
8343 processSync(mapper);
8344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008345 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008346 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008347
8348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008350 ASSERT_EQ(0, motionArgs.buttonState);
8351
Michael Wrightd02c5b62014-02-10 15:10:22 -08008352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8353 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8354 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8355
8356 // press BTN_EXTRA, release BTN_EXTRA
8357 processKey(mapper, BTN_EXTRA, 1);
8358 processSync(mapper);
8359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8360 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8361 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008362
Michael Wrightd02c5b62014-02-10 15:10:22 -08008363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008364 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008365 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8366
8367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8368 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8369 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008370
8371 processKey(mapper, BTN_EXTRA, 0);
8372 processSync(mapper);
8373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008374 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008375 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008376
8377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008378 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008379 ASSERT_EQ(0, motionArgs.buttonState);
8380
Michael Wrightd02c5b62014-02-10 15:10:22 -08008381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8382 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8383 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8384
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8386
Michael Wrightd02c5b62014-02-10 15:10:22 -08008387 // press BTN_STYLUS, release BTN_STYLUS
8388 processKey(mapper, BTN_STYLUS, 1);
8389 processSync(mapper);
8390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8391 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008392 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8393
8394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8395 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8396 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008397
8398 processKey(mapper, BTN_STYLUS, 0);
8399 processSync(mapper);
8400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008401 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008402 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008403
8404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008405 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008406 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008407
8408 // press BTN_STYLUS2, release BTN_STYLUS2
8409 processKey(mapper, BTN_STYLUS2, 1);
8410 processSync(mapper);
8411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8412 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008413 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8414
8415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8416 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8417 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008418
8419 processKey(mapper, BTN_STYLUS2, 0);
8420 processSync(mapper);
8421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008422 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008423 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008424
8425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008426 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008427 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008428
8429 // release touch
8430 processId(mapper, -1);
8431 processSync(mapper);
8432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8433 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8434 ASSERT_EQ(0, motionArgs.buttonState);
8435}
8436
8437TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008438 addConfigurationProperty("touch.deviceType", "touchScreen");
8439 prepareDisplay(DISPLAY_ORIENTATION_0);
8440 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008441 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008442
8443 NotifyMotionArgs motionArgs;
8444
8445 // default tool type is finger
8446 processId(mapper, 1);
8447 processPosition(mapper, 100, 200);
8448 processSync(mapper);
8449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8450 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8451 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8452
8453 // eraser
8454 processKey(mapper, BTN_TOOL_RUBBER, 1);
8455 processSync(mapper);
8456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8458 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8459
8460 // stylus
8461 processKey(mapper, BTN_TOOL_RUBBER, 0);
8462 processKey(mapper, BTN_TOOL_PEN, 1);
8463 processSync(mapper);
8464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8465 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8466 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8467
8468 // brush
8469 processKey(mapper, BTN_TOOL_PEN, 0);
8470 processKey(mapper, BTN_TOOL_BRUSH, 1);
8471 processSync(mapper);
8472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8473 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8475
8476 // pencil
8477 processKey(mapper, BTN_TOOL_BRUSH, 0);
8478 processKey(mapper, BTN_TOOL_PENCIL, 1);
8479 processSync(mapper);
8480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8482 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8483
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008484 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008485 processKey(mapper, BTN_TOOL_PENCIL, 0);
8486 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8487 processSync(mapper);
8488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8490 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8491
8492 // mouse
8493 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8494 processKey(mapper, BTN_TOOL_MOUSE, 1);
8495 processSync(mapper);
8496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8497 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8498 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8499
8500 // lens
8501 processKey(mapper, BTN_TOOL_MOUSE, 0);
8502 processKey(mapper, BTN_TOOL_LENS, 1);
8503 processSync(mapper);
8504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8505 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8506 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8507
8508 // double-tap
8509 processKey(mapper, BTN_TOOL_LENS, 0);
8510 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8511 processSync(mapper);
8512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8513 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8515
8516 // triple-tap
8517 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8518 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8519 processSync(mapper);
8520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8522 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8523
8524 // quad-tap
8525 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8526 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8527 processSync(mapper);
8528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8529 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8530 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8531
8532 // finger
8533 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8534 processKey(mapper, BTN_TOOL_FINGER, 1);
8535 processSync(mapper);
8536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8538 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8539
8540 // stylus trumps finger
8541 processKey(mapper, BTN_TOOL_PEN, 1);
8542 processSync(mapper);
8543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8544 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8545 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8546
8547 // eraser trumps stylus
8548 processKey(mapper, BTN_TOOL_RUBBER, 1);
8549 processSync(mapper);
8550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8553
8554 // mouse trumps eraser
8555 processKey(mapper, BTN_TOOL_MOUSE, 1);
8556 processSync(mapper);
8557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8559 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8560
8561 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8562 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8563 processSync(mapper);
8564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8566 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8567
8568 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8569 processToolType(mapper, MT_TOOL_PEN);
8570 processSync(mapper);
8571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8572 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8573 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8574
8575 // back to default tool type
8576 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8577 processKey(mapper, BTN_TOOL_MOUSE, 0);
8578 processKey(mapper, BTN_TOOL_RUBBER, 0);
8579 processKey(mapper, BTN_TOOL_PEN, 0);
8580 processKey(mapper, BTN_TOOL_FINGER, 0);
8581 processSync(mapper);
8582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8583 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8585}
8586
8587TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008588 addConfigurationProperty("touch.deviceType", "touchScreen");
8589 prepareDisplay(DISPLAY_ORIENTATION_0);
8590 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008591 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008592 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008593
8594 NotifyMotionArgs motionArgs;
8595
8596 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8597 processId(mapper, 1);
8598 processPosition(mapper, 100, 200);
8599 processSync(mapper);
8600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8601 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8603 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8604
8605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8606 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8608 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8609
8610 // move a little
8611 processPosition(mapper, 150, 250);
8612 processSync(mapper);
8613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8614 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8616 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8617
8618 // down when BTN_TOUCH is pressed, pressure defaults to 1
8619 processKey(mapper, BTN_TOUCH, 1);
8620 processSync(mapper);
8621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8622 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8624 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8625
8626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8627 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8629 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8630
8631 // up when BTN_TOUCH is released, hover restored
8632 processKey(mapper, BTN_TOUCH, 0);
8633 processSync(mapper);
8634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8635 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8637 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8638
8639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8640 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8642 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8643
8644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8645 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8646 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8647 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8648
8649 // exit hover when pointer goes away
8650 processId(mapper, -1);
8651 processSync(mapper);
8652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8653 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8655 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8656}
8657
8658TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008659 addConfigurationProperty("touch.deviceType", "touchScreen");
8660 prepareDisplay(DISPLAY_ORIENTATION_0);
8661 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008662 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008663
8664 NotifyMotionArgs motionArgs;
8665
8666 // initially hovering because pressure is 0
8667 processId(mapper, 1);
8668 processPosition(mapper, 100, 200);
8669 processPressure(mapper, 0);
8670 processSync(mapper);
8671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8672 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8673 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8674 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8675
8676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8677 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8679 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8680
8681 // move a little
8682 processPosition(mapper, 150, 250);
8683 processSync(mapper);
8684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8685 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8686 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8687 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8688
8689 // down when pressure becomes non-zero
8690 processPressure(mapper, RAW_PRESSURE_MAX);
8691 processSync(mapper);
8692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8693 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8694 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8695 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8696
8697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8698 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8699 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8700 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8701
8702 // up when pressure becomes 0, hover restored
8703 processPressure(mapper, 0);
8704 processSync(mapper);
8705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8706 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8707 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8708 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8709
8710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8711 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8712 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8713 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8714
8715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8716 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8718 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8719
8720 // exit hover when pointer goes away
8721 processId(mapper, -1);
8722 processSync(mapper);
8723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8724 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8726 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8727}
8728
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008729/**
8730 * Set the input device port <--> display port associations, and check that the
8731 * events are routed to the display that matches the display port.
8732 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8733 */
8734TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008735 const std::string usb2 = "USB2";
8736 const uint8_t hdmi1 = 0;
8737 const uint8_t hdmi2 = 1;
8738 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008739 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008740
8741 addConfigurationProperty("touch.deviceType", "touchScreen");
8742 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008743 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008744
8745 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8746 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8747
8748 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8749 // for this input device is specified, and the matching viewport is not present,
8750 // the input device should be disabled (at the mapper level).
8751
8752 // Add viewport for display 2 on hdmi2
8753 prepareSecondaryDisplay(type, hdmi2);
8754 // Send a touch event
8755 processPosition(mapper, 100, 100);
8756 processSync(mapper);
8757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8758
8759 // Add viewport for display 1 on hdmi1
8760 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8761 // Send a touch event again
8762 processPosition(mapper, 100, 100);
8763 processSync(mapper);
8764
8765 NotifyMotionArgs args;
8766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8767 ASSERT_EQ(DISPLAY_ID, args.displayId);
8768}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008769
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008770TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8771 addConfigurationProperty("touch.deviceType", "touchScreen");
8772 prepareAxes(POSITION);
8773 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8774
8775 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8776
8777 prepareDisplay(DISPLAY_ORIENTATION_0);
8778 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8779
8780 // Send a touch event
8781 processPosition(mapper, 100, 100);
8782 processSync(mapper);
8783
8784 NotifyMotionArgs args;
8785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8786 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8787}
8788
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008789TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008790 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008791 std::shared_ptr<FakePointerController> fakePointerController =
8792 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008793 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008794 fakePointerController->setPosition(100, 200);
8795 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008796 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008797
Garfield Tan888a6a42020-01-09 11:39:16 -08008798 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008799 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008800
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008801 prepareDisplay(DISPLAY_ORIENTATION_0);
8802 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008803 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008804
8805 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008806 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008807
8808 NotifyMotionArgs motionArgs;
8809 processPosition(mapper, 100, 100);
8810 processSync(mapper);
8811
8812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8813 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8814 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8815}
8816
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008817/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008818 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8819 */
8820TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8821 addConfigurationProperty("touch.deviceType", "touchScreen");
8822 prepareAxes(POSITION);
8823 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8824
8825 prepareDisplay(DISPLAY_ORIENTATION_0);
8826 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8827 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8828 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8829 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8830
8831 NotifyMotionArgs args;
8832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8833 ASSERT_EQ(26, args.readTime);
8834
8835 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8836 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8837 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8838
8839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8840 ASSERT_EQ(33, args.readTime);
8841}
8842
8843/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008844 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8845 * events should not be delivered to the listener.
8846 */
8847TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8848 addConfigurationProperty("touch.deviceType", "touchScreen");
8849 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8850 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8851 ViewportType::INTERNAL);
8852 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8853 prepareAxes(POSITION);
8854 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8855
8856 NotifyMotionArgs motionArgs;
8857 processPosition(mapper, 100, 100);
8858 processSync(mapper);
8859
8860 mFakeListener->assertNotifyMotionWasNotCalled();
8861}
8862
Garfield Tanc734e4f2021-01-15 20:01:39 -08008863TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8864 addConfigurationProperty("touch.deviceType", "touchScreen");
8865 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8866 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8867 ViewportType::INTERNAL);
8868 std::optional<DisplayViewport> optionalDisplayViewport =
8869 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8870 ASSERT_TRUE(optionalDisplayViewport.has_value());
8871 DisplayViewport displayViewport = *optionalDisplayViewport;
8872
8873 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8874 prepareAxes(POSITION);
8875 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8876
8877 // Finger down
8878 int32_t x = 100, y = 100;
8879 processPosition(mapper, x, y);
8880 processSync(mapper);
8881
8882 NotifyMotionArgs motionArgs;
8883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8884 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8885
8886 // Deactivate display viewport
8887 displayViewport.isActive = false;
8888 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8889 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8890
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008891 // The ongoing touch should be canceled immediately
8892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8893 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8894
8895 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008896 x += 10, y += 10;
8897 processPosition(mapper, x, y);
8898 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008900
8901 // Reactivate display viewport
8902 displayViewport.isActive = true;
8903 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8904 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8905
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008906 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008907 x += 10, y += 10;
8908 processPosition(mapper, x, y);
8909 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8911 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008912}
8913
Arthur Hung7c645402019-01-25 17:45:42 +08008914TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8915 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008916 prepareAxes(POSITION | ID | SLOT);
8917 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008918 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008919
8920 // Create the second touch screen device, and enable multi fingers.
8921 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008922 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008923 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008924 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008925 std::shared_ptr<InputDevice> device2 =
8926 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008927 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008928
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008929 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8930 0 /*flat*/, 0 /*fuzz*/);
8931 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8932 0 /*flat*/, 0 /*fuzz*/);
8933 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8934 0 /*flat*/, 0 /*fuzz*/);
8935 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8936 0 /*flat*/, 0 /*fuzz*/);
8937 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8938 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8939 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008940
8941 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008942 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008943 std::list<NotifyArgs> unused =
8944 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8945 0 /*changes*/);
8946 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08008947
8948 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008949 std::shared_ptr<FakePointerController> fakePointerController =
8950 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008951 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008952
8953 // Setup policy for associated displays and show touches.
8954 const uint8_t hdmi1 = 0;
8955 const uint8_t hdmi2 = 1;
8956 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8957 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8958 mFakePolicy->setShowTouches(true);
8959
8960 // Create displays.
8961 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008962 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008963
8964 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008965 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8966 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8967 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008968
8969 // Two fingers down at default display.
8970 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8971 processPosition(mapper, x1, y1);
8972 processId(mapper, 1);
8973 processSlot(mapper, 1);
8974 processPosition(mapper, x2, y2);
8975 processId(mapper, 2);
8976 processSync(mapper);
8977
8978 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8979 fakePointerController->getSpots().find(DISPLAY_ID);
8980 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8981 ASSERT_EQ(size_t(2), iter->second.size());
8982
8983 // Two fingers down at second display.
8984 processPosition(mapper2, x1, y1);
8985 processId(mapper2, 1);
8986 processSlot(mapper2, 1);
8987 processPosition(mapper2, x2, y2);
8988 processId(mapper2, 2);
8989 processSync(mapper2);
8990
8991 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8992 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8993 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008994
8995 // Disable the show touches configuration and ensure the spots are cleared.
8996 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008997 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8998 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00008999
9000 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009001}
9002
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009003TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009004 prepareAxes(POSITION);
9005 addConfigurationProperty("touch.deviceType", "touchScreen");
9006 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009007 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009008
9009 NotifyMotionArgs motionArgs;
9010 // Unrotated video frame
9011 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9012 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009013 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009014 processPosition(mapper, 100, 200);
9015 processSync(mapper);
9016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9017 ASSERT_EQ(frames, motionArgs.videoFrames);
9018
9019 // Subsequent touch events should not have any videoframes
9020 // This is implemented separately in FakeEventHub,
9021 // but that should match the behaviour of TouchVideoDevice.
9022 processPosition(mapper, 200, 200);
9023 processSync(mapper);
9024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9025 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9026}
9027
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009028TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009029 prepareAxes(POSITION);
9030 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009031 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009032 // Unrotated video frame
9033 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9034 NotifyMotionArgs motionArgs;
9035
9036 // Test all 4 orientations
9037 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009038 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9039 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9040 clearViewports();
9041 prepareDisplay(orientation);
9042 std::vector<TouchVideoFrame> frames{frame};
9043 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9044 processPosition(mapper, 100, 200);
9045 processSync(mapper);
9046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9047 ASSERT_EQ(frames, motionArgs.videoFrames);
9048 }
9049}
9050
9051TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9052 prepareAxes(POSITION);
9053 addConfigurationProperty("touch.deviceType", "touchScreen");
9054 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9055 // orientation-aware are affected by display rotation.
9056 addConfigurationProperty("touch.orientationAware", "0");
9057 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9058 // Unrotated video frame
9059 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9060 NotifyMotionArgs motionArgs;
9061
9062 // Test all 4 orientations
9063 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009064 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9065 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9066 clearViewports();
9067 prepareDisplay(orientation);
9068 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009069 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009070 processPosition(mapper, 100, 200);
9071 processSync(mapper);
9072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009073 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9074 // compared to the display. This is so that when the window transform (which contains the
9075 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9076 // window's coordinate space.
9077 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009078 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009079
9080 // Release finger.
9081 processSync(mapper);
9082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009083 }
9084}
9085
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009086TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009087 prepareAxes(POSITION);
9088 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009089 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009090 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9091 // so mix these.
9092 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9093 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9094 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9095 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9096 NotifyMotionArgs motionArgs;
9097
9098 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009099 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009100 processPosition(mapper, 100, 200);
9101 processSync(mapper);
9102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009103 ASSERT_EQ(frames, motionArgs.videoFrames);
9104}
9105
9106TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9107 prepareAxes(POSITION);
9108 addConfigurationProperty("touch.deviceType", "touchScreen");
9109 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9110 // orientation-aware are affected by display rotation.
9111 addConfigurationProperty("touch.orientationAware", "0");
9112 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9113 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9114 // so mix these.
9115 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9116 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9117 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9118 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9119 NotifyMotionArgs motionArgs;
9120
9121 prepareDisplay(DISPLAY_ORIENTATION_90);
9122 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9123 processPosition(mapper, 100, 200);
9124 processSync(mapper);
9125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9126 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9127 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9128 // compared to the display. This is so that when the window transform (which contains the
9129 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9130 // window's coordinate space.
9131 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
9132 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009133 ASSERT_EQ(frames, motionArgs.videoFrames);
9134}
9135
Arthur Hung9da14732019-09-02 16:16:58 +08009136/**
9137 * If we had defined port associations, but the viewport is not ready, the touch device would be
9138 * expected to be disabled, and it should be enabled after the viewport has found.
9139 */
9140TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009141 constexpr uint8_t hdmi2 = 1;
9142 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009143 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009144
9145 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9146
9147 addConfigurationProperty("touch.deviceType", "touchScreen");
9148 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009149 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009150
9151 ASSERT_EQ(mDevice->isEnabled(), false);
9152
9153 // Add display on hdmi2, the device should be enabled and can receive touch event.
9154 prepareSecondaryDisplay(type, hdmi2);
9155 ASSERT_EQ(mDevice->isEnabled(), true);
9156
9157 // Send a touch event.
9158 processPosition(mapper, 100, 100);
9159 processSync(mapper);
9160
9161 NotifyMotionArgs args;
9162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9163 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9164}
9165
Arthur Hung421eb1c2020-01-16 00:09:42 +08009166TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009167 addConfigurationProperty("touch.deviceType", "touchScreen");
9168 prepareDisplay(DISPLAY_ORIENTATION_0);
9169 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009170 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009171
9172 NotifyMotionArgs motionArgs;
9173
9174 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9175 // finger down
9176 processId(mapper, 1);
9177 processPosition(mapper, x1, y1);
9178 processSync(mapper);
9179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9180 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9181 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9182
9183 // finger move
9184 processId(mapper, 1);
9185 processPosition(mapper, x2, y2);
9186 processSync(mapper);
9187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9189 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9190
9191 // finger up.
9192 processId(mapper, -1);
9193 processSync(mapper);
9194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9195 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9196 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9197
9198 // new finger down
9199 processId(mapper, 1);
9200 processPosition(mapper, x3, y3);
9201 processSync(mapper);
9202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9203 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9204 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9205}
9206
9207/**
arthurhungcc7f9802020-04-30 17:55:40 +08009208 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9209 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009210 */
arthurhungcc7f9802020-04-30 17:55:40 +08009211TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009212 addConfigurationProperty("touch.deviceType", "touchScreen");
9213 prepareDisplay(DISPLAY_ORIENTATION_0);
9214 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009215 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009216
9217 NotifyMotionArgs motionArgs;
9218
9219 // default tool type is finger
9220 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009221 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009222 processPosition(mapper, x1, y1);
9223 processSync(mapper);
9224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9225 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9226 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9227
9228 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9229 processToolType(mapper, MT_TOOL_PALM);
9230 processSync(mapper);
9231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9232 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9233
9234 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009235 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009236 processPosition(mapper, x2, y2);
9237 processSync(mapper);
9238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9239
9240 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009241 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009242 processSync(mapper);
9243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9244
9245 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009246 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009247 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009248 processPosition(mapper, x3, y3);
9249 processSync(mapper);
9250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9251 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9252 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9253}
9254
arthurhungbf89a482020-04-17 17:37:55 +08009255/**
arthurhungcc7f9802020-04-30 17:55:40 +08009256 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9257 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009258 */
arthurhungcc7f9802020-04-30 17:55:40 +08009259TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009260 addConfigurationProperty("touch.deviceType", "touchScreen");
9261 prepareDisplay(DISPLAY_ORIENTATION_0);
9262 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9263 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9264
9265 NotifyMotionArgs motionArgs;
9266
9267 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009268 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9269 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009270 processPosition(mapper, x1, y1);
9271 processSync(mapper);
9272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9273 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9274 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9275
9276 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009277 processSlot(mapper, SECOND_SLOT);
9278 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009279 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009280 processSync(mapper);
9281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009282 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9284
9285 // If the tool type of the first finger changes to MT_TOOL_PALM,
9286 // we expect to receive ACTION_POINTER_UP with cancel flag.
9287 processSlot(mapper, FIRST_SLOT);
9288 processId(mapper, FIRST_TRACKING_ID);
9289 processToolType(mapper, MT_TOOL_PALM);
9290 processSync(mapper);
9291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009292 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009293 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9294
9295 // The following MOVE events of second finger should be processed.
9296 processSlot(mapper, SECOND_SLOT);
9297 processId(mapper, SECOND_TRACKING_ID);
9298 processPosition(mapper, x2 + 1, y2 + 1);
9299 processSync(mapper);
9300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9301 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9302 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9303
9304 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9305 // it. Second finger receive move.
9306 processSlot(mapper, FIRST_SLOT);
9307 processId(mapper, INVALID_TRACKING_ID);
9308 processSync(mapper);
9309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9311 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9312
9313 // Second finger keeps moving.
9314 processSlot(mapper, SECOND_SLOT);
9315 processId(mapper, SECOND_TRACKING_ID);
9316 processPosition(mapper, x2 + 2, y2 + 2);
9317 processSync(mapper);
9318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9320 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9321
9322 // Second finger up.
9323 processId(mapper, INVALID_TRACKING_ID);
9324 processSync(mapper);
9325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9326 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9327 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9328}
9329
9330/**
9331 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9332 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9333 */
9334TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9335 addConfigurationProperty("touch.deviceType", "touchScreen");
9336 prepareDisplay(DISPLAY_ORIENTATION_0);
9337 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9338 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9339
9340 NotifyMotionArgs motionArgs;
9341
9342 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9343 // First finger down.
9344 processId(mapper, FIRST_TRACKING_ID);
9345 processPosition(mapper, x1, y1);
9346 processSync(mapper);
9347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9348 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9349 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9350
9351 // Second finger down.
9352 processSlot(mapper, SECOND_SLOT);
9353 processId(mapper, SECOND_TRACKING_ID);
9354 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009355 processSync(mapper);
9356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009357 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9359
arthurhungcc7f9802020-04-30 17:55:40 +08009360 // If the tool type of the first finger changes to MT_TOOL_PALM,
9361 // we expect to receive ACTION_POINTER_UP with cancel flag.
9362 processSlot(mapper, FIRST_SLOT);
9363 processId(mapper, FIRST_TRACKING_ID);
9364 processToolType(mapper, MT_TOOL_PALM);
9365 processSync(mapper);
9366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009367 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009368 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9369
9370 // Second finger keeps moving.
9371 processSlot(mapper, SECOND_SLOT);
9372 processId(mapper, SECOND_TRACKING_ID);
9373 processPosition(mapper, x2 + 1, y2 + 1);
9374 processSync(mapper);
9375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9376 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9377
9378 // second finger becomes palm, receive cancel due to only 1 finger is active.
9379 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009380 processToolType(mapper, MT_TOOL_PALM);
9381 processSync(mapper);
9382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9383 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9384
arthurhungcc7f9802020-04-30 17:55:40 +08009385 // third finger down.
9386 processSlot(mapper, THIRD_SLOT);
9387 processId(mapper, THIRD_TRACKING_ID);
9388 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009389 processPosition(mapper, x3, y3);
9390 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9392 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009394 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9395
9396 // third finger move
9397 processId(mapper, THIRD_TRACKING_ID);
9398 processPosition(mapper, x3 + 1, y3 + 1);
9399 processSync(mapper);
9400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9402
9403 // first finger up, third finger receive move.
9404 processSlot(mapper, FIRST_SLOT);
9405 processId(mapper, INVALID_TRACKING_ID);
9406 processSync(mapper);
9407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9408 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9409 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9410
9411 // second finger up, third finger receive move.
9412 processSlot(mapper, SECOND_SLOT);
9413 processId(mapper, INVALID_TRACKING_ID);
9414 processSync(mapper);
9415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9417 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9418
9419 // third finger up.
9420 processSlot(mapper, THIRD_SLOT);
9421 processId(mapper, INVALID_TRACKING_ID);
9422 processSync(mapper);
9423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9424 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9425 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9426}
9427
9428/**
9429 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9430 * and the active finger could still be allowed to receive the events
9431 */
9432TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9433 addConfigurationProperty("touch.deviceType", "touchScreen");
9434 prepareDisplay(DISPLAY_ORIENTATION_0);
9435 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9436 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9437
9438 NotifyMotionArgs motionArgs;
9439
9440 // default tool type is finger
9441 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9442 processId(mapper, FIRST_TRACKING_ID);
9443 processPosition(mapper, x1, y1);
9444 processSync(mapper);
9445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9446 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9447 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9448
9449 // Second finger down.
9450 processSlot(mapper, SECOND_SLOT);
9451 processId(mapper, SECOND_TRACKING_ID);
9452 processPosition(mapper, x2, y2);
9453 processSync(mapper);
9454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009455 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009456 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9457
9458 // If the tool type of the second finger changes to MT_TOOL_PALM,
9459 // we expect to receive ACTION_POINTER_UP with cancel flag.
9460 processId(mapper, SECOND_TRACKING_ID);
9461 processToolType(mapper, MT_TOOL_PALM);
9462 processSync(mapper);
9463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009464 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009465 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9466
9467 // The following MOVE event should be processed.
9468 processSlot(mapper, FIRST_SLOT);
9469 processId(mapper, FIRST_TRACKING_ID);
9470 processPosition(mapper, x1 + 1, y1 + 1);
9471 processSync(mapper);
9472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9473 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9474 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9475
9476 // second finger up.
9477 processSlot(mapper, SECOND_SLOT);
9478 processId(mapper, INVALID_TRACKING_ID);
9479 processSync(mapper);
9480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9482
9483 // first finger keep moving
9484 processSlot(mapper, FIRST_SLOT);
9485 processId(mapper, FIRST_TRACKING_ID);
9486 processPosition(mapper, x1 + 2, y1 + 2);
9487 processSync(mapper);
9488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9490
9491 // first finger up.
9492 processId(mapper, INVALID_TRACKING_ID);
9493 processSync(mapper);
9494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9495 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9496 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009497}
9498
Arthur Hung9ad18942021-06-19 02:04:46 +00009499/**
9500 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9501 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9502 * cause slot be valid again.
9503 */
9504TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9505 addConfigurationProperty("touch.deviceType", "touchScreen");
9506 prepareDisplay(DISPLAY_ORIENTATION_0);
9507 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9508 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9509
9510 NotifyMotionArgs motionArgs;
9511
9512 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9513 // First finger down.
9514 processId(mapper, FIRST_TRACKING_ID);
9515 processPosition(mapper, x1, y1);
9516 processPressure(mapper, RAW_PRESSURE_MAX);
9517 processSync(mapper);
9518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9519 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9520 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9521
9522 // First finger move.
9523 processId(mapper, FIRST_TRACKING_ID);
9524 processPosition(mapper, x1 + 1, y1 + 1);
9525 processPressure(mapper, RAW_PRESSURE_MAX);
9526 processSync(mapper);
9527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9529 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9530
9531 // Second finger down.
9532 processSlot(mapper, SECOND_SLOT);
9533 processId(mapper, SECOND_TRACKING_ID);
9534 processPosition(mapper, x2, y2);
9535 processPressure(mapper, RAW_PRESSURE_MAX);
9536 processSync(mapper);
9537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009538 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009539 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9540
9541 // second finger up with some unexpected data.
9542 processSlot(mapper, SECOND_SLOT);
9543 processId(mapper, INVALID_TRACKING_ID);
9544 processPosition(mapper, x2, y2);
9545 processSync(mapper);
9546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009547 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009548 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9549
9550 // first finger up with some unexpected data.
9551 processSlot(mapper, FIRST_SLOT);
9552 processId(mapper, INVALID_TRACKING_ID);
9553 processPosition(mapper, x2, y2);
9554 processPressure(mapper, RAW_PRESSURE_MAX);
9555 processSync(mapper);
9556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9557 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9558 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9559}
9560
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009561TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9562 addConfigurationProperty("touch.deviceType", "touchScreen");
9563 prepareDisplay(DISPLAY_ORIENTATION_0);
9564 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9565 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9566
9567 // First finger down.
9568 processId(mapper, FIRST_TRACKING_ID);
9569 processPosition(mapper, 100, 200);
9570 processPressure(mapper, RAW_PRESSURE_MAX);
9571 processSync(mapper);
9572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9573 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9574
9575 // Second finger down.
9576 processSlot(mapper, SECOND_SLOT);
9577 processId(mapper, SECOND_TRACKING_ID);
9578 processPosition(mapper, 300, 400);
9579 processPressure(mapper, RAW_PRESSURE_MAX);
9580 processSync(mapper);
9581 ASSERT_NO_FATAL_FAILURE(
9582 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9583
9584 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009585 // preserved. Resetting should cancel the ongoing gesture.
9586 resetMapper(mapper, ARBITRARY_TIME);
9587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9588 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009589
9590 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9591 // the existing touch state to generate a down event.
9592 processPosition(mapper, 301, 302);
9593 processSync(mapper);
9594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9595 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9597 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9598
9599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9600}
9601
9602TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9603 addConfigurationProperty("touch.deviceType", "touchScreen");
9604 prepareDisplay(DISPLAY_ORIENTATION_0);
9605 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9606 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9607
9608 // First finger touches down and releases.
9609 processId(mapper, FIRST_TRACKING_ID);
9610 processPosition(mapper, 100, 200);
9611 processPressure(mapper, RAW_PRESSURE_MAX);
9612 processSync(mapper);
9613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9614 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9615 processId(mapper, INVALID_TRACKING_ID);
9616 processSync(mapper);
9617 ASSERT_NO_FATAL_FAILURE(
9618 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9619
9620 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9621 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009622 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9624
9625 // Send an empty sync frame. Since there are no pointers, no events are generated.
9626 processSync(mapper);
9627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9628}
9629
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009630// --- MultiTouchInputMapperTest_ExternalDevice ---
9631
9632class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9633protected:
Chris Yea52ade12020-08-27 16:49:20 -07009634 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009635};
9636
9637/**
9638 * Expect fallback to internal viewport if device is external and external viewport is not present.
9639 */
9640TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9641 prepareAxes(POSITION);
9642 addConfigurationProperty("touch.deviceType", "touchScreen");
9643 prepareDisplay(DISPLAY_ORIENTATION_0);
9644 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9645
9646 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9647
9648 NotifyMotionArgs motionArgs;
9649
9650 // Expect the event to be sent to the internal viewport,
9651 // because an external viewport is not present.
9652 processPosition(mapper, 100, 100);
9653 processSync(mapper);
9654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9655 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9656
9657 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009658 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009659 processPosition(mapper, 100, 100);
9660 processSync(mapper);
9661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9662 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9663}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009664
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009665TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9666 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9667 std::shared_ptr<FakePointerController> fakePointerController =
9668 std::make_shared<FakePointerController>();
9669 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9670 fakePointerController->setPosition(0, 0);
9671 fakePointerController->setButtonState(0);
9672
9673 // prepare device and capture
9674 prepareDisplay(DISPLAY_ORIENTATION_0);
9675 prepareAxes(POSITION | ID | SLOT);
9676 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9677 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9678 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009679 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009680 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9681
9682 // captured touchpad should be a touchpad source
9683 NotifyDeviceResetArgs resetArgs;
9684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9685 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9686
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009687 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009688
9689 const InputDeviceInfo::MotionRange* relRangeX =
9690 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9691 ASSERT_NE(relRangeX, nullptr);
9692 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9693 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9694 const InputDeviceInfo::MotionRange* relRangeY =
9695 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9696 ASSERT_NE(relRangeY, nullptr);
9697 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9698 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9699
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009700 // run captured pointer tests - note that this is unscaled, so input listener events should be
9701 // identical to what the hardware sends (accounting for any
9702 // calibration).
9703 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009704 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009705 processId(mapper, 1);
9706 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9707 processKey(mapper, BTN_TOUCH, 1);
9708 processSync(mapper);
9709
9710 // expect coord[0] to contain initial location of touch 0
9711 NotifyMotionArgs args;
9712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9713 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9714 ASSERT_EQ(1U, args.pointerCount);
9715 ASSERT_EQ(0, args.pointerProperties[0].id);
9716 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9717 ASSERT_NO_FATAL_FAILURE(
9718 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9719
9720 // FINGER 1 DOWN
9721 processSlot(mapper, 1);
9722 processId(mapper, 2);
9723 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9724 processSync(mapper);
9725
9726 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009728 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009729 ASSERT_EQ(2U, args.pointerCount);
9730 ASSERT_EQ(0, args.pointerProperties[0].id);
9731 ASSERT_EQ(1, args.pointerProperties[1].id);
9732 ASSERT_NO_FATAL_FAILURE(
9733 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9734 ASSERT_NO_FATAL_FAILURE(
9735 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9736
9737 // FINGER 1 MOVE
9738 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9739 processSync(mapper);
9740
9741 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9742 // from move
9743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9744 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9745 ASSERT_NO_FATAL_FAILURE(
9746 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9747 ASSERT_NO_FATAL_FAILURE(
9748 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9749
9750 // FINGER 0 MOVE
9751 processSlot(mapper, 0);
9752 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9753 processSync(mapper);
9754
9755 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9757 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9758 ASSERT_NO_FATAL_FAILURE(
9759 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9760 ASSERT_NO_FATAL_FAILURE(
9761 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9762
9763 // BUTTON DOWN
9764 processKey(mapper, BTN_LEFT, 1);
9765 processSync(mapper);
9766
9767 // touchinputmapper design sends a move before button press
9768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9769 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9771 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9772
9773 // BUTTON UP
9774 processKey(mapper, BTN_LEFT, 0);
9775 processSync(mapper);
9776
9777 // touchinputmapper design sends a move after button release
9778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9779 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9781 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9782
9783 // FINGER 0 UP
9784 processId(mapper, -1);
9785 processSync(mapper);
9786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9787 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9788
9789 // FINGER 1 MOVE
9790 processSlot(mapper, 1);
9791 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9792 processSync(mapper);
9793
9794 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9796 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9797 ASSERT_EQ(1U, args.pointerCount);
9798 ASSERT_EQ(1, args.pointerProperties[0].id);
9799 ASSERT_NO_FATAL_FAILURE(
9800 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9801
9802 // FINGER 1 UP
9803 processId(mapper, -1);
9804 processKey(mapper, BTN_TOUCH, 0);
9805 processSync(mapper);
9806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9807 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9808
9809 // non captured touchpad should be a mouse source
9810 mFakePolicy->setPointerCapture(false);
9811 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9813 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9814}
9815
9816TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9817 std::shared_ptr<FakePointerController> fakePointerController =
9818 std::make_shared<FakePointerController>();
9819 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9820 fakePointerController->setPosition(0, 0);
9821 fakePointerController->setButtonState(0);
9822
9823 // prepare device and capture
9824 prepareDisplay(DISPLAY_ORIENTATION_0);
9825 prepareAxes(POSITION | ID | SLOT);
9826 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9827 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009828 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009829 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9830 // run uncaptured pointer tests - pushes out generic events
9831 // FINGER 0 DOWN
9832 processId(mapper, 3);
9833 processPosition(mapper, 100, 100);
9834 processKey(mapper, BTN_TOUCH, 1);
9835 processSync(mapper);
9836
9837 // start at (100,100), cursor should be at (0,0) * scale
9838 NotifyMotionArgs args;
9839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9840 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9841 ASSERT_NO_FATAL_FAILURE(
9842 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9843
9844 // FINGER 0 MOVE
9845 processPosition(mapper, 200, 200);
9846 processSync(mapper);
9847
9848 // compute scaling to help with touch position checking
9849 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9850 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9851 float scale =
9852 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9853
9854 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9856 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9857 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9858 0, 0, 0, 0, 0, 0, 0));
9859}
9860
9861TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9862 std::shared_ptr<FakePointerController> fakePointerController =
9863 std::make_shared<FakePointerController>();
9864
9865 prepareDisplay(DISPLAY_ORIENTATION_0);
9866 prepareAxes(POSITION | ID | SLOT);
9867 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009868 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009869 mFakePolicy->setPointerCapture(false);
9870 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9871
9872 // uncaptured touchpad should be a pointer device
9873 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
9874
9875 // captured touchpad should be a touchpad device
9876 mFakePolicy->setPointerCapture(true);
9877 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9878 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9879}
9880
HQ Liue6983c72022-04-19 22:14:56 +00009881class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9882protected:
9883 float mPointerMovementScale;
9884 float mPointerXZoomScale;
9885 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9886 addConfigurationProperty("touch.deviceType", "pointer");
9887 std::shared_ptr<FakePointerController> fakePointerController =
9888 std::make_shared<FakePointerController>();
9889 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9890 fakePointerController->setPosition(0, 0);
9891 fakePointerController->setButtonState(0);
9892 prepareDisplay(DISPLAY_ORIENTATION_0);
9893
9894 prepareAxes(POSITION);
9895 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9896 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9897 // needs to be disabled, and the pointer gesture needs to be enabled.
9898 mFakePolicy->setPointerCapture(false);
9899 mFakePolicy->setPointerGestureEnabled(true);
9900 mFakePolicy->setPointerController(fakePointerController);
9901
9902 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9903 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9904 mPointerMovementScale =
9905 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9906 mPointerXZoomScale =
9907 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9908 }
9909
9910 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9911 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9912 /*flat*/ 0,
9913 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9914 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9915 /*flat*/ 0,
9916 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9917 }
9918};
9919
9920/**
9921 * Two fingers down on a pointer mode touch pad. The width
9922 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9923 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9924 * be greater than the both value to be freeform gesture, so that after two
9925 * fingers start to move downwards, the gesture should be swipe.
9926 */
9927TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9928 // The min freeform gesture width is 25units/mm x 30mm = 750
9929 // which is greater than fraction of the diagnal length of the touchpad (349).
9930 // Thus, MaxSwipWidth is 750.
9931 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9932 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9933 NotifyMotionArgs motionArgs;
9934
9935 // Two fingers down at once.
9936 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9937 // Pointer's initial position is used the [0,0] coordinate.
9938 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9939
9940 processId(mapper, FIRST_TRACKING_ID);
9941 processPosition(mapper, x1, y1);
9942 processMTSync(mapper);
9943 processId(mapper, SECOND_TRACKING_ID);
9944 processPosition(mapper, x2, y2);
9945 processMTSync(mapper);
9946 processSync(mapper);
9947
9948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9949 ASSERT_EQ(1U, motionArgs.pointerCount);
9950 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9951 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009952 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009953 ASSERT_NO_FATAL_FAILURE(
9954 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9955
9956 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9957 // that there should be 1 pointer.
9958 int32_t movingDistance = 200;
9959 y1 += movingDistance;
9960 y2 += movingDistance;
9961
9962 processId(mapper, FIRST_TRACKING_ID);
9963 processPosition(mapper, x1, y1);
9964 processMTSync(mapper);
9965 processId(mapper, SECOND_TRACKING_ID);
9966 processPosition(mapper, x2, y2);
9967 processMTSync(mapper);
9968 processSync(mapper);
9969
9970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9971 ASSERT_EQ(1U, motionArgs.pointerCount);
9972 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9973 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009974 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009975 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9976 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9977 0, 0, 0, 0));
9978}
9979
9980/**
9981 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9982 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9983 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9984 * value to be freeform gesture, so that after two fingers start to move downwards,
9985 * the gesture should be swipe.
9986 */
9987TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9988 // The min freeform gesture width is 5units/mm x 30mm = 150
9989 // which is greater than fraction of the diagnal length of the touchpad (349).
9990 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
9991 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
9992 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9993 NotifyMotionArgs motionArgs;
9994
9995 // Two fingers down at once.
9996 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9997 // Pointer's initial position is used the [0,0] coordinate.
9998 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9999
10000 processId(mapper, FIRST_TRACKING_ID);
10001 processPosition(mapper, x1, y1);
10002 processMTSync(mapper);
10003 processId(mapper, SECOND_TRACKING_ID);
10004 processPosition(mapper, x2, y2);
10005 processMTSync(mapper);
10006 processSync(mapper);
10007
10008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10009 ASSERT_EQ(1U, motionArgs.pointerCount);
10010 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10011 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010012 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010013 ASSERT_NO_FATAL_FAILURE(
10014 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10015
10016 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10017 // and there should be 1 pointer.
10018 int32_t movingDistance = 200;
10019 y1 += movingDistance;
10020 y2 += movingDistance;
10021
10022 processId(mapper, FIRST_TRACKING_ID);
10023 processPosition(mapper, x1, y1);
10024 processMTSync(mapper);
10025 processId(mapper, SECOND_TRACKING_ID);
10026 processPosition(mapper, x2, y2);
10027 processMTSync(mapper);
10028 processSync(mapper);
10029
10030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10031 ASSERT_EQ(1U, motionArgs.pointerCount);
10032 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10033 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010034 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010035 // New coordinate is the scaled relative coordinate from the initial coordinate.
10036 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10037 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10038 0, 0, 0, 0));
10039}
10040
10041/**
10042 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10043 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10044 * freeform gestures after two fingers start to move downwards.
10045 */
10046TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
10047 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10048 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10049
10050 NotifyMotionArgs motionArgs;
10051
10052 // Two fingers down at once. Wider than the max swipe width.
10053 // The gesture is expected to be PRESS, then transformed to FREEFORM
10054 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10055
10056 processId(mapper, FIRST_TRACKING_ID);
10057 processPosition(mapper, x1, y1);
10058 processMTSync(mapper);
10059 processId(mapper, SECOND_TRACKING_ID);
10060 processPosition(mapper, x2, y2);
10061 processMTSync(mapper);
10062 processSync(mapper);
10063
10064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10065 ASSERT_EQ(1U, motionArgs.pointerCount);
10066 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10067 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010068 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010069 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10070 ASSERT_NO_FATAL_FAILURE(
10071 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10072
10073 int32_t movingDistance = 200;
10074
10075 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10076 // then two down events for two pointers.
10077 y1 += movingDistance;
10078 y2 += movingDistance;
10079
10080 processId(mapper, FIRST_TRACKING_ID);
10081 processPosition(mapper, x1, y1);
10082 processMTSync(mapper);
10083 processId(mapper, SECOND_TRACKING_ID);
10084 processPosition(mapper, x2, y2);
10085 processMTSync(mapper);
10086 processSync(mapper);
10087
10088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10089 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10090 ASSERT_EQ(1U, motionArgs.pointerCount);
10091 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10093 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10094 ASSERT_EQ(1U, motionArgs.pointerCount);
10095 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10097 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010098 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010099 ASSERT_EQ(2U, motionArgs.pointerCount);
10100 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010102 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010103 // Two pointers' scaled relative coordinates from their initial centroid.
10104 // Initial y coordinates are 0 as y1 and y2 have the same value.
10105 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10106 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10107 // When pointers move, the new coordinates equal to the initial coordinates plus
10108 // scaled moving distance.
10109 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10110 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10111 0, 0, 0, 0));
10112 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10113 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10114 0, 0, 0, 0));
10115
10116 // Move two fingers down again, expect one MOVE motion event.
10117 y1 += movingDistance;
10118 y2 += movingDistance;
10119
10120 processId(mapper, FIRST_TRACKING_ID);
10121 processPosition(mapper, x1, y1);
10122 processMTSync(mapper);
10123 processId(mapper, SECOND_TRACKING_ID);
10124 processPosition(mapper, x2, y2);
10125 processMTSync(mapper);
10126 processSync(mapper);
10127
10128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10129 ASSERT_EQ(2U, motionArgs.pointerCount);
10130 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10131 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010132 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10134 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10135 0, 0, 0, 0, 0));
10136 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10137 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10138 0, 0, 0, 0, 0));
10139}
10140
Harry Cutts39b7ca22022-10-05 15:55:48 +000010141TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
10142 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10143 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10144 NotifyMotionArgs motionArgs;
10145
10146 // Place two fingers down.
10147 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10148
10149 processId(mapper, FIRST_TRACKING_ID);
10150 processPosition(mapper, x1, y1);
10151 processMTSync(mapper);
10152 processId(mapper, SECOND_TRACKING_ID);
10153 processPosition(mapper, x2, y2);
10154 processMTSync(mapper);
10155 processSync(mapper);
10156
10157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10158 ASSERT_EQ(1U, motionArgs.pointerCount);
10159 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10160 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10161 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10162 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10163
10164 // Move the two fingers down and to the left.
10165 int32_t movingDistance = 200;
10166 x1 -= movingDistance;
10167 y1 += movingDistance;
10168 x2 -= movingDistance;
10169 y2 += movingDistance;
10170
10171 processId(mapper, FIRST_TRACKING_ID);
10172 processPosition(mapper, x1, y1);
10173 processMTSync(mapper);
10174 processId(mapper, SECOND_TRACKING_ID);
10175 processPosition(mapper, x2, y2);
10176 processMTSync(mapper);
10177 processSync(mapper);
10178
10179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10180 ASSERT_EQ(1U, motionArgs.pointerCount);
10181 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10182 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10183 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10184 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10185}
10186
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010187// --- JoystickInputMapperTest ---
10188
10189class JoystickInputMapperTest : public InputMapperTest {
10190protected:
10191 static const int32_t RAW_X_MIN;
10192 static const int32_t RAW_X_MAX;
10193 static const int32_t RAW_Y_MIN;
10194 static const int32_t RAW_Y_MAX;
10195
10196 void SetUp() override {
10197 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10198 }
10199 void prepareAxes() {
10200 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10201 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10202 }
10203
10204 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10205 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10206 }
10207
10208 void processSync(JoystickInputMapper& mapper) {
10209 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10210 }
10211
10212 void prepareVirtualDisplay(int32_t orientation) {
10213 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10214 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10215 NO_PORT, ViewportType::VIRTUAL);
10216 }
10217};
10218
10219const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10220const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10221const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10222const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10223
10224TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10225 prepareAxes();
10226 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10227
10228 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10229
10230 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
10231
10232 // Send an axis event
10233 processAxis(mapper, ABS_X, 100);
10234 processSync(mapper);
10235
10236 NotifyMotionArgs args;
10237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10238 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10239
10240 // Send another axis event
10241 processAxis(mapper, ABS_Y, 100);
10242 processSync(mapper);
10243
10244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10245 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10246}
10247
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010248// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010249
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010250class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010251protected:
10252 static const char* DEVICE_NAME;
10253 static const char* DEVICE_LOCATION;
10254 static const int32_t DEVICE_ID;
10255 static const int32_t DEVICE_GENERATION;
10256 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010257 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010258 static const int32_t EVENTHUB_ID;
10259
10260 std::shared_ptr<FakeEventHub> mFakeEventHub;
10261 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010262 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010263 std::unique_ptr<InstrumentedInputReader> mReader;
10264 std::shared_ptr<InputDevice> mDevice;
10265
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010266 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010267 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010268 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010269 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010270 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010271 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010272 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10273 }
10274
10275 void SetUp() override { SetUp(DEVICE_CLASSES); }
10276
10277 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010278 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010279 mFakePolicy.clear();
10280 }
10281
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010282 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010283 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10284 mReader->requestRefreshConfiguration(changes);
10285 mReader->loopOnce();
10286 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010287 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010288 }
10289
10290 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10291 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010292 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010293 InputDeviceIdentifier identifier;
10294 identifier.name = name;
10295 identifier.location = location;
10296 std::shared_ptr<InputDevice> device =
10297 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10298 identifier);
10299 mReader->pushNextDevice(device);
10300 mFakeEventHub->addDevice(eventHubId, name, classes);
10301 mReader->loopOnce();
10302 return device;
10303 }
10304
10305 template <class T, typename... Args>
10306 T& addControllerAndConfigure(Args... args) {
10307 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10308
10309 return controller;
10310 }
10311};
10312
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010313const char* PeripheralControllerTest::DEVICE_NAME = "device";
10314const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10315const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10316const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10317const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010318const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10319 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010320const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010321
10322// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010323class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010324protected:
10325 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010326 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010327 }
10328};
10329
10330TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010331 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010332
10333 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
10334 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
10335}
10336
10337TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010338 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010339
10340 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
10341 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
10342}
10343
10344// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010345class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010346protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010347 void SetUp() override {
10348 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10349 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010350};
10351
Chris Ye85758332021-05-16 23:05:17 -070010352TEST_F(LightControllerTest, MonoLight) {
10353 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010354 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010355 .maxBrightness = 255,
10356 .flags = InputLightClass::BRIGHTNESS,
10357 .path = ""};
10358 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010359
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010360 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010361 InputDeviceInfo info;
10362 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010363 std::vector<InputDeviceLightInfo> lights = info.getLights();
10364 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010365 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10366 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10367
10368 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10369 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10370}
10371
10372TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10373 RawLightInfo infoMono = {.id = 1,
10374 .name = "mono_keyboard_backlight",
10375 .maxBrightness = 255,
10376 .flags = InputLightClass::BRIGHTNESS |
10377 InputLightClass::KEYBOARD_BACKLIGHT,
10378 .path = ""};
10379 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10380
10381 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10382 InputDeviceInfo info;
10383 controller.populateDeviceInfo(&info);
10384 std::vector<InputDeviceLightInfo> lights = info.getLights();
10385 ASSERT_EQ(1U, lights.size());
10386 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10387 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010388
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010389 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10390 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010391}
10392
10393TEST_F(LightControllerTest, RGBLight) {
10394 RawLightInfo infoRed = {.id = 1,
10395 .name = "red",
10396 .maxBrightness = 255,
10397 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10398 .path = ""};
10399 RawLightInfo infoGreen = {.id = 2,
10400 .name = "green",
10401 .maxBrightness = 255,
10402 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10403 .path = ""};
10404 RawLightInfo infoBlue = {.id = 3,
10405 .name = "blue",
10406 .maxBrightness = 255,
10407 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10408 .path = ""};
10409 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10410 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10411 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10412
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010413 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010414 InputDeviceInfo info;
10415 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010416 std::vector<InputDeviceLightInfo> lights = info.getLights();
10417 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010418 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10419 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10420 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10421
10422 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10423 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10424}
10425
10426TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10427 RawLightInfo infoRed = {.id = 1,
10428 .name = "red_keyboard_backlight",
10429 .maxBrightness = 255,
10430 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10431 InputLightClass::KEYBOARD_BACKLIGHT,
10432 .path = ""};
10433 RawLightInfo infoGreen = {.id = 2,
10434 .name = "green_keyboard_backlight",
10435 .maxBrightness = 255,
10436 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10437 InputLightClass::KEYBOARD_BACKLIGHT,
10438 .path = ""};
10439 RawLightInfo infoBlue = {.id = 3,
10440 .name = "blue_keyboard_backlight",
10441 .maxBrightness = 255,
10442 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10443 InputLightClass::KEYBOARD_BACKLIGHT,
10444 .path = ""};
10445 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10446 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10447 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10448
10449 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10450 InputDeviceInfo info;
10451 controller.populateDeviceInfo(&info);
10452 std::vector<InputDeviceLightInfo> lights = info.getLights();
10453 ASSERT_EQ(1U, lights.size());
10454 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10455 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10456 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10457
10458 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10459 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10460}
10461
10462TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10463 RawLightInfo infoRed = {.id = 1,
10464 .name = "red",
10465 .maxBrightness = 255,
10466 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10467 .path = ""};
10468 RawLightInfo infoGreen = {.id = 2,
10469 .name = "green",
10470 .maxBrightness = 255,
10471 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10472 .path = ""};
10473 RawLightInfo infoBlue = {.id = 3,
10474 .name = "blue",
10475 .maxBrightness = 255,
10476 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10477 .path = ""};
10478 RawLightInfo infoGlobal = {.id = 3,
10479 .name = "global_keyboard_backlight",
10480 .maxBrightness = 255,
10481 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10482 InputLightClass::KEYBOARD_BACKLIGHT,
10483 .path = ""};
10484 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10485 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10486 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10487 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10488
10489 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10490 InputDeviceInfo info;
10491 controller.populateDeviceInfo(&info);
10492 std::vector<InputDeviceLightInfo> lights = info.getLights();
10493 ASSERT_EQ(1U, lights.size());
10494 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10495 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10496 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010497
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010498 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10499 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010500}
10501
10502TEST_F(LightControllerTest, MultiColorRGBLight) {
10503 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010504 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010505 .maxBrightness = 255,
10506 .flags = InputLightClass::BRIGHTNESS |
10507 InputLightClass::MULTI_INTENSITY |
10508 InputLightClass::MULTI_INDEX,
10509 .path = ""};
10510
10511 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10512
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010513 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010514 InputDeviceInfo info;
10515 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010516 std::vector<InputDeviceLightInfo> lights = info.getLights();
10517 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010518 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10519 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10520 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10521
10522 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10523 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10524}
10525
10526TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10527 RawLightInfo infoColor = {.id = 1,
10528 .name = "multi_color_keyboard_backlight",
10529 .maxBrightness = 255,
10530 .flags = InputLightClass::BRIGHTNESS |
10531 InputLightClass::MULTI_INTENSITY |
10532 InputLightClass::MULTI_INDEX |
10533 InputLightClass::KEYBOARD_BACKLIGHT,
10534 .path = ""};
10535
10536 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10537
10538 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10539 InputDeviceInfo info;
10540 controller.populateDeviceInfo(&info);
10541 std::vector<InputDeviceLightInfo> lights = info.getLights();
10542 ASSERT_EQ(1U, lights.size());
10543 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10544 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10545 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010546
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010547 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10548 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010549}
10550
10551TEST_F(LightControllerTest, PlayerIdLight) {
10552 RawLightInfo info1 = {.id = 1,
10553 .name = "player1",
10554 .maxBrightness = 255,
10555 .flags = InputLightClass::BRIGHTNESS,
10556 .path = ""};
10557 RawLightInfo info2 = {.id = 2,
10558 .name = "player2",
10559 .maxBrightness = 255,
10560 .flags = InputLightClass::BRIGHTNESS,
10561 .path = ""};
10562 RawLightInfo info3 = {.id = 3,
10563 .name = "player3",
10564 .maxBrightness = 255,
10565 .flags = InputLightClass::BRIGHTNESS,
10566 .path = ""};
10567 RawLightInfo info4 = {.id = 4,
10568 .name = "player4",
10569 .maxBrightness = 255,
10570 .flags = InputLightClass::BRIGHTNESS,
10571 .path = ""};
10572 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10573 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10574 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10575 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10576
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010577 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010578 InputDeviceInfo info;
10579 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010580 std::vector<InputDeviceLightInfo> lights = info.getLights();
10581 ASSERT_EQ(1U, lights.size());
10582 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010583 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10584 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010585
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010586 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10587 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10588 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010589}
10590
Michael Wrightd02c5b62014-02-10 15:10:22 -080010591} // namespace android