blob: 10acdd26553d973bff4815aa317b593311b0b77f [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;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000258 std::optional<int32_t /*deviceId*/> mStylusGestureNotified GUARDED_BY(mLock){};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259
260protected:
Chris Yea52ade12020-08-27 16:49:20 -0700261 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800262
263public:
264 FakeInputReaderPolicy() {
265 }
266
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700267 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800268 waitForInputDevices([](bool devicesChanged) {
269 if (!devicesChanged) {
270 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
271 }
272 });
273 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700274
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800275 void assertInputDevicesNotChanged() {
276 waitForInputDevices([](bool devicesChanged) {
277 if (devicesChanged) {
278 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
279 }
280 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700281 }
282
Prabir Pradhanda20b172022-09-26 17:01:18 +0000283 void assertStylusGestureNotified(int32_t deviceId) {
284 std::scoped_lock lock(mLock);
285 ASSERT_TRUE(mStylusGestureNotified);
286 ASSERT_EQ(deviceId, *mStylusGestureNotified);
287 mStylusGestureNotified.reset();
288 }
289
290 void assertStylusGestureNotNotified() {
291 std::scoped_lock lock(mLock);
292 ASSERT_FALSE(mStylusGestureNotified);
293 }
294
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700295 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100296 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100297 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700298 }
299
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700300 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
301 return mConfig.getDisplayViewportByUniqueId(uniqueId);
302 }
303 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
304 return mConfig.getDisplayViewportByType(type);
305 }
306
307 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
308 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700309 }
310
Prabir Pradhan5632d622021-09-06 07:57:20 -0700311 void addDisplayViewport(DisplayViewport viewport) {
312 mViewports.push_back(std::move(viewport));
313 mConfig.setDisplayViewports(mViewports);
314 }
315
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700316 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000317 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700318 std::optional<uint8_t> physicalPort, ViewportType type) {
319 const bool isRotated =
320 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
321 DisplayViewport v;
322 v.displayId = displayId;
323 v.orientation = orientation;
324 v.logicalLeft = 0;
325 v.logicalTop = 0;
326 v.logicalRight = isRotated ? height : width;
327 v.logicalBottom = isRotated ? width : height;
328 v.physicalLeft = 0;
329 v.physicalTop = 0;
330 v.physicalRight = isRotated ? height : width;
331 v.physicalBottom = isRotated ? width : height;
332 v.deviceWidth = isRotated ? height : width;
333 v.deviceHeight = isRotated ? width : height;
334 v.isActive = isActive;
335 v.uniqueId = uniqueId;
336 v.physicalPort = physicalPort;
337 v.type = type;
338
339 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800340 }
341
Arthur Hung6cd19a42019-08-30 19:04:12 +0800342 bool updateViewport(const DisplayViewport& viewport) {
343 size_t count = mViewports.size();
344 for (size_t i = 0; i < count; i++) {
345 const DisplayViewport& currentViewport = mViewports[i];
346 if (currentViewport.displayId == viewport.displayId) {
347 mViewports[i] = viewport;
348 mConfig.setDisplayViewports(mViewports);
349 return true;
350 }
351 }
352 // no viewport found.
353 return false;
354 }
355
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100356 void addExcludedDeviceName(const std::string& deviceName) {
357 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800358 }
359
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700360 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
361 mConfig.portAssociations.insert({inputPort, displayPort});
362 }
363
Christine Franks1ba71cc2021-04-07 14:37:42 -0700364 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
365 const std::string& displayUniqueId) {
366 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
367 }
368
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000369 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700370
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000371 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700372
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000373 void setPointerController(std::shared_ptr<FakePointerController> controller) {
374 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800375 }
376
377 const InputReaderConfiguration* getReaderConfiguration() const {
378 return &mConfig;
379 }
380
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800381 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382 return mInputDevices;
383 }
384
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100385 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700386 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700387 return transform;
388 }
389
390 void setTouchAffineTransformation(const TouchAffineTransformation t) {
391 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800392 }
393
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000394 PointerCaptureRequest setPointerCapture(bool enabled) {
395 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
396 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800397 }
398
Arthur Hung7c645402019-01-25 17:45:42 +0800399 void setShowTouches(bool enabled) {
400 mConfig.showTouches = enabled;
401 }
402
Garfield Tan888a6a42020-01-09 11:39:16 -0800403 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
404 mConfig.defaultPointerDisplayId = pointerDisplayId;
405 }
406
HQ Liue6983c72022-04-19 22:14:56 +0000407 void setPointerGestureEnabled(bool enabled) { mConfig.pointerGesturesEnabled = enabled; }
408
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800409 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
410
HQ Liue6983c72022-04-19 22:14:56 +0000411 float getPointerGestureZoomSpeedRatio() { return mConfig.pointerGestureZoomSpeedRatio; }
412
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000413 void setVelocityControlParams(const VelocityControlParameters& params) {
414 mConfig.pointerVelocityControlParameters = params;
415 mConfig.wheelVelocityControlParameters = params;
416 }
417
Michael Wrightd02c5b62014-02-10 15:10:22 -0800418private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000419 uint32_t mNextPointerCaptureSequenceNumber = 0;
420
Chris Yea52ade12020-08-27 16:49:20 -0700421 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800422 *outConfig = mConfig;
423 }
424
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000425 std::shared_ptr<PointerControllerInterface> obtainPointerController(
426 int32_t /*deviceId*/) override {
427 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800428 }
429
Chris Yea52ade12020-08-27 16:49:20 -0700430 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700431 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800432 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700433 mInputDevicesChanged = true;
434 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800435 }
436
Chris Yea52ade12020-08-27 16:49:20 -0700437 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
438 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700439 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800440 }
441
Chris Yea52ade12020-08-27 16:49:20 -0700442 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800443
444 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
445 std::unique_lock<std::mutex> lock(mLock);
446 base::ScopedLockAssertion assumeLocked(mLock);
447
448 const bool devicesChanged =
449 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
450 return mInputDevicesChanged;
451 });
452 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
453 mInputDevicesChanged = false;
454 }
Prabir Pradhanda20b172022-09-26 17:01:18 +0000455
456 void notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) override {
457 std::scoped_lock<std::mutex> lock(mLock);
458 mStylusGestureNotified = deviceId;
459 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800460};
461
Michael Wrightd02c5b62014-02-10 15:10:22 -0800462// --- FakeEventHub ---
463
464class FakeEventHub : public EventHubInterface {
465 struct KeyInfo {
466 int32_t keyCode;
467 uint32_t flags;
468 };
469
Chris Yef59a2f42020-10-16 12:55:26 -0700470 struct SensorInfo {
471 InputDeviceSensorType sensorType;
472 int32_t sensorDataIndex;
473 };
474
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475 struct Device {
476 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700477 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800478 PropertyMap configuration;
479 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
480 KeyedVector<int, bool> relativeAxes;
481 KeyedVector<int32_t, int32_t> keyCodeStates;
482 KeyedVector<int32_t, int32_t> scanCodeStates;
483 KeyedVector<int32_t, int32_t> switchStates;
484 KeyedVector<int32_t, int32_t> absoluteAxisValue;
485 KeyedVector<int32_t, KeyInfo> keysByScanCode;
486 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
487 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100488 // fake mapping which would normally come from keyCharacterMap
489 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700490 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
491 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800492 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700493 bool enabled;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000494 InputDeviceCountryCode countryCode;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700495
496 status_t enable() {
497 enabled = true;
498 return OK;
499 }
500
501 status_t disable() {
502 enabled = false;
503 return OK;
504 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800505
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700506 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800507 };
508
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700509 std::mutex mLock;
510 std::condition_variable mEventsCondition;
511
Michael Wrightd02c5b62014-02-10 15:10:22 -0800512 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100513 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000514 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600515 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000516 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800517 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
518 // Simulates a device light brightness, from light id to light brightness.
519 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
520 // Simulates a device light intensities, from light id to light intensities map.
521 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
522 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800523
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700524public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800525 virtual ~FakeEventHub() {
526 for (size_t i = 0; i < mDevices.size(); i++) {
527 delete mDevices.valueAt(i);
528 }
529 }
530
Michael Wrightd02c5b62014-02-10 15:10:22 -0800531 FakeEventHub() { }
532
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700533 void addDevice(int32_t deviceId, const std::string& name,
534 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800535 Device* device = new Device(classes);
536 device->identifier.name = name;
537 mDevices.add(deviceId, device);
538
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000539 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800540 }
541
542 void removeDevice(int32_t deviceId) {
543 delete mDevices.valueFor(deviceId);
544 mDevices.removeItem(deviceId);
545
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000546 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800547 }
548
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000549 bool isDeviceEnabled(int32_t deviceId) const override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700550 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700551 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700552 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
553 return false;
554 }
555 return device->enabled;
556 }
557
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000558 status_t enableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700559 status_t result;
560 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700561 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700562 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
563 return BAD_VALUE;
564 }
565 if (device->enabled) {
566 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
567 return OK;
568 }
569 result = device->enable();
570 return result;
571 }
572
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000573 status_t disableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700574 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700575 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700576 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
577 return BAD_VALUE;
578 }
579 if (!device->enabled) {
580 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
581 return OK;
582 }
583 return device->disable();
584 }
585
Michael Wrightd02c5b62014-02-10 15:10:22 -0800586 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000587 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588 }
589
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700590 void addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800591 Device* device = getDevice(deviceId);
592 device->configuration.addProperty(key, value);
593 }
594
595 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
596 Device* device = getDevice(deviceId);
597 device->configuration.addAll(configuration);
598 }
599
600 void addAbsoluteAxis(int32_t deviceId, int axis,
601 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
602 Device* device = getDevice(deviceId);
603
604 RawAbsoluteAxisInfo info;
605 info.valid = true;
606 info.minValue = minValue;
607 info.maxValue = maxValue;
608 info.flat = flat;
609 info.fuzz = fuzz;
610 info.resolution = resolution;
611 device->absoluteAxes.add(axis, info);
612 }
613
614 void addRelativeAxis(int32_t deviceId, int32_t axis) {
615 Device* device = getDevice(deviceId);
616 device->relativeAxes.add(axis, true);
617 }
618
619 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
620 Device* device = getDevice(deviceId);
621 device->keyCodeStates.replaceValueFor(keyCode, state);
622 }
623
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000624 void setCountryCode(int32_t deviceId, InputDeviceCountryCode countryCode) {
625 Device* device = getDevice(deviceId);
626 device->countryCode = countryCode;
627 }
628
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
630 Device* device = getDevice(deviceId);
631 device->scanCodeStates.replaceValueFor(scanCode, state);
632 }
633
634 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
635 Device* device = getDevice(deviceId);
636 device->switchStates.replaceValueFor(switchCode, state);
637 }
638
639 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
640 Device* device = getDevice(deviceId);
641 device->absoluteAxisValue.replaceValueFor(axis, value);
642 }
643
644 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
645 int32_t keyCode, uint32_t flags) {
646 Device* device = getDevice(deviceId);
647 KeyInfo info;
648 info.keyCode = keyCode;
649 info.flags = flags;
650 if (scanCode) {
651 device->keysByScanCode.add(scanCode, info);
652 }
653 if (usageCode) {
654 device->keysByUsageCode.add(usageCode, info);
655 }
656 }
657
Philip Junker4af3b3d2021-12-14 10:36:55 +0100658 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
659 Device* device = getDevice(deviceId);
660 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
661 }
662
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 void addLed(int32_t deviceId, int32_t led, bool initialState) {
664 Device* device = getDevice(deviceId);
665 device->leds.add(led, initialState);
666 }
667
Chris Yef59a2f42020-10-16 12:55:26 -0700668 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
669 int32_t sensorDataIndex) {
670 Device* device = getDevice(deviceId);
671 SensorInfo info;
672 info.sensorType = sensorType;
673 info.sensorDataIndex = sensorDataIndex;
674 device->sensorsByAbsCode.emplace(absCode, info);
675 }
676
677 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
678 Device* device = getDevice(deviceId);
679 typename BitArray<MSC_MAX>::Buffer buffer;
680 buffer[mscEvent / 32] = 1 << mscEvent % 32;
681 device->mscBitmask.loadFromBuffer(buffer);
682 }
683
Chris Ye3fdbfef2021-01-06 18:45:18 -0800684 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
685 mRawLightInfos.emplace(rawId, std::move(info));
686 }
687
688 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
689 mLightBrightness.emplace(rawId, brightness);
690 }
691
692 void fakeLightIntensities(int32_t rawId,
693 const std::unordered_map<LightColor, int32_t> intensities) {
694 mLightIntensities.emplace(rawId, std::move(intensities));
695 }
696
Michael Wrightd02c5b62014-02-10 15:10:22 -0800697 bool getLedState(int32_t deviceId, int32_t led) {
698 Device* device = getDevice(deviceId);
699 return device->leds.valueFor(led);
700 }
701
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100702 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703 return mExcludedDevices;
704 }
705
706 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
707 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800708 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800709 }
710
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000711 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
712 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700713 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800714 RawEvent event;
715 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000716 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800717 event.deviceId = deviceId;
718 event.type = type;
719 event.code = code;
720 event.value = value;
721 mEvents.push_back(event);
722
723 if (type == EV_ABS) {
724 setAbsoluteAxisValue(deviceId, code, value);
725 }
726 }
727
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600728 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
729 std::vector<TouchVideoFrame>> videoFrames) {
730 mVideoFrames = std::move(videoFrames);
731 }
732
Michael Wrightd02c5b62014-02-10 15:10:22 -0800733 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700734 std::unique_lock<std::mutex> lock(mLock);
735 base::ScopedLockAssertion assumeLocked(mLock);
736 const bool queueIsEmpty =
737 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
738 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
739 if (!queueIsEmpty) {
740 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
741 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 }
743
744private:
745 Device* getDevice(int32_t deviceId) const {
746 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100747 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800748 }
749
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700750 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800751 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700752 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800753 }
754
Chris Yea52ade12020-08-27 16:49:20 -0700755 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800756 Device* device = getDevice(deviceId);
757 return device ? device->identifier : InputDeviceIdentifier();
758 }
759
Chris Yea52ade12020-08-27 16:49:20 -0700760 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800761
Chris Yea52ade12020-08-27 16:49:20 -0700762 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800763 Device* device = getDevice(deviceId);
764 if (device) {
765 *outConfiguration = device->configuration;
766 }
767 }
768
Chris Yea52ade12020-08-27 16:49:20 -0700769 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
770 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800771 Device* device = getDevice(deviceId);
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700772 if (device) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800773 ssize_t index = device->absoluteAxes.indexOfKey(axis);
774 if (index >= 0) {
775 *outAxisInfo = device->absoluteAxes.valueAt(index);
776 return OK;
777 }
778 }
779 outAxisInfo->clear();
780 return -1;
781 }
782
Chris Yea52ade12020-08-27 16:49:20 -0700783 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800784 Device* device = getDevice(deviceId);
785 if (device) {
786 return device->relativeAxes.indexOfKey(axis) >= 0;
787 }
788 return false;
789 }
790
Chris Yea52ade12020-08-27 16:49:20 -0700791 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800792
Chris Yef59a2f42020-10-16 12:55:26 -0700793 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
794 Device* device = getDevice(deviceId);
795 if (device) {
796 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
797 }
798 return false;
799 }
800
Chris Yea52ade12020-08-27 16:49:20 -0700801 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
802 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800803 Device* device = getDevice(deviceId);
804 if (device) {
805 const KeyInfo* key = getKey(device, scanCode, usageCode);
806 if (key) {
807 if (outKeycode) {
808 *outKeycode = key->keyCode;
809 }
810 if (outFlags) {
811 *outFlags = key->flags;
812 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700813 if (outMetaState) {
814 *outMetaState = metaState;
815 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800816 return OK;
817 }
818 }
819 return NAME_NOT_FOUND;
820 }
821
822 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
823 if (usageCode) {
824 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
825 if (index >= 0) {
826 return &device->keysByUsageCode.valueAt(index);
827 }
828 }
829 if (scanCode) {
830 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
831 if (index >= 0) {
832 return &device->keysByScanCode.valueAt(index);
833 }
834 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700835 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836 }
837
Chris Yea52ade12020-08-27 16:49:20 -0700838 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800839
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000840 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
841 int32_t deviceId, int32_t absCode) const override {
Chris Yef59a2f42020-10-16 12:55:26 -0700842 Device* device = getDevice(deviceId);
843 if (!device) {
844 return Errorf("Sensor device not found.");
845 }
846 auto it = device->sensorsByAbsCode.find(absCode);
847 if (it == device->sensorsByAbsCode.end()) {
848 return Errorf("Sensor map not found.");
849 }
850 const SensorInfo& info = it->second;
851 return std::make_pair(info.sensorType, info.sensorDataIndex);
852 }
853
Chris Yea52ade12020-08-27 16:49:20 -0700854 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800855 mExcludedDevices = devices;
856 }
857
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700858 std::vector<RawEvent> getEvents(int) override {
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000859 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700861 std::vector<RawEvent> buffer;
862 std::swap(buffer, mEvents);
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000863
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700864 mEventsCondition.notify_all();
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700865 return buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800866 }
867
Chris Yea52ade12020-08-27 16:49:20 -0700868 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600869 auto it = mVideoFrames.find(deviceId);
870 if (it != mVideoFrames.end()) {
871 std::vector<TouchVideoFrame> frames = std::move(it->second);
872 mVideoFrames.erase(deviceId);
873 return frames;
874 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800875 return {};
876 }
877
Chris Yea52ade12020-08-27 16:49:20 -0700878 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800879 Device* device = getDevice(deviceId);
880 if (device) {
881 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
882 if (index >= 0) {
883 return device->scanCodeStates.valueAt(index);
884 }
885 }
886 return AKEY_STATE_UNKNOWN;
887 }
888
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000889 InputDeviceCountryCode getCountryCode(int32_t deviceId) const override {
890 Device* device = getDevice(deviceId);
891 if (device) {
892 return device->countryCode;
893 }
894 return InputDeviceCountryCode::INVALID;
895 }
896
Chris Yea52ade12020-08-27 16:49:20 -0700897 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898 Device* device = getDevice(deviceId);
899 if (device) {
900 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
901 if (index >= 0) {
902 return device->keyCodeStates.valueAt(index);
903 }
904 }
905 return AKEY_STATE_UNKNOWN;
906 }
907
Chris Yea52ade12020-08-27 16:49:20 -0700908 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909 Device* device = getDevice(deviceId);
910 if (device) {
911 ssize_t index = device->switchStates.indexOfKey(sw);
912 if (index >= 0) {
913 return device->switchStates.valueAt(index);
914 }
915 }
916 return AKEY_STATE_UNKNOWN;
917 }
918
Chris Yea52ade12020-08-27 16:49:20 -0700919 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
920 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921 Device* device = getDevice(deviceId);
922 if (device) {
923 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
924 if (index >= 0) {
925 *outValue = device->absoluteAxisValue.valueAt(index);
926 return OK;
927 }
928 }
929 *outValue = 0;
930 return -1;
931 }
932
Philip Junker4af3b3d2021-12-14 10:36:55 +0100933 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
934 Device* device = getDevice(deviceId);
935 if (!device) {
936 return AKEYCODE_UNKNOWN;
937 }
938 auto it = device->keyCodeMapping.find(locationKeyCode);
939 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
940 }
941
Chris Yea52ade12020-08-27 16:49:20 -0700942 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700943 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700944 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945 bool result = false;
946 Device* device = getDevice(deviceId);
947 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700948 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700949 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800950 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
951 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
952 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953 }
954 }
955 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
956 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
957 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958 }
959 }
960 }
961 }
962 return result;
963 }
964
Chris Yea52ade12020-08-27 16:49:20 -0700965 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800966 Device* device = getDevice(deviceId);
967 if (device) {
968 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
969 return index >= 0;
970 }
971 return false;
972 }
973
Arthur Hungcb40a002021-08-03 14:31:01 +0000974 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
975 Device* device = getDevice(deviceId);
976 if (!device) {
977 return false;
978 }
979 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
980 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
981 return true;
982 }
983 }
984 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
985 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
986 return true;
987 }
988 }
989 return false;
990 }
991
Chris Yea52ade12020-08-27 16:49:20 -0700992 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800993 Device* device = getDevice(deviceId);
994 return device && device->leds.indexOfKey(led) >= 0;
995 }
996
Chris Yea52ade12020-08-27 16:49:20 -0700997 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800998 Device* device = getDevice(deviceId);
999 if (device) {
1000 ssize_t index = device->leds.indexOfKey(led);
1001 if (index >= 0) {
1002 device->leds.replaceValueAt(led, on);
1003 } else {
1004 ADD_FAILURE()
1005 << "Attempted to set the state of an LED that the EventHub declared "
1006 "was not present. led=" << led;
1007 }
1008 }
1009 }
1010
Chris Yea52ade12020-08-27 16:49:20 -07001011 void getVirtualKeyDefinitions(
1012 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001013 outVirtualKeys.clear();
1014
1015 Device* device = getDevice(deviceId);
1016 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001017 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001018 }
1019 }
1020
Chris Yea52ade12020-08-27 16:49:20 -07001021 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -07001022 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001023 }
1024
Chris Yea52ade12020-08-27 16:49:20 -07001025 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001026 return false;
1027 }
1028
Chris Yea52ade12020-08-27 16:49:20 -07001029 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001030
Chris Yea52ade12020-08-27 16:49:20 -07001031 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001032
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001033 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return mVibrators; };
Chris Ye87143712020-11-10 05:05:58 +00001034
Chris Yee2b1e5c2021-03-10 22:45:12 -08001035 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
1036 return BATTERY_CAPACITY;
1037 }
Kim Low03ea0352020-11-06 12:45:07 -08001038
Chris Yee2b1e5c2021-03-10 22:45:12 -08001039 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
1040 return BATTERY_STATUS;
1041 }
1042
Andy Chenf9f1a022022-08-29 20:07:10 -04001043 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override {
1044 return {DEFAULT_BATTERY};
1045 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001046
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001047 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
1048 int32_t batteryId) const override {
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001049 if (batteryId != DEFAULT_BATTERY) return {};
1050 static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY,
1051 .name = "default battery",
1052 .flags = InputBatteryClass::CAPACITY,
1053 .path = BATTERY_DEVPATH};
1054 return BATTERY_INFO;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001055 }
Kim Low03ea0352020-11-06 12:45:07 -08001056
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001057 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001058 std::vector<int32_t> ids;
1059 for (const auto& [rawId, info] : mRawLightInfos) {
1060 ids.push_back(rawId);
1061 }
1062 return ids;
1063 }
1064
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001065 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001066 auto it = mRawLightInfos.find(lightId);
1067 if (it == mRawLightInfos.end()) {
1068 return std::nullopt;
1069 }
1070 return it->second;
1071 }
1072
1073 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1074 mLightBrightness.emplace(lightId, brightness);
1075 }
1076
1077 void setLightIntensities(int32_t deviceId, int32_t lightId,
1078 std::unordered_map<LightColor, int32_t> intensities) override {
1079 mLightIntensities.emplace(lightId, intensities);
1080 };
1081
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001082 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001083 auto lightIt = mLightBrightness.find(lightId);
1084 if (lightIt == mLightBrightness.end()) {
1085 return std::nullopt;
1086 }
1087 return lightIt->second;
1088 }
1089
1090 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001091 int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001092 auto lightIt = mLightIntensities.find(lightId);
1093 if (lightIt == mLightIntensities.end()) {
1094 return std::nullopt;
1095 }
1096 return lightIt->second;
1097 };
1098
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001099 void dump(std::string&) const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001100
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001101 void monitor() const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102
Chris Yea52ade12020-08-27 16:49:20 -07001103 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001104
Chris Yea52ade12020-08-27 16:49:20 -07001105 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001106};
1107
Michael Wrightd02c5b62014-02-10 15:10:22 -08001108// --- FakeInputMapper ---
1109
1110class FakeInputMapper : public InputMapper {
1111 uint32_t mSources;
1112 int32_t mKeyboardType;
1113 int32_t mMetaState;
1114 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1115 KeyedVector<int32_t, int32_t> mScanCodeStates;
1116 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001117 // fake mapping which would normally come from keyCharacterMap
1118 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001119 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001120
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001121 std::mutex mLock;
1122 std::condition_variable mStateChangedCondition;
1123 bool mConfigureWasCalled GUARDED_BY(mLock);
1124 bool mResetWasCalled GUARDED_BY(mLock);
1125 bool mProcessWasCalled GUARDED_BY(mLock);
1126 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001127
Arthur Hungc23540e2018-11-29 20:42:11 +08001128 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001129public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001130 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1131 : InputMapper(deviceContext),
1132 mSources(sources),
1133 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001134 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001135 mConfigureWasCalled(false),
1136 mResetWasCalled(false),
1137 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001138
Chris Yea52ade12020-08-27 16:49:20 -07001139 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001140
1141 void setKeyboardType(int32_t keyboardType) {
1142 mKeyboardType = keyboardType;
1143 }
1144
1145 void setMetaState(int32_t metaState) {
1146 mMetaState = metaState;
1147 }
1148
1149 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001150 std::unique_lock<std::mutex> lock(mLock);
1151 base::ScopedLockAssertion assumeLocked(mLock);
1152 const bool configureCalled =
1153 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1154 return mConfigureWasCalled;
1155 });
1156 if (!configureCalled) {
1157 FAIL() << "Expected configure() to have been called.";
1158 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001159 mConfigureWasCalled = false;
1160 }
1161
1162 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001163 std::unique_lock<std::mutex> lock(mLock);
1164 base::ScopedLockAssertion assumeLocked(mLock);
1165 const bool resetCalled =
1166 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1167 return mResetWasCalled;
1168 });
1169 if (!resetCalled) {
1170 FAIL() << "Expected reset() to have been called.";
1171 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001172 mResetWasCalled = false;
1173 }
1174
Yi Kong9b14ac62018-07-17 13:48:38 -07001175 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001176 std::unique_lock<std::mutex> lock(mLock);
1177 base::ScopedLockAssertion assumeLocked(mLock);
1178 const bool processCalled =
1179 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1180 return mProcessWasCalled;
1181 });
1182 if (!processCalled) {
1183 FAIL() << "Expected process() to have been called.";
1184 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001185 if (outLastEvent) {
1186 *outLastEvent = mLastEvent;
1187 }
1188 mProcessWasCalled = false;
1189 }
1190
1191 void setKeyCodeState(int32_t keyCode, int32_t state) {
1192 mKeyCodeStates.replaceValueFor(keyCode, state);
1193 }
1194
1195 void setScanCodeState(int32_t scanCode, int32_t state) {
1196 mScanCodeStates.replaceValueFor(scanCode, state);
1197 }
1198
1199 void setSwitchState(int32_t switchCode, int32_t state) {
1200 mSwitchStates.replaceValueFor(switchCode, state);
1201 }
1202
1203 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001204 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001205 }
1206
Philip Junker4af3b3d2021-12-14 10:36:55 +01001207 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1208 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1209 }
1210
Michael Wrightd02c5b62014-02-10 15:10:22 -08001211private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001212 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001213
Chris Yea52ade12020-08-27 16:49:20 -07001214 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001215 InputMapper::populateDeviceInfo(deviceInfo);
1216
1217 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1218 deviceInfo->setKeyboardType(mKeyboardType);
1219 }
1220 }
1221
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001222 std::list<NotifyArgs> configure(nsecs_t, const InputReaderConfiguration* config,
1223 uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001224 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001225 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001226
1227 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001228 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001229 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1230 mViewport = config->getDisplayViewportByPort(*displayPort);
1231 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001232
1233 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001234 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001235 }
1236
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001237 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001238 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001239 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001240 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001241 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001242 }
1243
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001244 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001245 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001246 mLastEvent = *rawEvent;
1247 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001248 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001249 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001250 }
1251
Chris Yea52ade12020-08-27 16:49:20 -07001252 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001253 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1254 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1255 }
1256
Philip Junker4af3b3d2021-12-14 10:36:55 +01001257 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1258 auto it = mKeyCodeMapping.find(locationKeyCode);
1259 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1260 }
1261
Chris Yea52ade12020-08-27 16:49:20 -07001262 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001263 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1264 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1265 }
1266
Chris Yea52ade12020-08-27 16:49:20 -07001267 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001268 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1269 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1270 }
1271
Chris Yea52ade12020-08-27 16:49:20 -07001272 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001273 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001274 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001275 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001276 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1277 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1278 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001279 }
1280 }
1281 }
Chris Yea52ade12020-08-27 16:49:20 -07001282 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001283 return result;
1284 }
1285
1286 virtual int32_t getMetaState() {
1287 return mMetaState;
1288 }
1289
1290 virtual void fadePointer() {
1291 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001292
1293 virtual std::optional<int32_t> getAssociatedDisplay() {
1294 if (mViewport) {
1295 return std::make_optional(mViewport->displayId);
1296 }
1297 return std::nullopt;
1298 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001299};
1300
1301
1302// --- InstrumentedInputReader ---
1303
1304class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001305 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001306
1307public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001308 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1309 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001310 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001311 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001312
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001313 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001314
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001315 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001316
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001317 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001318 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001319 InputDeviceIdentifier identifier;
1320 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001321 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001322 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001323 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001324 }
1325
Prabir Pradhan28efc192019-11-05 01:10:04 +00001326 // Make the protected loopOnce method accessible to tests.
1327 using InputReader::loopOnce;
1328
Michael Wrightd02c5b62014-02-10 15:10:22 -08001329protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001330 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1331 const InputDeviceIdentifier& identifier)
1332 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001333 if (!mNextDevices.empty()) {
1334 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1335 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001336 return device;
1337 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001338 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001339 }
1340
arthurhungdcef2dc2020-08-11 14:47:50 +08001341 // --- FakeInputReaderContext ---
1342 class FakeInputReaderContext : public ContextImpl {
1343 int32_t mGlobalMetaState;
1344 bool mUpdateGlobalMetaStateWasCalled;
1345 int32_t mGeneration;
1346
1347 public:
1348 FakeInputReaderContext(InputReader* reader)
1349 : ContextImpl(reader),
1350 mGlobalMetaState(0),
1351 mUpdateGlobalMetaStateWasCalled(false),
1352 mGeneration(1) {}
1353
1354 virtual ~FakeInputReaderContext() {}
1355
1356 void assertUpdateGlobalMetaStateWasCalled() {
1357 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1358 << "Expected updateGlobalMetaState() to have been called.";
1359 mUpdateGlobalMetaStateWasCalled = false;
1360 }
1361
1362 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1363
1364 uint32_t getGeneration() { return mGeneration; }
1365
1366 void updateGlobalMetaState() override {
1367 mUpdateGlobalMetaStateWasCalled = true;
1368 ContextImpl::updateGlobalMetaState();
1369 }
1370
1371 int32_t getGlobalMetaState() override {
1372 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1373 }
1374
1375 int32_t bumpGeneration() override {
1376 mGeneration = ContextImpl::bumpGeneration();
1377 return mGeneration;
1378 }
1379 } mFakeContext;
1380
Michael Wrightd02c5b62014-02-10 15:10:22 -08001381 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001382
1383public:
1384 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001385};
1386
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001387// --- InputReaderPolicyTest ---
1388class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001389protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001390 sp<FakeInputReaderPolicy> mFakePolicy;
1391
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001392 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001393 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001394};
1395
1396/**
1397 * Check that empty set of viewports is an acceptable configuration.
1398 * Also try to get internal viewport two different ways - by type and by uniqueId.
1399 *
1400 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1401 * Such configuration is not currently allowed.
1402 */
1403TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001404 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001405
1406 // We didn't add any viewports yet, so there shouldn't be any.
1407 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001408 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001409 ASSERT_FALSE(internalViewport);
1410
1411 // Add an internal viewport, then clear it
1412 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001413 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001414 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001415
1416 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001417 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001418 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001419 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001420
1421 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001422 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001423 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001424 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001425
1426 mFakePolicy->clearViewports();
1427 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001428 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001429 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001430 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001431 ASSERT_FALSE(internalViewport);
1432}
1433
1434TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1435 const std::string internalUniqueId = "local:0";
1436 const std::string externalUniqueId = "local:1";
1437 const std::string virtualUniqueId1 = "virtual:2";
1438 const std::string virtualUniqueId2 = "virtual:3";
1439 constexpr int32_t virtualDisplayId1 = 2;
1440 constexpr int32_t virtualDisplayId2 = 3;
1441
1442 // Add an internal viewport
1443 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001444 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1445 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001446 // Add an external viewport
1447 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001448 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1449 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001450 // Add an virtual viewport
1451 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001452 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1453 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001454 // Add another virtual viewport
1455 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001456 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1457 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001458
1459 // Check matching by type for internal
1460 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001461 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001462 ASSERT_TRUE(internalViewport);
1463 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1464
1465 // Check matching by type for external
1466 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001467 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001468 ASSERT_TRUE(externalViewport);
1469 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1470
1471 // Check matching by uniqueId for virtual viewport #1
1472 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001473 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001474 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001475 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001476 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1477 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1478
1479 // Check matching by uniqueId for virtual viewport #2
1480 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001481 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001482 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001483 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001484 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1485 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1486}
1487
1488
1489/**
1490 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1491 * that lookup works by checking display id.
1492 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1493 */
1494TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1495 const std::string uniqueId1 = "uniqueId1";
1496 const std::string uniqueId2 = "uniqueId2";
1497 constexpr int32_t displayId1 = 2;
1498 constexpr int32_t displayId2 = 3;
1499
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001500 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1501 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001502 for (const ViewportType& type : types) {
1503 mFakePolicy->clearViewports();
1504 // Add a viewport
1505 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001506 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1507 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001508 // Add another viewport
1509 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001510 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1511 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001512
1513 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001514 std::optional<DisplayViewport> viewport1 =
1515 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001516 ASSERT_TRUE(viewport1);
1517 ASSERT_EQ(displayId1, viewport1->displayId);
1518 ASSERT_EQ(type, viewport1->type);
1519
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001520 std::optional<DisplayViewport> viewport2 =
1521 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001522 ASSERT_TRUE(viewport2);
1523 ASSERT_EQ(displayId2, viewport2->displayId);
1524 ASSERT_EQ(type, viewport2->type);
1525
1526 // When there are multiple viewports of the same kind, and uniqueId is not specified
1527 // in the call to getDisplayViewport, then that situation is not supported.
1528 // The viewports can be stored in any order, so we cannot rely on the order, since that
1529 // is just implementation detail.
1530 // However, we can check that it still returns *a* viewport, we just cannot assert
1531 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001532 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001533 ASSERT_TRUE(someViewport);
1534 }
1535}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001536
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001537/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001538 * When we have multiple internal displays make sure we always return the default display when
1539 * querying by type.
1540 */
1541TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1542 const std::string uniqueId1 = "uniqueId1";
1543 const std::string uniqueId2 = "uniqueId2";
1544 constexpr int32_t nonDefaultDisplayId = 2;
1545 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1546 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1547
1548 // Add the default display first and ensure it gets returned.
1549 mFakePolicy->clearViewports();
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 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001554 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001555 ViewportType::INTERNAL);
1556
1557 std::optional<DisplayViewport> viewport =
1558 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1559 ASSERT_TRUE(viewport);
1560 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1561 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1562
1563 // Add the default display second to make sure order doesn't matter.
1564 mFakePolicy->clearViewports();
1565 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001566 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001567 ViewportType::INTERNAL);
1568 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001569 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001570 ViewportType::INTERNAL);
1571
1572 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1573 ASSERT_TRUE(viewport);
1574 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1575 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1576}
1577
1578/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001579 * Check getDisplayViewportByPort
1580 */
1581TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001582 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001583 const std::string uniqueId1 = "uniqueId1";
1584 const std::string uniqueId2 = "uniqueId2";
1585 constexpr int32_t displayId1 = 1;
1586 constexpr int32_t displayId2 = 2;
1587 const uint8_t hdmi1 = 0;
1588 const uint8_t hdmi2 = 1;
1589 const uint8_t hdmi3 = 2;
1590
1591 mFakePolicy->clearViewports();
1592 // Add a viewport that's associated with some display port that's not of interest.
1593 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001594 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1595 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001596 // Add another viewport, connected to HDMI1 port
1597 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001598 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1599 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001600
1601 // Check that correct display viewport was returned by comparing the display ports.
1602 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1603 ASSERT_TRUE(hdmi1Viewport);
1604 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1605 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1606
1607 // Check that we can still get the same viewport using the uniqueId
1608 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1609 ASSERT_TRUE(hdmi1Viewport);
1610 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1611 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1612 ASSERT_EQ(type, hdmi1Viewport->type);
1613
1614 // Check that we cannot find a port with "HDMI2", because we never added one
1615 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1616 ASSERT_FALSE(hdmi2Viewport);
1617}
1618
Michael Wrightd02c5b62014-02-10 15:10:22 -08001619// --- InputReaderTest ---
1620
1621class InputReaderTest : public testing::Test {
1622protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001623 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001624 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001625 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001626 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001627
Chris Yea52ade12020-08-27 16:49:20 -07001628 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001629 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001630 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001631 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001632
Prabir Pradhan28efc192019-11-05 01:10:04 +00001633 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001634 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001635 }
1636
Chris Yea52ade12020-08-27 16:49:20 -07001637 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001638 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001639 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001640 }
1641
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001642 void addDevice(int32_t eventHubId, const std::string& name,
1643 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001644 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001645
1646 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001647 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001648 }
1649 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001650 mReader->loopOnce();
1651 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001652 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1653 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001654 }
1655
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001656 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001657 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001658 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001659 }
1660
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001661 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001662 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001663 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001664 }
1665
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001666 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001667 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001668 ftl::Flags<InputDeviceClass> classes,
1669 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001670 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001671 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1672 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001673 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001674 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001675 return mapper;
1676 }
1677};
1678
Chris Ye98d3f532020-10-01 21:48:59 -07001679TEST_F(InputReaderTest, PolicyGetInputDevices) {
1680 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001681 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001682 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001683
1684 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001685 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001686 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001687 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001688 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001689 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1690 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001691 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001692}
1693
Chris Yee7310032020-09-22 15:36:28 -07001694TEST_F(InputReaderTest, GetMergedInputDevices) {
1695 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1696 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1697 // Add two subdevices to device
1698 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1699 // Must add at least one mapper or the device will be ignored!
1700 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1701 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1702
1703 // Push same device instance for next device to be added, so they'll have same identifier.
1704 mReader->pushNextDevice(device);
1705 mReader->pushNextDevice(device);
1706 ASSERT_NO_FATAL_FAILURE(
1707 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1708 ASSERT_NO_FATAL_FAILURE(
1709 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1710
1711 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001712 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001713}
1714
Chris Yee14523a2020-12-19 13:46:00 -08001715TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1716 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1717 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1718 // Add two subdevices to device
1719 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1720 // Must add at least one mapper or the device will be ignored!
1721 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1722 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1723
1724 // Push same device instance for next device to be added, so they'll have same identifier.
1725 mReader->pushNextDevice(device);
1726 mReader->pushNextDevice(device);
1727 // Sensor device is initially disabled
1728 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1729 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1730 nullptr));
1731 // Device is disabled because the only sub device is a sensor device and disabled initially.
1732 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1733 ASSERT_FALSE(device->isEnabled());
1734 ASSERT_NO_FATAL_FAILURE(
1735 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1736 // The merged device is enabled if any sub device is enabled
1737 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1738 ASSERT_TRUE(device->isEnabled());
1739}
1740
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001741TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001742 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001743 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001744 constexpr int32_t eventHubId = 1;
1745 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001746 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001747 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001748 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001749 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001750
Yi Kong9b14ac62018-07-17 13:48:38 -07001751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001752
1753 NotifyDeviceResetArgs resetArgs;
1754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001755 ASSERT_EQ(deviceId, resetArgs.deviceId);
1756
1757 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001758 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001759 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001760
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001762 ASSERT_EQ(deviceId, resetArgs.deviceId);
1763 ASSERT_EQ(device->isEnabled(), false);
1764
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001765 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001766 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001769 ASSERT_EQ(device->isEnabled(), false);
1770
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001771 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001772 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001774 ASSERT_EQ(deviceId, resetArgs.deviceId);
1775 ASSERT_EQ(device->isEnabled(), true);
1776}
1777
Michael Wrightd02c5b62014-02-10 15:10:22 -08001778TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001779 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001780 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001781 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001782 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001783 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001784 AINPUT_SOURCE_KEYBOARD, nullptr);
1785 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001786
1787 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1788 AINPUT_SOURCE_ANY, AKEYCODE_A))
1789 << "Should return unknown when the device id is >= 0 but unknown.";
1790
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001791 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1792 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1793 << "Should return unknown when the device id is valid but the sources are not "
1794 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001795
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001796 ASSERT_EQ(AKEY_STATE_DOWN,
1797 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1798 AKEYCODE_A))
1799 << "Should return value provided by mapper when device id is valid and the device "
1800 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001801
1802 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1803 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1804 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1805
1806 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1807 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1808 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1809}
1810
Philip Junker4af3b3d2021-12-14 10:36:55 +01001811TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1812 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1813 constexpr int32_t eventHubId = 1;
1814 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1815 InputDeviceClass::KEYBOARD,
1816 AINPUT_SOURCE_KEYBOARD, nullptr);
1817 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1818
1819 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1820 << "Should return unknown when the device with the specified id is not found.";
1821
1822 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1823 << "Should return correct mapping when device id is valid and mapping exists.";
1824
1825 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1826 << "Should return the location key code when device id is valid and there's no "
1827 "mapping.";
1828}
1829
1830TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1831 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1832 constexpr int32_t eventHubId = 1;
1833 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1834 InputDeviceClass::JOYSTICK,
1835 AINPUT_SOURCE_GAMEPAD, nullptr);
1836 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1837
1838 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1839 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1840}
1841
Michael Wrightd02c5b62014-02-10 15:10:22 -08001842TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001843 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001844 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001845 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001846 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001847 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001848 AINPUT_SOURCE_KEYBOARD, nullptr);
1849 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001850
1851 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1852 AINPUT_SOURCE_ANY, KEY_A))
1853 << "Should return unknown when the device id is >= 0 but unknown.";
1854
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001855 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1856 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1857 << "Should return unknown when the device id is valid but the sources are not "
1858 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001859
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001860 ASSERT_EQ(AKEY_STATE_DOWN,
1861 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1862 KEY_A))
1863 << "Should return value provided by mapper when device id is valid and the device "
1864 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001865
1866 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1867 AINPUT_SOURCE_TRACKBALL, KEY_A))
1868 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1869
1870 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1871 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1872 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1873}
1874
1875TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001876 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001877 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001878 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001879 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001880 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001881 AINPUT_SOURCE_KEYBOARD, nullptr);
1882 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001883
1884 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1885 AINPUT_SOURCE_ANY, SW_LID))
1886 << "Should return unknown when the device id is >= 0 but unknown.";
1887
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001888 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1889 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1890 << "Should return unknown when the device id is valid but the sources are not "
1891 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001892
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001893 ASSERT_EQ(AKEY_STATE_DOWN,
1894 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1895 SW_LID))
1896 << "Should return value provided by mapper when device id is valid and the device "
1897 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001898
1899 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1900 AINPUT_SOURCE_TRACKBALL, SW_LID))
1901 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1902
1903 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1904 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1905 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1906}
1907
1908TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001909 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001910 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001911 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001912 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001913 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001914 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001915
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001916 mapper.addSupportedKeyCode(AKEYCODE_A);
1917 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001918
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001919 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001920 uint8_t flags[4] = { 0, 0, 0, 1 };
1921
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001922 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001923 << "Should return false when device id is >= 0 but unknown.";
1924 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1925
1926 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001927 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001928 << "Should return false when device id is valid but the sources are not supported by "
1929 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001930 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1931
1932 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001933 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001934 keyCodes, flags))
1935 << "Should return value provided by mapper when device id is valid and the device "
1936 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001937 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1938
1939 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001940 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1941 << "Should return false when the device id is < 0 but the sources are not supported by "
1942 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001943 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1944
1945 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001946 ASSERT_TRUE(
1947 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1948 << "Should return value provided by mapper when device id is < 0 and one of the "
1949 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001950 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1951}
1952
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001953TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001954 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001955 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001956
1957 NotifyConfigurationChangedArgs args;
1958
1959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1960 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1961}
1962
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001963TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001964 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001965 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001966 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001967 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001968 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001969 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001970 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001971 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001972
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001973 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001974 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001975 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1976
1977 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001978 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001979 ASSERT_EQ(when, event.when);
1980 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001981 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001982 ASSERT_EQ(EV_KEY, event.type);
1983 ASSERT_EQ(KEY_A, event.code);
1984 ASSERT_EQ(1, event.value);
1985}
1986
Garfield Tan1c7bc862020-01-28 13:24:04 -08001987TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001988 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001989 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001990 constexpr int32_t eventHubId = 1;
1991 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001992 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001993 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001994 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001995 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001996
1997 NotifyDeviceResetArgs resetArgs;
1998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001999 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002000
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002001 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002002 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002004 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002005 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002006
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002007 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002008 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002010 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002011 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002012
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002013 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002014 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002016 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002017 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002018}
2019
Garfield Tan1c7bc862020-01-28 13:24:04 -08002020TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
2021 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002022 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002023 constexpr int32_t eventHubId = 1;
2024 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2025 // Must add at least one mapper or the device will be ignored!
2026 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002027 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002028 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
2029
2030 NotifyDeviceResetArgs resetArgs;
2031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2032 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
2033}
2034
Arthur Hungc23540e2018-11-29 20:42:11 +08002035TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002036 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002037 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002038 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08002039 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002040 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2041 FakeInputMapper& mapper =
2042 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002043 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002044
2045 const uint8_t hdmi1 = 1;
2046
2047 // Associated touch screen with second display.
2048 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2049
2050 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002051 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002052 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002053 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002054 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002055 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002056 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002057 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002058 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002059 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002060
2061 // Add the device, and make sure all of the callbacks are triggered.
2062 // The device is added after the input port associations are processed since
2063 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002064 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002067 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002068
Arthur Hung2c9a3342019-07-23 14:18:59 +08002069 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002070 ASSERT_EQ(deviceId, device->getId());
2071 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2072 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002073
2074 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002075 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002076 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002077 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002078}
2079
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002080TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2081 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002082 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002083 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2084 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2085 // Must add at least one mapper or the device will be ignored!
2086 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2087 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2088 mReader->pushNextDevice(device);
2089 mReader->pushNextDevice(device);
2090 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2091 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2092
2093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2094
2095 NotifyDeviceResetArgs resetArgs;
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 disableDevice(deviceId);
2103 mReader->loopOnce();
2104
2105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2106 ASSERT_EQ(deviceId, resetArgs.deviceId);
2107 ASSERT_FALSE(device->isEnabled());
2108 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2109 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2110
2111 enableDevice(deviceId);
2112 mReader->loopOnce();
2113
2114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2115 ASSERT_EQ(deviceId, resetArgs.deviceId);
2116 ASSERT_TRUE(device->isEnabled());
2117 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2118 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2119}
2120
2121TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2122 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002123 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002124 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2125 // Add two subdevices to device
2126 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2127 FakeInputMapper& mapperDevice1 =
2128 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2129 FakeInputMapper& mapperDevice2 =
2130 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2131 mReader->pushNextDevice(device);
2132 mReader->pushNextDevice(device);
2133 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2134 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2135
2136 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2137 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2138
2139 ASSERT_EQ(AKEY_STATE_DOWN,
2140 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2141 ASSERT_EQ(AKEY_STATE_DOWN,
2142 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2143 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2144 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2145}
2146
Prabir Pradhan7e186182020-11-10 13:56:45 -08002147TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2148 NotifyPointerCaptureChangedArgs args;
2149
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002150 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002151 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2152 mReader->loopOnce();
2153 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002154 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2155 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002156
2157 mFakePolicy->setPointerCapture(false);
2158 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2159 mReader->loopOnce();
2160 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002161 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002162
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002163 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002164 // does not change.
2165 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2166 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002167 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002168}
2169
Chris Ye87143712020-11-10 05:05:58 +00002170class FakeVibratorInputMapper : public FakeInputMapper {
2171public:
2172 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2173 : FakeInputMapper(deviceContext, sources) {}
2174
2175 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2176};
2177
2178TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2179 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002180 ftl::Flags<InputDeviceClass> deviceClass =
2181 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002182 constexpr int32_t eventHubId = 1;
2183 const char* DEVICE_LOCATION = "BLUETOOTH";
2184 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2185 FakeVibratorInputMapper& mapper =
2186 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2187 mReader->pushNextDevice(device);
2188
2189 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2190 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2191
2192 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2193 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2194}
2195
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002196// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002197
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002198class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002199public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002200 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002201
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002202 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002203
Andy Chenf9f1a022022-08-29 20:07:10 -04002204 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2205
Chris Yee2b1e5c2021-03-10 22:45:12 -08002206 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2207
2208 void dump(std::string& dump) override {}
2209
2210 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2211 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002212 }
2213
Chris Yee2b1e5c2021-03-10 22:45:12 -08002214 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2215 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002216 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002217
2218 bool setLightColor(int32_t lightId, int32_t color) override {
2219 getDeviceContext().setLightBrightness(lightId, color >> 24);
2220 return true;
2221 }
2222
2223 std::optional<int32_t> getLightColor(int32_t lightId) override {
2224 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2225 if (!result.has_value()) {
2226 return std::nullopt;
2227 }
2228 return result.value() << 24;
2229 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002230
2231 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2232
2233 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2234
2235private:
2236 InputDeviceContext& mDeviceContext;
2237 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2238 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04002239 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002240};
2241
Chris Yee2b1e5c2021-03-10 22:45:12 -08002242TEST_F(InputReaderTest, BatteryGetCapacity) {
2243 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002244 ftl::Flags<InputDeviceClass> deviceClass =
2245 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002246 constexpr int32_t eventHubId = 1;
2247 const char* DEVICE_LOCATION = "BLUETOOTH";
2248 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002249 FakePeripheralController& controller =
2250 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002251 mReader->pushNextDevice(device);
2252
2253 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2254
2255 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2256 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2257}
2258
2259TEST_F(InputReaderTest, BatteryGetStatus) {
2260 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002261 ftl::Flags<InputDeviceClass> deviceClass =
2262 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002263 constexpr int32_t eventHubId = 1;
2264 const char* DEVICE_LOCATION = "BLUETOOTH";
2265 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002266 FakePeripheralController& controller =
2267 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002268 mReader->pushNextDevice(device);
2269
2270 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2271
2272 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2273 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2274}
2275
Prabir Pradhane287ecd2022-09-07 21:18:05 +00002276TEST_F(InputReaderTest, BatteryGetDevicePath) {
2277 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2278 ftl::Flags<InputDeviceClass> deviceClass =
2279 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2280 constexpr int32_t eventHubId = 1;
2281 const char* DEVICE_LOCATION = "BLUETOOTH";
2282 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2283 device->addController<FakePeripheralController>(eventHubId);
2284 mReader->pushNextDevice(device);
2285
2286 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2287
2288 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), BATTERY_DEVPATH);
2289}
2290
Chris Ye3fdbfef2021-01-06 18:45:18 -08002291TEST_F(InputReaderTest, LightGetColor) {
2292 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002293 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002294 constexpr int32_t eventHubId = 1;
2295 const char* DEVICE_LOCATION = "BLUETOOTH";
2296 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002297 FakePeripheralController& controller =
2298 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002299 mReader->pushNextDevice(device);
2300 RawLightInfo info = {.id = 1,
2301 .name = "Mono",
2302 .maxBrightness = 255,
2303 .flags = InputLightClass::BRIGHTNESS,
2304 .path = ""};
2305 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2306 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2307
2308 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002309
Chris Yee2b1e5c2021-03-10 22:45:12 -08002310 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2311 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002312 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2313 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2314}
2315
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002316// --- InputReaderIntegrationTest ---
2317
2318// These tests create and interact with the InputReader only through its interface.
2319// The InputReader is started during SetUp(), which starts its processing in its own
2320// thread. The tests use linux uinput to emulate input devices.
2321// NOTE: Interacting with the physical device while these tests are running may cause
2322// the tests to fail.
2323class InputReaderIntegrationTest : public testing::Test {
2324protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002325 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002326 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002327 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002328
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002329 std::shared_ptr<FakePointerController> mFakePointerController;
2330
Chris Yea52ade12020-08-27 16:49:20 -07002331 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002332#if !defined(__ANDROID__)
2333 GTEST_SKIP();
2334#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002335 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002336 mFakePointerController = std::make_shared<FakePointerController>();
2337 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002338 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2339 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002340
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002341 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2342 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002343 ASSERT_EQ(mReader->start(), OK);
2344
2345 // Since this test is run on a real device, all the input devices connected
2346 // to the test device will show up in mReader. We wait for those input devices to
2347 // show up before beginning the tests.
2348 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2349 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2350 }
2351
Chris Yea52ade12020-08-27 16:49:20 -07002352 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002353#if !defined(__ANDROID__)
2354 return;
2355#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002356 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002357 mReader.reset();
2358 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002359 mFakePolicy.clear();
2360 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00002361
2362 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
2363 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
2364 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
2365 [&name](const InputDeviceInfo& info) {
2366 return info.getIdentifier().name == name;
2367 });
2368 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
2369 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002370};
2371
2372TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2373 // An invalid input device that is only used for this test.
2374 class InvalidUinputDevice : public UinputDevice {
2375 public:
Prabir Pradhanb7d434e2022-10-14 22:41:38 +00002376 InvalidUinputDevice() : UinputDevice("Invalid Device", 99 /*productId*/) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002377
2378 private:
2379 void configureDevice(int fd, uinput_user_dev* device) override {}
2380 };
2381
2382 const size_t numDevices = mFakePolicy->getInputDevices().size();
2383
2384 // UinputDevice does not set any event or key bits, so InputReader should not
2385 // consider it as a valid device.
2386 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2387 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2388 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2389 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2390
2391 invalidDevice.reset();
2392 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2393 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2394 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2395}
2396
2397TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2398 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2399
2400 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2401 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2402 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2403 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2404
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002405 const auto device = findDeviceByName(keyboard->getName());
2406 ASSERT_TRUE(device.has_value());
2407 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
2408 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
2409 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002410
2411 keyboard.reset();
2412 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2413 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2414 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2415}
2416
2417TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2418 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2419 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2420
2421 NotifyConfigurationChangedArgs configChangedArgs;
2422 ASSERT_NO_FATAL_FAILURE(
2423 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002424 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002425 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2426
2427 NotifyKeyArgs keyArgs;
2428 keyboard->pressAndReleaseHomeKey();
2429 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2430 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002431 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002432 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002433 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002434 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002435 prevTimestamp = keyArgs.eventTime;
2436
2437 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2438 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002439 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002440 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002441 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002442}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002443
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002444TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
2445 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2446 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2447
2448 const auto device = findDeviceByName(stylus->getName());
2449 ASSERT_TRUE(device.has_value());
2450
Prabir Pradhana3621852022-10-14 18:57:23 +00002451 // An external stylus with buttons should also be recognized as a keyboard.
2452 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002453 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
2454 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
2455
2456 const auto DOWN =
2457 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
2458 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
2459
2460 stylus->pressAndReleaseKey(BTN_STYLUS);
2461 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2462 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2463 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2464 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2465
2466 stylus->pressAndReleaseKey(BTN_STYLUS2);
2467 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2468 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
2469 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2470 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
2471
2472 stylus->pressAndReleaseKey(BTN_STYLUS3);
2473 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2474 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
2475 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2476 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
2477}
2478
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002479/**
2480 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2481 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2482 * are passed to the listener.
2483 */
2484static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2485TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2486 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2487 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2488 NotifyKeyArgs keyArgs;
2489
2490 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2491 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2492 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2493 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2494
2495 controller->pressAndReleaseKey(BTN_GEAR_UP);
2496 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2497 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2498 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2499}
2500
Arthur Hungaab25622020-01-16 11:22:11 +08002501// --- TouchProcessTest ---
2502class TouchIntegrationTest : public InputReaderIntegrationTest {
2503protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002504 const std::string UNIQUE_ID = "local:0";
2505
Chris Yea52ade12020-08-27 16:49:20 -07002506 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002507#if !defined(__ANDROID__)
2508 GTEST_SKIP();
2509#endif
Arthur Hungaab25622020-01-16 11:22:11 +08002510 InputReaderIntegrationTest::SetUp();
2511 // At least add an internal display.
2512 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2513 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002514 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002515
2516 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2517 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2518 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00002519 const auto info = findDeviceByName(mDevice->getName());
2520 ASSERT_TRUE(info);
2521 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08002522 }
2523
2524 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2525 int32_t orientation, const std::string& uniqueId,
2526 std::optional<uint8_t> physicalPort,
2527 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002528 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2529 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002530 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2531 }
2532
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002533 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2534 NotifyMotionArgs args;
2535 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2536 EXPECT_EQ(action, args.action);
2537 ASSERT_EQ(points.size(), args.pointerCount);
2538 for (size_t i = 0; i < args.pointerCount; i++) {
2539 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2540 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2541 }
2542 }
2543
Arthur Hungaab25622020-01-16 11:22:11 +08002544 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00002545 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08002546};
2547
2548TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2549 NotifyMotionArgs args;
2550 const Point centerPoint = mDevice->getCenterPoint();
2551
2552 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002553 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002554 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002555 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002556 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2557 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2558
2559 // ACTION_MOVE
2560 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002561 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002562 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2563 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2564
2565 // ACTION_UP
2566 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002567 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002568 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2569 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2570}
2571
2572TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2573 NotifyMotionArgs args;
2574 const Point centerPoint = mDevice->getCenterPoint();
2575
2576 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002577 mDevice->sendSlot(FIRST_SLOT);
2578 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002579 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002580 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002581 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2582 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2583
2584 // ACTION_POINTER_DOWN (Second slot)
2585 const Point secondPoint = centerPoint + Point(100, 100);
2586 mDevice->sendSlot(SECOND_SLOT);
2587 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002588 mDevice->sendDown(secondPoint);
2589 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002590 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002591 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002592
2593 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002594 mDevice->sendMove(secondPoint + Point(1, 1));
2595 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002596 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2597 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2598
2599 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002600 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002601 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002602 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002603 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002604
2605 // ACTION_UP
2606 mDevice->sendSlot(FIRST_SLOT);
2607 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002608 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002609 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2610 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2611}
2612
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002613/**
2614 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2615 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2616 * data?
2617 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2618 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2619 * for Pointer 0 only is generated after.
2620 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2621 * events, we will not miss any information.
2622 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2623 * event generated afterwards that contains the newest movement of pointer 0.
2624 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2625 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2626 * losing information about non-palm pointers.
2627 */
2628TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2629 NotifyMotionArgs args;
2630 const Point centerPoint = mDevice->getCenterPoint();
2631
2632 // ACTION_DOWN
2633 mDevice->sendSlot(FIRST_SLOT);
2634 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2635 mDevice->sendDown(centerPoint);
2636 mDevice->sendSync();
2637 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2638
2639 // ACTION_POINTER_DOWN (Second slot)
2640 const Point secondPoint = centerPoint + Point(100, 100);
2641 mDevice->sendSlot(SECOND_SLOT);
2642 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2643 mDevice->sendDown(secondPoint);
2644 mDevice->sendSync();
2645 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2646
2647 // ACTION_MOVE (First slot)
2648 mDevice->sendSlot(FIRST_SLOT);
2649 mDevice->sendMove(centerPoint + Point(5, 5));
2650 // ACTION_POINTER_UP (Second slot)
2651 mDevice->sendSlot(SECOND_SLOT);
2652 mDevice->sendPointerUp();
2653 // Send a single sync for the above 2 pointer updates
2654 mDevice->sendSync();
2655
2656 // First, we should get POINTER_UP for the second pointer
2657 assertReceivedMotion(ACTION_POINTER_1_UP,
2658 {/*first pointer */ centerPoint + Point(5, 5),
2659 /*second pointer*/ secondPoint});
2660
2661 // Next, the MOVE event for the first pointer
2662 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2663}
2664
2665/**
2666 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2667 * move, and then it will go up, all in the same frame.
2668 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2669 * gets sent to the listener.
2670 */
2671TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2672 NotifyMotionArgs args;
2673 const Point centerPoint = mDevice->getCenterPoint();
2674
2675 // ACTION_DOWN
2676 mDevice->sendSlot(FIRST_SLOT);
2677 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2678 mDevice->sendDown(centerPoint);
2679 mDevice->sendSync();
2680 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2681
2682 // ACTION_POINTER_DOWN (Second slot)
2683 const Point secondPoint = centerPoint + Point(100, 100);
2684 mDevice->sendSlot(SECOND_SLOT);
2685 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2686 mDevice->sendDown(secondPoint);
2687 mDevice->sendSync();
2688 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2689
2690 // ACTION_MOVE (First slot)
2691 mDevice->sendSlot(FIRST_SLOT);
2692 mDevice->sendMove(centerPoint + Point(5, 5));
2693 // ACTION_POINTER_UP (Second slot)
2694 mDevice->sendSlot(SECOND_SLOT);
2695 mDevice->sendMove(secondPoint + Point(6, 6));
2696 mDevice->sendPointerUp();
2697 // Send a single sync for the above 2 pointer updates
2698 mDevice->sendSync();
2699
2700 // First, we should get POINTER_UP for the second pointer
2701 // The movement of the second pointer during the liftoff frame is ignored.
2702 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2703 assertReceivedMotion(ACTION_POINTER_1_UP,
2704 {/*first pointer */ centerPoint + Point(5, 5),
2705 /*second pointer*/ secondPoint});
2706
2707 // Next, the MOVE event for the first pointer
2708 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2709}
2710
Arthur Hungaab25622020-01-16 11:22:11 +08002711TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2712 NotifyMotionArgs args;
2713 const Point centerPoint = mDevice->getCenterPoint();
2714
2715 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002716 mDevice->sendSlot(FIRST_SLOT);
2717 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002718 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002719 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002720 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2721 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2722
arthurhungcc7f9802020-04-30 17:55:40 +08002723 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002724 const Point secondPoint = centerPoint + Point(100, 100);
2725 mDevice->sendSlot(SECOND_SLOT);
2726 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2727 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002728 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002729 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002730 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002731
arthurhungcc7f9802020-04-30 17:55:40 +08002732 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002733 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002734 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002735 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2736 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2737
arthurhungcc7f9802020-04-30 17:55:40 +08002738 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2739 // a palm event.
2740 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002741 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002742 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002743 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002744 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002745 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002746
arthurhungcc7f9802020-04-30 17:55:40 +08002747 // Send up to second slot, expect first slot send moving.
2748 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002749 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002750 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2751 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002752
arthurhungcc7f9802020-04-30 17:55:40 +08002753 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002754 mDevice->sendSlot(FIRST_SLOT);
2755 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002756 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002757
arthurhungcc7f9802020-04-30 17:55:40 +08002758 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2759 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002760}
2761
Prabir Pradhanda20b172022-09-26 17:01:18 +00002762TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
2763 const Point centerPoint = mDevice->getCenterPoint();
2764
2765 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
2766 mDevice->sendSlot(FIRST_SLOT);
2767 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2768 mDevice->sendToolType(MT_TOOL_PEN);
2769 mDevice->sendDown(centerPoint);
2770 mDevice->sendSync();
2771 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2772 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2773 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2774
2775 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2776
2777 // Release the stylus touch.
2778 mDevice->sendUp();
2779 mDevice->sendSync();
2780 ASSERT_NO_FATAL_FAILURE(
2781 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2782
2783 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2784
2785 // Touch down with the finger, without the pen tool selected. The policy is not notified.
2786 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2787 mDevice->sendToolType(MT_TOOL_FINGER);
2788 mDevice->sendDown(centerPoint);
2789 mDevice->sendSync();
2790 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2791 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2792 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2793
2794 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2795
2796 mDevice->sendUp();
2797 mDevice->sendSync();
2798 ASSERT_NO_FATAL_FAILURE(
2799 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2800
2801 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
2802 // The policy should be notified of the stylus presence.
2803 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2804 mDevice->sendToolType(MT_TOOL_PEN);
2805 mDevice->sendMove(centerPoint);
2806 mDevice->sendSync();
2807 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2808 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2809 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2810
2811 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2812}
2813
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002814TEST_F(TouchIntegrationTest, StylusButtonsGenerateKeyEvents) {
2815 mDevice->sendKey(BTN_STYLUS, 1);
2816 mDevice->sendSync();
2817 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2818 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2819 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2820
2821 mDevice->sendKey(BTN_STYLUS, 0);
2822 mDevice->sendSync();
2823 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2824 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2825 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2826}
2827
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002828TEST_F(TouchIntegrationTest, StylusButtonsSurroundingTouchGesture) {
2829 const Point centerPoint = mDevice->getCenterPoint();
2830
2831 // Press the stylus button.
2832 mDevice->sendKey(BTN_STYLUS, 1);
2833 mDevice->sendSync();
2834 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2835 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2836 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2837
2838 // Start and finish a stylus gesture.
2839 mDevice->sendSlot(FIRST_SLOT);
2840 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2841 mDevice->sendToolType(MT_TOOL_PEN);
2842 mDevice->sendDown(centerPoint);
2843 mDevice->sendSync();
2844 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2845 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2846 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
2847 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2848 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2849 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2850 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
2851 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2852
2853 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2854 mDevice->sendSync();
2855 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2856 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2857 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2858 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2859 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2860 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2861
2862 // Release the stylus button.
2863 mDevice->sendKey(BTN_STYLUS, 0);
2864 mDevice->sendSync();
2865 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2866 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2867 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2868}
2869
2870TEST_F(TouchIntegrationTest, StylusButtonsWithinTouchGesture) {
2871 const Point centerPoint = mDevice->getCenterPoint();
2872
2873 // Start a stylus gesture.
2874 mDevice->sendSlot(FIRST_SLOT);
2875 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2876 mDevice->sendToolType(MT_TOOL_PEN);
2877 mDevice->sendDown(centerPoint);
2878 mDevice->sendSync();
2879 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2880 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2881 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2882
2883 // Press and release a stylus button. Each change in button state also generates a MOVE event.
2884 mDevice->sendKey(BTN_STYLUS, 1);
2885 mDevice->sendSync();
2886 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2887 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2888 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2889 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2890 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2891 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
2892 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2893 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2894 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2895 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
2896 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2897
2898 mDevice->sendKey(BTN_STYLUS, 0);
2899 mDevice->sendSync();
2900 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2901 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2902 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2903 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2904 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2905 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2906 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2907 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2908 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2909
2910 // Finish the stylus gesture.
2911 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2912 mDevice->sendSync();
2913 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2914 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2915 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2916}
2917
Michael Wrightd02c5b62014-02-10 15:10:22 -08002918// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002919class InputDeviceTest : public testing::Test {
2920protected:
2921 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002922 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002923 static const int32_t DEVICE_ID;
2924 static const int32_t DEVICE_GENERATION;
2925 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002926 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002927 static const int32_t EVENTHUB_ID;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002928 static const std::string DEVICE_BLUETOOTH_ADDRESS;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002929
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002930 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002931 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002932 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002933 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002934 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002935
Chris Yea52ade12020-08-27 16:49:20 -07002936 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002937 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002938 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002939 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002940 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002941 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002942 InputDeviceIdentifier identifier;
2943 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002944 identifier.location = DEVICE_LOCATION;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002945 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
arthurhungdcef2dc2020-08-11 14:47:50 +08002946 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002947 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002948 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002949 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002950 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002951 }
2952
Chris Yea52ade12020-08-27 16:49:20 -07002953 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002954 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002955 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002956 }
2957};
2958
2959const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002960const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002961const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002962const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2963const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002964const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002965 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002966const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002967const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002968
2969TEST_F(InputDeviceTest, ImmutableProperties) {
2970 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002971 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002972 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002973}
2974
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002975TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
2976 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2977
2978 // Configuration
2979 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2980 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002981 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002982
2983 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2984}
2985
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002986TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2987 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002988}
2989
Michael Wrightd02c5b62014-02-10 15:10:22 -08002990TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2991 // Configuration.
2992 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002993 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994
2995 // Reset.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002996 unused += mDevice->reset(ARBITRARY_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002997
2998 NotifyDeviceResetArgs resetArgs;
2999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3000 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3001 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3002
3003 // Metadata.
3004 ASSERT_TRUE(mDevice->isIgnored());
3005 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
3006
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00003007 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003008 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003009 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003010 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
3011 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
3012
3013 // State queries.
3014 ASSERT_EQ(0, mDevice->getMetaState());
3015
3016 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
3017 << "Ignored device should return unknown key code state.";
3018 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
3019 << "Ignored device should return unknown scan code state.";
3020 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
3021 << "Ignored device should return unknown switch state.";
3022
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003023 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08003024 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003025 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003026 << "Ignored device should never mark any key codes.";
3027 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
3028 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
3029}
3030
3031TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
3032 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003033 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08003034
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003035 FakeInputMapper& mapper1 =
3036 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003037 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3038 mapper1.setMetaState(AMETA_ALT_ON);
3039 mapper1.addSupportedKeyCode(AKEYCODE_A);
3040 mapper1.addSupportedKeyCode(AKEYCODE_B);
3041 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
3042 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
3043 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
3044 mapper1.setScanCodeState(3, AKEY_STATE_UP);
3045 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003046
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003047 FakeInputMapper& mapper2 =
3048 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003049 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003050
3051 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003052 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003053
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003054 std::string propertyValue;
3055 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003056 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003057 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003058
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003059 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
3060 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003061
3062 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003063 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003064 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
3065 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003066
3067 NotifyDeviceResetArgs resetArgs;
3068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3069 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3070 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3071
3072 // Metadata.
3073 ASSERT_FALSE(mDevice->isIgnored());
3074 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
3075
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00003076 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003078 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
3080 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
3081
3082 // State queries.
3083 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
3084 << "Should query mappers and combine meta states.";
3085
3086 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3087 << "Should return unknown key code state when source not supported.";
3088 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3089 << "Should return unknown scan code state when source not supported.";
3090 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3091 << "Should return unknown switch state when source not supported.";
3092
3093 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
3094 << "Should query mapper when source is supported.";
3095 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
3096 << "Should query mapper when source is supported.";
3097 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
3098 << "Should query mapper when source is supported.";
3099
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003100 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08003101 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003102 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103 << "Should do nothing when source is unsupported.";
3104 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
3105 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
3106 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
3107 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
3108
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003109 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110 << "Should query mapper when source is supported.";
3111 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
3112 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
3113 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
3114 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
3115
3116 // Event handling.
3117 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003118 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003119 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003120
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003121 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
3122 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003123}
3124
Arthur Hung2c9a3342019-07-23 14:18:59 +08003125// A single input device is associated with a specific display. Check that:
3126// 1. Device is disabled if the viewport corresponding to the associated display is not found
3127// 2. Device is disabled when setEnabled API is called
3128TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003129 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003130
3131 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003132 std::list<NotifyArgs> unused =
3133 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003134
3135 // Device should be enabled by default.
3136 ASSERT_TRUE(mDevice->isEnabled());
3137
3138 // Prepare associated info.
3139 constexpr uint8_t hdmi = 1;
3140 const std::string UNIQUE_ID = "local:1";
3141
3142 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003143 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3144 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003145 // Device should be disabled because it is associated with a specific display via
3146 // input port <-> display port association, but the corresponding display is not found
3147 ASSERT_FALSE(mDevice->isEnabled());
3148
3149 // Prepare displays.
3150 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003151 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
3152 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003153 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3154 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003155 ASSERT_TRUE(mDevice->isEnabled());
3156
3157 // Device should be disabled after set disable.
3158 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003159 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3160 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003161 ASSERT_FALSE(mDevice->isEnabled());
3162
3163 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003164 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3165 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003166 ASSERT_FALSE(mDevice->isEnabled());
3167}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168
Christine Franks1ba71cc2021-04-07 14:37:42 -07003169TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
3170 // Device should be enabled by default.
3171 mFakePolicy->clearViewports();
3172 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003173 std::list<NotifyArgs> unused =
3174 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003175 ASSERT_TRUE(mDevice->isEnabled());
3176
3177 // Device should be disabled because it is associated with a specific display, but the
3178 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08003179 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003180 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3181 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003182 ASSERT_FALSE(mDevice->isEnabled());
3183
3184 // Device should be enabled when a display is found.
3185 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3186 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3187 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003188 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3189 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003190 ASSERT_TRUE(mDevice->isEnabled());
3191
3192 // Device should be disabled after set disable.
3193 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003194 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3195 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003196 ASSERT_FALSE(mDevice->isEnabled());
3197
3198 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003199 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3200 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003201 ASSERT_FALSE(mDevice->isEnabled());
3202}
3203
Christine Franks2a2293c2022-01-18 11:51:16 -08003204TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
3205 mFakePolicy->clearViewports();
3206 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003207 std::list<NotifyArgs> unused =
3208 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08003209
Christine Franks2a2293c2022-01-18 11:51:16 -08003210 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
3211 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3212 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3213 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003214 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3215 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08003216 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
3217}
3218
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07003219/**
3220 * This test reproduces a crash caused by a dangling reference that remains after device is added
3221 * and removed. The reference is accessed in InputDevice::dump(..);
3222 */
3223TEST_F(InputDeviceTest, DumpDoesNotCrash) {
3224 constexpr int32_t TEST_EVENTHUB_ID = 10;
3225 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
3226
3227 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
3228 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
3229 device.removeEventHubDevice(TEST_EVENTHUB_ID);
3230 std::string dumpStr, eventHubDevStr;
3231 device.dump(dumpStr, eventHubDevStr);
3232}
3233
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003234TEST_F(InputDeviceTest, GetBluetoothAddress) {
3235 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
3236 ASSERT_TRUE(address);
3237 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
3238}
3239
Michael Wrightd02c5b62014-02-10 15:10:22 -08003240// --- InputMapperTest ---
3241
3242class InputMapperTest : public testing::Test {
3243protected:
3244 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003245 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003246 static const int32_t DEVICE_ID;
3247 static const int32_t DEVICE_GENERATION;
3248 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003249 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003250 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003251
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003252 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003253 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003254 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08003255 std::unique_ptr<InstrumentedInputReader> mReader;
3256 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003257
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003258 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003259 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003260 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003261 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08003262 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003263 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08003264 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003265 // Consume the device reset notification generated when adding a new device.
3266 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003267 }
3268
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003269 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003270 SetUp(DEVICE_CLASSES);
3271 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003272
Chris Yea52ade12020-08-27 16:49:20 -07003273 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003274 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003275 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276 }
3277
3278 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003279 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003280 }
3281
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003282 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003283 if (!changes ||
3284 (changes &
3285 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3286 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003287 mReader->requestRefreshConfiguration(changes);
3288 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003289 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003290 std::list<NotifyArgs> out =
3291 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003292 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003293 for (const NotifyArgs& args : out) {
3294 mFakeListener->notify(args);
3295 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003296 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003297 return out;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003298 }
3299
arthurhungdcef2dc2020-08-11 14:47:50 +08003300 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3301 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003302 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003303 InputDeviceIdentifier identifier;
3304 identifier.name = name;
3305 identifier.location = location;
3306 std::shared_ptr<InputDevice> device =
3307 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3308 identifier);
3309 mReader->pushNextDevice(device);
3310 mFakeEventHub->addDevice(eventHubId, name, classes);
3311 mReader->loopOnce();
3312 return device;
3313 }
3314
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003315 template <class T, typename... Args>
3316 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003317 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003318 configureDevice(0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003319 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
3320 resetArgList += mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003321 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003322 for (const NotifyArgs& loopArgs : resetArgList) {
3323 mFakeListener->notify(loopArgs);
3324 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003325 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003326 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327 }
3328
3329 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003330 int32_t orientation, const std::string& uniqueId,
3331 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003332 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3333 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003334 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3335 }
3336
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003337 void clearViewports() {
3338 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003339 }
3340
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003341 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
3342 int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003343 RawEvent event;
3344 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003345 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003346 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003347 event.type = type;
3348 event.code = code;
3349 event.value = value;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003350 std::list<NotifyArgs> processArgList = mapper.process(&event);
3351 for (const NotifyArgs& args : processArgList) {
3352 mFakeListener->notify(args);
3353 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003354 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003355 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003356 return processArgList;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003357 }
3358
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00003359 void resetMapper(InputMapper& mapper, nsecs_t when) {
3360 const auto resetArgs = mapper.reset(when);
3361 for (const auto args : resetArgs) {
3362 mFakeListener->notify(args);
3363 }
3364 // Loop the reader to flush the input listener queue.
3365 mReader->loopOnce();
3366 }
3367
Michael Wrightd02c5b62014-02-10 15:10:22 -08003368 static void assertMotionRange(const InputDeviceInfo& info,
3369 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3370 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003371 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003372 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3373 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3374 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3375 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3376 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3377 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3378 }
3379
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003380 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3381 float size, float touchMajor, float touchMinor, float toolMajor,
3382 float toolMinor, float orientation, float distance,
3383 float scaledAxisEpsilon = 1.f) {
3384 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3385 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003386 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3387 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003388 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3389 scaledAxisEpsilon);
3390 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3391 scaledAxisEpsilon);
3392 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3393 scaledAxisEpsilon);
3394 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3395 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003396 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3397 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3398 }
3399
Michael Wright17db18e2020-06-26 20:51:44 +01003400 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003401 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003402 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003403 ASSERT_NEAR(x, actualX, 1);
3404 ASSERT_NEAR(y, actualY, 1);
3405 }
3406};
3407
3408const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003409const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003410const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003411const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3412const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003413const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3414 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003415const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003416
3417// --- SwitchInputMapperTest ---
3418
3419class SwitchInputMapperTest : public InputMapperTest {
3420protected:
3421};
3422
3423TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003424 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003426 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003427}
3428
3429TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003430 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003431
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003432 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003433 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003434
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003435 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003436 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003437}
3438
3439TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003440 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003441 std::list<NotifyArgs> out;
3442 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3443 ASSERT_TRUE(out.empty());
3444 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3445 ASSERT_TRUE(out.empty());
3446 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3447 ASSERT_TRUE(out.empty());
3448 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003449
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003450 ASSERT_EQ(1u, out.size());
3451 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003452 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003453 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3454 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003455 args.switchMask);
3456 ASSERT_EQ(uint32_t(0), args.policyFlags);
3457}
3458
Chris Ye87143712020-11-10 05:05:58 +00003459// --- VibratorInputMapperTest ---
3460class VibratorInputMapperTest : public InputMapperTest {
3461protected:
3462 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3463};
3464
3465TEST_F(VibratorInputMapperTest, GetSources) {
3466 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3467
3468 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3469}
3470
3471TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3472 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3473
3474 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3475}
3476
3477TEST_F(VibratorInputMapperTest, Vibrate) {
3478 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003479 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003480 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3481
3482 VibrationElement pattern(2);
3483 VibrationSequence sequence(2);
3484 pattern.duration = std::chrono::milliseconds(200);
3485 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3486 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3487 sequence.addElement(pattern);
3488 pattern.duration = std::chrono::milliseconds(500);
3489 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3490 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3491 sequence.addElement(pattern);
3492
3493 std::vector<int64_t> timings = {0, 1};
3494 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3495
3496 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003497 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003498 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003499 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003500 // Verify vibrator state listener was notified.
3501 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003502 ASSERT_EQ(1u, out.size());
3503 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3504 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3505 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003506 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003507 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003508 ASSERT_FALSE(mapper.isVibrating());
3509 // Verify vibrator state listener was notified.
3510 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003511 ASSERT_EQ(1u, out.size());
3512 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3513 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3514 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003515}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003516
Chris Yef59a2f42020-10-16 12:55:26 -07003517// --- SensorInputMapperTest ---
3518
3519class SensorInputMapperTest : public InputMapperTest {
3520protected:
3521 static const int32_t ACCEL_RAW_MIN;
3522 static const int32_t ACCEL_RAW_MAX;
3523 static const int32_t ACCEL_RAW_FUZZ;
3524 static const int32_t ACCEL_RAW_FLAT;
3525 static const int32_t ACCEL_RAW_RESOLUTION;
3526
3527 static const int32_t GYRO_RAW_MIN;
3528 static const int32_t GYRO_RAW_MAX;
3529 static const int32_t GYRO_RAW_FUZZ;
3530 static const int32_t GYRO_RAW_FLAT;
3531 static const int32_t GYRO_RAW_RESOLUTION;
3532
3533 static const float GRAVITY_MS2_UNIT;
3534 static const float DEGREE_RADIAN_UNIT;
3535
3536 void prepareAccelAxes();
3537 void prepareGyroAxes();
3538 void setAccelProperties();
3539 void setGyroProperties();
3540 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3541};
3542
3543const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3544const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3545const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3546const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3547const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3548
3549const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3550const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3551const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3552const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3553const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3554
3555const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3556const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3557
3558void SensorInputMapperTest::prepareAccelAxes() {
3559 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3560 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3561 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3562 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3563 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3564 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3565}
3566
3567void SensorInputMapperTest::prepareGyroAxes() {
3568 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3569 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3570 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3571 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3572 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3573 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3574}
3575
3576void SensorInputMapperTest::setAccelProperties() {
3577 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3578 /* sensorDataIndex */ 0);
3579 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3580 /* sensorDataIndex */ 1);
3581 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3582 /* sensorDataIndex */ 2);
3583 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3584 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3585 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3586 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3587 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3588}
3589
3590void SensorInputMapperTest::setGyroProperties() {
3591 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3592 /* sensorDataIndex */ 0);
3593 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3594 /* sensorDataIndex */ 1);
3595 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3596 /* sensorDataIndex */ 2);
3597 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3598 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3599 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3600 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3601 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3602}
3603
3604TEST_F(SensorInputMapperTest, GetSources) {
3605 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3606
3607 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3608}
3609
3610TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3611 setAccelProperties();
3612 prepareAccelAxes();
3613 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3614
3615 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3616 std::chrono::microseconds(10000),
3617 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003618 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003619 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3620 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3621 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3622 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3623 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003624
3625 NotifySensorArgs args;
3626 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3627 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3628 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3629
3630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3631 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3632 ASSERT_EQ(args.deviceId, DEVICE_ID);
3633 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3634 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3635 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3636 ASSERT_EQ(args.values, values);
3637 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3638}
3639
3640TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3641 setGyroProperties();
3642 prepareGyroAxes();
3643 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3644
3645 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3646 std::chrono::microseconds(10000),
3647 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003648 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003649 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3650 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3651 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3653 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003654
3655 NotifySensorArgs args;
3656 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3657 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3658 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3659
3660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3661 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3662 ASSERT_EQ(args.deviceId, DEVICE_ID);
3663 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3664 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3665 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3666 ASSERT_EQ(args.values, values);
3667 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3668}
3669
Michael Wrightd02c5b62014-02-10 15:10:22 -08003670// --- KeyboardInputMapperTest ---
3671
3672class KeyboardInputMapperTest : public InputMapperTest {
3673protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003674 const std::string UNIQUE_ID = "local:0";
3675
3676 void prepareDisplay(int32_t orientation);
3677
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003678 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003679 int32_t originalKeyCode, int32_t rotatedKeyCode,
3680 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003681};
3682
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003683/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3684 * orientation.
3685 */
3686void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003687 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3688 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003689}
3690
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003691void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003692 int32_t originalScanCode, int32_t originalKeyCode,
3693 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003694 NotifyKeyArgs args;
3695
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003696 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3698 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3699 ASSERT_EQ(originalScanCode, args.scanCode);
3700 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003701 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003702
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003703 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3705 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3706 ASSERT_EQ(originalScanCode, args.scanCode);
3707 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003708 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003709}
3710
Michael Wrightd02c5b62014-02-10 15:10:22 -08003711TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003712 KeyboardInputMapper& mapper =
3713 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3714 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003715
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003716 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003717}
3718
3719TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3720 const int32_t USAGE_A = 0x070004;
3721 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003722 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3723 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003724 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3725 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3726 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003727
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003728 KeyboardInputMapper& mapper =
3729 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3730 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003731 // Initial metastate is AMETA_NONE.
3732 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733
3734 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003735 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003736 NotifyKeyArgs args;
3737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3738 ASSERT_EQ(DEVICE_ID, args.deviceId);
3739 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3740 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3741 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3742 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3743 ASSERT_EQ(KEY_HOME, args.scanCode);
3744 ASSERT_EQ(AMETA_NONE, args.metaState);
3745 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3746 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3747 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3748
3749 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003750 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3752 ASSERT_EQ(DEVICE_ID, args.deviceId);
3753 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3754 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3755 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3756 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3757 ASSERT_EQ(KEY_HOME, args.scanCode);
3758 ASSERT_EQ(AMETA_NONE, args.metaState);
3759 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3760 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3761 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3762
3763 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003764 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3765 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3767 ASSERT_EQ(DEVICE_ID, args.deviceId);
3768 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3769 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3770 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3771 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3772 ASSERT_EQ(0, args.scanCode);
3773 ASSERT_EQ(AMETA_NONE, args.metaState);
3774 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3775 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3776 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3777
3778 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3780 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3782 ASSERT_EQ(DEVICE_ID, args.deviceId);
3783 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3784 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3785 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3786 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3787 ASSERT_EQ(0, args.scanCode);
3788 ASSERT_EQ(AMETA_NONE, args.metaState);
3789 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3790 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3791 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3792
3793 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3797 ASSERT_EQ(DEVICE_ID, args.deviceId);
3798 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3799 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3800 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3801 ASSERT_EQ(0, args.keyCode);
3802 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3803 ASSERT_EQ(AMETA_NONE, args.metaState);
3804 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3805 ASSERT_EQ(0U, args.policyFlags);
3806 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3807
3808 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003809 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3810 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3812 ASSERT_EQ(DEVICE_ID, args.deviceId);
3813 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3814 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3815 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3816 ASSERT_EQ(0, args.keyCode);
3817 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3818 ASSERT_EQ(AMETA_NONE, args.metaState);
3819 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3820 ASSERT_EQ(0U, args.policyFlags);
3821 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3822}
3823
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003824/**
3825 * Ensure that the readTime is set to the time when the EV_KEY is received.
3826 */
3827TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3828 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3829
3830 KeyboardInputMapper& mapper =
3831 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3832 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3833 NotifyKeyArgs args;
3834
3835 // Key down
3836 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3838 ASSERT_EQ(12, args.readTime);
3839
3840 // Key up
3841 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3843 ASSERT_EQ(15, args.readTime);
3844}
3845
Michael Wrightd02c5b62014-02-10 15:10:22 -08003846TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003847 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3848 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003849 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3850 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3851 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003852
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003853 KeyboardInputMapper& mapper =
3854 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3855 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003856
Arthur Hung95f68612022-04-07 14:08:22 +08003857 // Initial metastate is AMETA_NONE.
3858 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003859
3860 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003861 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003862 NotifyKeyArgs args;
3863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3864 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003865 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003866 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003867
3868 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003869 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3871 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003872 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003873
3874 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003875 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3877 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003878 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003879
3880 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003881 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3883 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003884 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003885 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003886}
3887
3888TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003889 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3890 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3891 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3892 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003893
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003894 KeyboardInputMapper& mapper =
3895 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3896 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003897
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003898 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003899 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3900 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3901 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3902 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3903 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3904 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3905 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3906 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3907}
3908
3909TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003910 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3911 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3912 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3913 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003914
Michael Wrightd02c5b62014-02-10 15:10:22 -08003915 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003916 KeyboardInputMapper& mapper =
3917 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3918 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003919
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003920 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003921 ASSERT_NO_FATAL_FAILURE(
3922 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3923 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3924 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3925 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3926 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3927 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3928 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003929
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003930 clearViewports();
3931 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003932 ASSERT_NO_FATAL_FAILURE(
3933 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3934 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3935 AKEYCODE_DPAD_UP, DISPLAY_ID));
3936 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3937 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3938 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3939 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003940
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003941 clearViewports();
3942 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003943 ASSERT_NO_FATAL_FAILURE(
3944 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3945 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3946 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3947 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3948 AKEYCODE_DPAD_UP, DISPLAY_ID));
3949 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3950 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003951
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003952 clearViewports();
3953 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003954 ASSERT_NO_FATAL_FAILURE(
3955 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3956 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3957 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3958 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3959 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3960 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3961 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003962
3963 // Special case: if orientation changes while key is down, we still emit the same keycode
3964 // in the key up as we did in the key down.
3965 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003966 clearViewports();
3967 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003968 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3970 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3971 ASSERT_EQ(KEY_UP, args.scanCode);
3972 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3973
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003974 clearViewports();
3975 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003976 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3978 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3979 ASSERT_EQ(KEY_UP, args.scanCode);
3980 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3981}
3982
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003983TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3984 // If the keyboard is not orientation aware,
3985 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003986 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003987
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003988 KeyboardInputMapper& mapper =
3989 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3990 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003991 NotifyKeyArgs args;
3992
3993 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003996 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3998 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3999
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004000 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4005 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
4006}
4007
4008TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
4009 // If the keyboard is orientation aware,
4010 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004011 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004012
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004013 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004014 KeyboardInputMapper& mapper =
4015 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4016 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004017 NotifyKeyArgs args;
4018
4019 // Display id should be ADISPLAY_ID_NONE without any display configuration.
4020 // ^--- already checked by the previous test
4021
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004022 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004023 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004024 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004026 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4028 ASSERT_EQ(DISPLAY_ID, args.displayId);
4029
4030 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004031 clearViewports();
4032 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004033 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4038 ASSERT_EQ(newDisplayId, args.displayId);
4039}
4040
Michael Wrightd02c5b62014-02-10 15:10:22 -08004041TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004042 KeyboardInputMapper& mapper =
4043 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4044 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004045
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004046 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004047 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004048
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004049 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004050 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004051}
4052
Philip Junker4af3b3d2021-12-14 10:36:55 +01004053TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
4054 KeyboardInputMapper& mapper =
4055 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4056 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4057
4058 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
4059 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
4060 << "If a mapping is available, the result is equal to the mapping";
4061
4062 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
4063 << "If no mapping is available, the result is the key location";
4064}
4065
Michael Wrightd02c5b62014-02-10 15:10:22 -08004066TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004067 KeyboardInputMapper& mapper =
4068 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4069 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004070
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004071 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004072 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004073
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004074 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004075 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004076}
4077
4078TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004079 KeyboardInputMapper& mapper =
4080 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4081 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004082
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004083 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004084
Michael Wrightd02c5b62014-02-10 15:10:22 -08004085 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004086 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004087 ASSERT_TRUE(flags[0]);
4088 ASSERT_FALSE(flags[1]);
4089}
4090
4091TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004092 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4093 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4094 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4095 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4096 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4097 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004098
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004099 KeyboardInputMapper& mapper =
4100 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4101 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004102 // Initial metastate is AMETA_NONE.
4103 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004104
4105 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004106 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4107 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4108 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109
4110 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004111 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4112 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004113 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4114 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4115 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004116 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004117
4118 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004121 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4122 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4123 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004124 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125
4126 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004127 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4128 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004129 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4130 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4131 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004132 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004133
4134 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004137 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4138 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4139 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004140 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141
4142 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004145 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4146 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4147 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004148 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004149
4150 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4152 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004153 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4154 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4155 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004156 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157}
4158
Chris Yea52ade12020-08-27 16:49:20 -07004159TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
4160 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
4161 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
4162 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
4163 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
4164
4165 KeyboardInputMapper& mapper =
4166 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4167 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4168
Chris Yea52ade12020-08-27 16:49:20 -07004169 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004170 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07004171 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4172 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
4173 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
4174 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4175
4176 NotifyKeyArgs args;
4177 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004178 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07004179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4180 ASSERT_EQ(AMETA_NONE, args.metaState);
4181 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4182 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4183 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4184
4185 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004186 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07004187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4188 ASSERT_EQ(AMETA_NONE, args.metaState);
4189 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4190 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4191 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4192}
4193
Arthur Hung2c9a3342019-07-23 14:18:59 +08004194TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
4195 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004196 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4197 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4198 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4199 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004200
4201 // keyboard 2.
4202 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08004203 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08004204 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004205 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08004206 std::shared_ptr<InputDevice> device2 =
4207 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004208 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08004209
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004210 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4211 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4212 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4213 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004214
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004215 KeyboardInputMapper& mapper =
4216 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4217 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004218
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004219 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004220 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004221 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004222 std::list<NotifyArgs> unused =
4223 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4224 0 /*changes*/);
4225 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004226
4227 // Prepared displays and associated info.
4228 constexpr uint8_t hdmi1 = 0;
4229 constexpr uint8_t hdmi2 = 1;
4230 const std::string SECONDARY_UNIQUE_ID = "local:1";
4231
4232 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
4233 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
4234
4235 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004236 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4237 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004238 ASSERT_FALSE(device2->isEnabled());
4239
4240 // Prepare second display.
4241 constexpr int32_t newDisplayId = 2;
4242 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004243 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004244 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004245 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004246 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004247 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4248 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004249
4250 // Device should be enabled after the associated display is found.
4251 ASSERT_TRUE(mDevice->isEnabled());
4252 ASSERT_TRUE(device2->isEnabled());
4253
4254 // Test pad key events
4255 ASSERT_NO_FATAL_FAILURE(
4256 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4257 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4258 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4259 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4260 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4261 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4262 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4263
4264 ASSERT_NO_FATAL_FAILURE(
4265 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
4266 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4267 AKEYCODE_DPAD_RIGHT, newDisplayId));
4268 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4269 AKEYCODE_DPAD_DOWN, newDisplayId));
4270 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4271 AKEYCODE_DPAD_LEFT, newDisplayId));
4272}
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273
arthurhungc903df12020-08-11 15:08:42 +08004274TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
4275 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4276 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4277 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4278 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4279 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4280 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4281
4282 KeyboardInputMapper& mapper =
4283 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4284 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004285 // Initial metastate is AMETA_NONE.
4286 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004287
4288 // Initialization should have turned all of the lights off.
4289 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4290 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4291 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4292
4293 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004296 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4297 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4298
4299 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004300 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004302 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4303 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
4304
4305 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004306 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004308 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4309 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4310
4311 mFakeEventHub->removeDevice(EVENTHUB_ID);
4312 mReader->loopOnce();
4313
4314 // keyboard 2 should default toggle keys.
4315 const std::string USB2 = "USB2";
4316 const std::string DEVICE_NAME2 = "KEYBOARD2";
4317 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4318 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4319 std::shared_ptr<InputDevice> device2 =
4320 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004321 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004322 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4323 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4324 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4325 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4326 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4327 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4328
arthurhung6fe95782020-10-05 22:41:16 +08004329 KeyboardInputMapper& mapper2 =
4330 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4331 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004332 std::list<NotifyArgs> unused =
4333 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4334 0 /*changes*/);
4335 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08004336
4337 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4338 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4339 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004340 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4341 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004342}
4343
Arthur Hungcb40a002021-08-03 14:31:01 +00004344TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4345 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4346 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4347 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4348
4349 // Suppose we have two mappers. (DPAD + KEYBOARD)
4350 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4351 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4352 KeyboardInputMapper& mapper =
4353 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4354 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004355 // Initial metastate is AMETA_NONE.
4356 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004357
4358 mReader->toggleCapsLockState(DEVICE_ID);
4359 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4360}
4361
Arthur Hungfb3cc112022-04-13 07:39:50 +00004362TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4363 // keyboard 1.
4364 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4365 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4366 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4367 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4368 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4369 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4370
4371 KeyboardInputMapper& mapper1 =
4372 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4373 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4374
4375 // keyboard 2.
4376 const std::string USB2 = "USB2";
4377 const std::string DEVICE_NAME2 = "KEYBOARD2";
4378 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4379 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4380 std::shared_ptr<InputDevice> device2 =
4381 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4382 ftl::Flags<InputDeviceClass>(0));
4383 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4384 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4385 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4386 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4387 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4388 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4389
4390 KeyboardInputMapper& mapper2 =
4391 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4392 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004393 std::list<NotifyArgs> unused =
4394 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4395 0 /*changes*/);
4396 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004397
Arthur Hung95f68612022-04-07 14:08:22 +08004398 // Initial metastate is AMETA_NONE.
4399 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4400 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4401
4402 // Toggle num lock on and off.
4403 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4404 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004405 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4406 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4407 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4408
4409 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4410 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4411 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4412 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4413 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4414
4415 // Toggle caps lock on and off.
4416 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4417 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4418 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4419 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4420 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4421
4422 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4423 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4424 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4425 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4426 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4427
4428 // Toggle scroll lock on and off.
4429 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4430 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4431 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4432 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4433 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4434
4435 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4436 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4437 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4438 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4439 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4440}
4441
Arthur Hung2141d542022-08-23 07:45:21 +00004442TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4443 const int32_t USAGE_A = 0x070004;
4444 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4445 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4446
4447 KeyboardInputMapper& mapper =
4448 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4449 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4450 // Key down by scan code.
4451 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4452 NotifyKeyArgs args;
4453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4454 ASSERT_EQ(DEVICE_ID, args.deviceId);
4455 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4456 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4457 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4458 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4459 ASSERT_EQ(KEY_HOME, args.scanCode);
4460 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4461
4462 // Disable device, it should synthesize cancellation events for down events.
4463 mFakePolicy->addDisabledDevice(DEVICE_ID);
4464 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4465
4466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4467 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4468 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4469 ASSERT_EQ(KEY_HOME, args.scanCode);
4470 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4471}
4472
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004473// --- KeyboardInputMapperTest_ExternalDevice ---
4474
4475class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4476protected:
Chris Yea52ade12020-08-27 16:49:20 -07004477 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004478};
4479
4480TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004481 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4482 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004483
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004484 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4485 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4486 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4487 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004488
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004489 KeyboardInputMapper& mapper =
4490 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4491 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004492
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004493 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004494 NotifyKeyArgs args;
4495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4496 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4497
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004498 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4500 ASSERT_EQ(uint32_t(0), args.policyFlags);
4501
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4504 ASSERT_EQ(uint32_t(0), args.policyFlags);
4505
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004506 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4508 ASSERT_EQ(uint32_t(0), args.policyFlags);
4509
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004510 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4512 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4513
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004514 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4516 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4517}
4518
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004519TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004520 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004521
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004522 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4523 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4524 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004525
Powei Fengd041c5d2019-05-03 17:11:33 -07004526 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004527 KeyboardInputMapper& mapper =
4528 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4529 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004530
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004531 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004532 NotifyKeyArgs args;
4533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4534 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4535
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004536 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4538 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4539
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4542 ASSERT_EQ(uint32_t(0), args.policyFlags);
4543
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004544 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4546 ASSERT_EQ(uint32_t(0), args.policyFlags);
4547
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4550 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4551
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004552 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4554 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4555}
4556
Michael Wrightd02c5b62014-02-10 15:10:22 -08004557// --- CursorInputMapperTest ---
4558
4559class CursorInputMapperTest : public InputMapperTest {
4560protected:
4561 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4562
Michael Wright17db18e2020-06-26 20:51:44 +01004563 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564
Chris Yea52ade12020-08-27 16:49:20 -07004565 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004566 InputMapperTest::SetUp();
4567
Michael Wright17db18e2020-06-26 20:51:44 +01004568 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004569 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004570 }
4571
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004572 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4573 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004574
4575 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004576 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4577 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4578 }
4579
4580 void prepareSecondaryDisplay() {
4581 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4582 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4583 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004584 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004585
4586 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4587 float pressure) {
4588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4589 0.0f, 0.0f, 0.0f, EPSILON));
4590 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591};
4592
4593const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4594
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004595void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4596 int32_t originalY, int32_t rotatedX,
4597 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004598 NotifyMotionArgs args;
4599
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004600 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4601 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4602 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004605 ASSERT_NO_FATAL_FAILURE(
4606 assertCursorPointerCoords(args.pointerCoords[0],
4607 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4608 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004609}
4610
4611TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004612 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004613 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004614
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004615 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616}
4617
4618TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004619 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004620 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004621
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004622 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004623}
4624
4625TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004627 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004628
4629 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004630 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004631
4632 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004633 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4634 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004635 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4636 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4637
4638 // When the bounds are set, then there should be a valid motion range.
4639 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4640
4641 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004642 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004643
4644 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4645 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4646 1, 800 - 1, 0.0f, 0.0f));
4647 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4648 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4649 2, 480 - 1, 0.0f, 0.0f));
4650 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4651 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4652 0.0f, 1.0f, 0.0f, 0.0f));
4653}
4654
4655TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004656 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004657 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004658
4659 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004660 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004661
4662 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4663 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4664 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4665 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4666 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4667 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4668 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4669 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4670 0.0f, 1.0f, 0.0f, 0.0f));
4671}
4672
4673TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004675 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004676
arthurhungdcef2dc2020-08-11 14:47:50 +08004677 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004678
4679 NotifyMotionArgs args;
4680
4681 // Button press.
4682 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4686 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4687 ASSERT_EQ(DEVICE_ID, args.deviceId);
4688 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4689 ASSERT_EQ(uint32_t(0), args.policyFlags);
4690 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4691 ASSERT_EQ(0, args.flags);
4692 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4693 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4694 ASSERT_EQ(0, args.edgeFlags);
4695 ASSERT_EQ(uint32_t(1), args.pointerCount);
4696 ASSERT_EQ(0, args.pointerProperties[0].id);
4697 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004698 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004699 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4700 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4701 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4702
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4704 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4705 ASSERT_EQ(DEVICE_ID, args.deviceId);
4706 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4707 ASSERT_EQ(uint32_t(0), args.policyFlags);
4708 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4709 ASSERT_EQ(0, args.flags);
4710 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4711 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4712 ASSERT_EQ(0, args.edgeFlags);
4713 ASSERT_EQ(uint32_t(1), args.pointerCount);
4714 ASSERT_EQ(0, args.pointerProperties[0].id);
4715 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004716 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004717 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4718 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4719 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4720
Michael Wrightd02c5b62014-02-10 15:10:22 -08004721 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004722 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4723 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4725 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4726 ASSERT_EQ(DEVICE_ID, args.deviceId);
4727 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4728 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004729 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4730 ASSERT_EQ(0, args.flags);
4731 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4732 ASSERT_EQ(0, args.buttonState);
4733 ASSERT_EQ(0, args.edgeFlags);
4734 ASSERT_EQ(uint32_t(1), args.pointerCount);
4735 ASSERT_EQ(0, args.pointerProperties[0].id);
4736 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004737 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004738 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4739 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4740 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4741
4742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4743 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4744 ASSERT_EQ(DEVICE_ID, args.deviceId);
4745 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4746 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004747 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4748 ASSERT_EQ(0, args.flags);
4749 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4750 ASSERT_EQ(0, args.buttonState);
4751 ASSERT_EQ(0, args.edgeFlags);
4752 ASSERT_EQ(uint32_t(1), args.pointerCount);
4753 ASSERT_EQ(0, args.pointerProperties[0].id);
4754 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004755 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004756 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4757 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4758 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4759}
4760
4761TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004762 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004763 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004764
4765 NotifyMotionArgs args;
4766
4767 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4771 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004772 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4773 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4774 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004775
4776 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4780 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004781 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4782 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004783}
4784
4785TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004786 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004787 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004788
4789 NotifyMotionArgs args;
4790
4791 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4795 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004796 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004797
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4799 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004800 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004801
Michael Wrightd02c5b62014-02-10 15:10:22 -08004802 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 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(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004806 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004807 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004808
4809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004811 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004812}
4813
4814TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004815 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004816 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817
4818 NotifyMotionArgs args;
4819
4820 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004821 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4822 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4823 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4826 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004827 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4828 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4829 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004830
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4832 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004833 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4834 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4835 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004836
Michael Wrightd02c5b62014-02-10 15:10:22 -08004837 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004838 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4842 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004843 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4844 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4845 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004846
4847 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004848 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4849 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004851 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004852 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004853
4854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004855 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004856 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004857}
4858
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004859TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004860 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004862 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4863 // need to be rotated.
4864 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004865 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004866
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004867 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004868 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4869 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4870 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4871 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4872 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4873 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4874 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4875 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4876}
4877
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004878TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004879 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004880 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004881 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4882 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004883 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004884
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004885 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004886 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004887 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4888 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4889 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4890 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4891 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4892 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4893 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4894 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4895
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004896 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004897 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004898 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4899 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4900 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4901 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4902 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4903 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4904 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4905 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004906
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004907 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004908 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004909 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4910 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4911 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4912 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4913 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4914 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4915 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4916 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4917
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004918 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004919 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004920 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4921 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4922 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4923 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4924 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4925 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4926 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4927 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004928}
4929
4930TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004931 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004932 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004933
4934 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4935 mFakePointerController->setPosition(100, 200);
4936 mFakePointerController->setButtonState(0);
4937
4938 NotifyMotionArgs motionArgs;
4939 NotifyKeyArgs keyArgs;
4940
4941 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4945 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4946 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4947 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004948 ASSERT_NO_FATAL_FAILURE(
4949 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004950
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4952 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4953 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4954 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004955 ASSERT_NO_FATAL_FAILURE(
4956 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004957
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004958 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004961 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004962 ASSERT_EQ(0, motionArgs.buttonState);
4963 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004964 ASSERT_NO_FATAL_FAILURE(
4965 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966
4967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004968 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004969 ASSERT_EQ(0, motionArgs.buttonState);
4970 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004971 ASSERT_NO_FATAL_FAILURE(
4972 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004973
4974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004975 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004980
4981 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004982 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4983 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4986 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4987 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4988 motionArgs.buttonState);
4989 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4990 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004991 ASSERT_NO_FATAL_FAILURE(
4992 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004993
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4995 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4996 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4997 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4998 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004999 ASSERT_NO_FATAL_FAILURE(
5000 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005001
5002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5003 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5004 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5005 motionArgs.buttonState);
5006 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5007 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005008 ASSERT_NO_FATAL_FAILURE(
5009 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005010
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005011 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
5012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005014 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005015 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5016 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005017 ASSERT_NO_FATAL_FAILURE(
5018 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005019
5020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005021 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005022 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5023 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005024 ASSERT_NO_FATAL_FAILURE(
5025 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005026
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005027 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5028 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005030 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5031 ASSERT_EQ(0, motionArgs.buttonState);
5032 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005033 ASSERT_NO_FATAL_FAILURE(
5034 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005037
5038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005039 ASSERT_EQ(0, motionArgs.buttonState);
5040 ASSERT_EQ(0, mFakePointerController->getButtonState());
5041 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005042 ASSERT_NO_FATAL_FAILURE(
5043 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005044
Michael Wrightd02c5b62014-02-10 15:10:22 -08005045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5046 ASSERT_EQ(0, motionArgs.buttonState);
5047 ASSERT_EQ(0, mFakePointerController->getButtonState());
5048 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005049 ASSERT_NO_FATAL_FAILURE(
5050 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005051
5052 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
5054 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5056 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5057 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005058
Michael Wrightd02c5b62014-02-10 15:10:22 -08005059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005060 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5062 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005063 ASSERT_NO_FATAL_FAILURE(
5064 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005065
5066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5067 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5068 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5069 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005070 ASSERT_NO_FATAL_FAILURE(
5071 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005072
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005073 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
5074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005076 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005077 ASSERT_EQ(0, motionArgs.buttonState);
5078 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005079 ASSERT_NO_FATAL_FAILURE(
5080 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005081
5082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005083 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005084 ASSERT_EQ(0, motionArgs.buttonState);
5085 ASSERT_EQ(0, mFakePointerController->getButtonState());
5086
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005087 ASSERT_NO_FATAL_FAILURE(
5088 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5090 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5091 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5092
5093 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
5095 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5097 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5098 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005099
Michael Wrightd02c5b62014-02-10 15:10:22 -08005100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005101 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005102 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5103 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005104 ASSERT_NO_FATAL_FAILURE(
5105 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005106
5107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5108 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5109 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5110 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005111 ASSERT_NO_FATAL_FAILURE(
5112 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005114 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
5115 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005117 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005118 ASSERT_EQ(0, motionArgs.buttonState);
5119 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005120 ASSERT_NO_FATAL_FAILURE(
5121 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005122
5123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5124 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5125 ASSERT_EQ(0, motionArgs.buttonState);
5126 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005127 ASSERT_NO_FATAL_FAILURE(
5128 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005129
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5131 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5132 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5133
5134 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
5136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5138 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5139 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005140
Michael Wrightd02c5b62014-02-10 15:10:22 -08005141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005142 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005143 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5144 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005145 ASSERT_NO_FATAL_FAILURE(
5146 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005147
5148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5149 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5150 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5151 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005152 ASSERT_NO_FATAL_FAILURE(
5153 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005154
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
5156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005158 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005159 ASSERT_EQ(0, motionArgs.buttonState);
5160 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005161 ASSERT_NO_FATAL_FAILURE(
5162 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005163
5164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5165 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5166 ASSERT_EQ(0, motionArgs.buttonState);
5167 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005168 ASSERT_NO_FATAL_FAILURE(
5169 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005170
Michael Wrightd02c5b62014-02-10 15:10:22 -08005171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5172 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5173 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5174
5175 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
5177 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5179 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5180 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005181
Michael Wrightd02c5b62014-02-10 15:10:22 -08005182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005183 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005184 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5185 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005186 ASSERT_NO_FATAL_FAILURE(
5187 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005188
5189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5190 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5191 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5192 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005193 ASSERT_NO_FATAL_FAILURE(
5194 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005195
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005196 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
5197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005199 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005200 ASSERT_EQ(0, motionArgs.buttonState);
5201 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005202 ASSERT_NO_FATAL_FAILURE(
5203 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005204
5205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5206 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5207 ASSERT_EQ(0, motionArgs.buttonState);
5208 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005209 ASSERT_NO_FATAL_FAILURE(
5210 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005211
Michael Wrightd02c5b62014-02-10 15:10:22 -08005212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5213 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5214 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5215}
5216
5217TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005218 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005219 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005220
5221 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5222 mFakePointerController->setPosition(100, 200);
5223 mFakePointerController->setButtonState(0);
5224
5225 NotifyMotionArgs args;
5226
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005227 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5228 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5229 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005231 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5232 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5234 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 +01005235 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005236}
5237
5238TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005239 addConfigurationProperty("cursor.mode", "pointer");
5240 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005241 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005242
5243 NotifyDeviceResetArgs resetArgs;
5244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5245 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5246 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5247
5248 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5249 mFakePointerController->setPosition(100, 200);
5250 mFakePointerController->setButtonState(0);
5251
5252 NotifyMotionArgs args;
5253
5254 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005255 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5256 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5257 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5259 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5262 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 +01005263 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005264
5265 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005266 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5269 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5270 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5271 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5272 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5274 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5275 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
5276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5277 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5278
5279 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005280 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5281 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5283 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5284 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
5285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5286 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5288 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5289 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
5290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5291 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5292
5293 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
5295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
5296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5298 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5299 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5301 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 +01005302 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005303
5304 // Disable pointer capture and check that the device generation got bumped
5305 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08005306 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005307 mFakePolicy->setPointerCapture(false);
5308 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08005309 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005310
5311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005312 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5313
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005314 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5315 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5318 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005319 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5321 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 +01005322 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005323}
5324
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00005325/**
5326 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
5327 * pointer acceleration or speed processing should not be applied.
5328 */
5329TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5330 addConfigurationProperty("cursor.mode", "pointer");
5331 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5332 100.f /*high threshold*/, 10.f /*acceleration*/);
5333 mFakePolicy->setVelocityControlParams(testParams);
5334 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5335
5336 NotifyDeviceResetArgs resetArgs;
5337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5338 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5339 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5340
5341 NotifyMotionArgs args;
5342
5343 // Move and verify scale is applied.
5344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5345 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5346 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5348 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5349 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5350 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5351 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5352 ASSERT_GT(relX, 10);
5353 ASSERT_GT(relY, 20);
5354
5355 // Enable Pointer Capture
5356 mFakePolicy->setPointerCapture(true);
5357 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5358 NotifyPointerCaptureChangedArgs captureArgs;
5359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5360 ASSERT_TRUE(captureArgs.request.enable);
5361
5362 // Move and verify scale is not applied.
5363 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5364 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5365 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5367 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5369 ASSERT_EQ(10, args.pointerCoords[0].getX());
5370 ASSERT_EQ(20, args.pointerCoords[0].getY());
5371}
5372
Prabir Pradhan208360b2022-06-24 18:37:04 +00005373TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5374 addConfigurationProperty("cursor.mode", "pointer");
5375 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5376
5377 NotifyDeviceResetArgs resetArgs;
5378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5379 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5380 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5381
5382 // Ensure the display is rotated.
5383 prepareDisplay(DISPLAY_ORIENTATION_90);
5384
5385 NotifyMotionArgs args;
5386
5387 // Verify that the coordinates are rotated.
5388 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5389 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5392 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5393 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5394 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5395 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5396
5397 // Enable Pointer Capture.
5398 mFakePolicy->setPointerCapture(true);
5399 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5400 NotifyPointerCaptureChangedArgs captureArgs;
5401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5402 ASSERT_TRUE(captureArgs.request.enable);
5403
5404 // Move and verify rotation is not applied.
5405 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5409 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5411 ASSERT_EQ(10, args.pointerCoords[0].getX());
5412 ASSERT_EQ(20, args.pointerCoords[0].getY());
5413}
5414
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005415TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005416 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005417
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005418 // Set up the default display.
5419 prepareDisplay(DISPLAY_ORIENTATION_90);
5420
5421 // Set up the secondary display as the display on which the pointer should be shown.
5422 // The InputDevice is not associated with any display.
5423 prepareSecondaryDisplay();
5424 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005425 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5426
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005427 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005428 mFakePointerController->setPosition(100, 200);
5429 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005430
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005431 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005432 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5433 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5434 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005436 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5437 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5438 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005439 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005440}
5441
5442TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5443 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5444
5445 // Set up the default display.
5446 prepareDisplay(DISPLAY_ORIENTATION_90);
5447
5448 // Set up the secondary display as the display on which the pointer should be shown,
5449 // and associate the InputDevice with the secondary display.
5450 prepareSecondaryDisplay();
5451 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5452 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5453 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5454
5455 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5456 mFakePointerController->setPosition(100, 200);
5457 mFakePointerController->setButtonState(0);
5458
5459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5460 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5461 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005463 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5464 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5465 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005466 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5467}
5468
5469TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5470 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5471
5472 // Set up the default display as the display on which the pointer should be shown.
5473 prepareDisplay(DISPLAY_ORIENTATION_90);
5474 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5475
5476 // Associate the InputDevice with the secondary display.
5477 prepareSecondaryDisplay();
5478 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5479 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5480
5481 // The mapper should not generate any events because it is associated with a display that is
5482 // different from the pointer display.
5483 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5484 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5485 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005487}
5488
Michael Wrightd02c5b62014-02-10 15:10:22 -08005489// --- TouchInputMapperTest ---
5490
5491class TouchInputMapperTest : public InputMapperTest {
5492protected:
5493 static const int32_t RAW_X_MIN;
5494 static const int32_t RAW_X_MAX;
5495 static const int32_t RAW_Y_MIN;
5496 static const int32_t RAW_Y_MAX;
5497 static const int32_t RAW_TOUCH_MIN;
5498 static const int32_t RAW_TOUCH_MAX;
5499 static const int32_t RAW_TOOL_MIN;
5500 static const int32_t RAW_TOOL_MAX;
5501 static const int32_t RAW_PRESSURE_MIN;
5502 static const int32_t RAW_PRESSURE_MAX;
5503 static const int32_t RAW_ORIENTATION_MIN;
5504 static const int32_t RAW_ORIENTATION_MAX;
5505 static const int32_t RAW_DISTANCE_MIN;
5506 static const int32_t RAW_DISTANCE_MAX;
5507 static const int32_t RAW_TILT_MIN;
5508 static const int32_t RAW_TILT_MAX;
5509 static const int32_t RAW_ID_MIN;
5510 static const int32_t RAW_ID_MAX;
5511 static const int32_t RAW_SLOT_MIN;
5512 static const int32_t RAW_SLOT_MAX;
5513 static const float X_PRECISION;
5514 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005515 static const float X_PRECISION_VIRTUAL;
5516 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005517
5518 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005519 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005520
5521 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5522
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005523 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005524 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005525
Michael Wrightd02c5b62014-02-10 15:10:22 -08005526 enum Axes {
5527 POSITION = 1 << 0,
5528 TOUCH = 1 << 1,
5529 TOOL = 1 << 2,
5530 PRESSURE = 1 << 3,
5531 ORIENTATION = 1 << 4,
5532 MINOR = 1 << 5,
5533 ID = 1 << 6,
5534 DISTANCE = 1 << 7,
5535 TILT = 1 << 8,
5536 SLOT = 1 << 9,
5537 TOOL_TYPE = 1 << 10,
5538 };
5539
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005540 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5541 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005542 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005543 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005544 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005545 int32_t toRawX(float displayX);
5546 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005547 int32_t toRotatedRawX(float displayX);
5548 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005549 float toCookedX(float rawX, float rawY);
5550 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005551 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005552 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005553 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005554 float toDisplayY(int32_t rawY, int32_t displayHeight);
5555
Michael Wrightd02c5b62014-02-10 15:10:22 -08005556};
5557
5558const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5559const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5560const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5561const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5562const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5563const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5564const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5565const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005566const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5567const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005568const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5569const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5570const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5571const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5572const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5573const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5574const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5575const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5576const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5577const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5578const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5579const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005580const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5581 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5582const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5583 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005584const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5585 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005586
5587const float TouchInputMapperTest::GEOMETRIC_SCALE =
5588 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5589 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5590
5591const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5592 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5593 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5594};
5595
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005596void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005597 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5598 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005599}
5600
5601void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5602 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5603 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005604}
5605
Santos Cordonfa5cf462017-04-05 10:37:00 -07005606void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005607 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5608 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5609 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005610}
5611
Michael Wrightd02c5b62014-02-10 15:10:22 -08005612void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005613 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5614 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5615 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5616 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005617}
5618
Jason Gerecke489fda82012-09-07 17:19:40 -07005619void TouchInputMapperTest::prepareLocationCalibration() {
5620 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5621}
5622
Michael Wrightd02c5b62014-02-10 15:10:22 -08005623int32_t TouchInputMapperTest::toRawX(float displayX) {
5624 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5625}
5626
5627int32_t TouchInputMapperTest::toRawY(float displayY) {
5628 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5629}
5630
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005631int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5632 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5633}
5634
5635int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5636 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5637}
5638
Jason Gerecke489fda82012-09-07 17:19:40 -07005639float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5640 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5641 return rawX;
5642}
5643
5644float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5645 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5646 return rawY;
5647}
5648
Michael Wrightd02c5b62014-02-10 15:10:22 -08005649float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005650 return toDisplayX(rawX, DISPLAY_WIDTH);
5651}
5652
5653float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5654 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005655}
5656
5657float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005658 return toDisplayY(rawY, DISPLAY_HEIGHT);
5659}
5660
5661float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5662 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005663}
5664
5665
5666// --- SingleTouchInputMapperTest ---
5667
5668class SingleTouchInputMapperTest : public TouchInputMapperTest {
5669protected:
5670 void prepareButtons();
5671 void prepareAxes(int axes);
5672
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005673 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5674 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5675 void processUp(SingleTouchInputMapper& mappery);
5676 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5677 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5678 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5679 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5680 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5681 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005682};
5683
5684void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005685 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005686}
5687
5688void SingleTouchInputMapperTest::prepareAxes(int axes) {
5689 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005690 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5691 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005692 }
5693 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005694 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5695 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005696 }
5697 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005698 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5699 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005700 }
5701 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005702 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5703 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005704 }
5705 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005706 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5707 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005708 }
5709}
5710
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005711void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005715}
5716
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005717void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005718 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5719 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005720}
5721
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005722void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005723 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724}
5725
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005726void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005727 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005728}
5729
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005730void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5731 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005732 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005733}
5734
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005735void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005736 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005737}
5738
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005739void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5740 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005741 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5742 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005743}
5744
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005745void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5746 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005748}
5749
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005750void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005751 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005752}
5753
Michael Wrightd02c5b62014-02-10 15:10:22 -08005754TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005755 prepareButtons();
5756 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005757 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005758
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005759 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005760}
5761
Michael Wrightd02c5b62014-02-10 15:10:22 -08005762TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005763 prepareButtons();
5764 prepareAxes(POSITION);
5765 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005766 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005767
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005768 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005769}
5770
5771TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005772 addConfigurationProperty("touch.deviceType", "touchScreen");
5773 prepareDisplay(DISPLAY_ORIENTATION_0);
5774 prepareButtons();
5775 prepareAxes(POSITION);
5776 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005777 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005778
5779 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005780 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005781
5782 // Virtual key is down.
5783 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5784 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5785 processDown(mapper, x, y);
5786 processSync(mapper);
5787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5788
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005789 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005790
5791 // Virtual key is up.
5792 processUp(mapper);
5793 processSync(mapper);
5794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5795
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005796 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005797}
5798
5799TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005800 addConfigurationProperty("touch.deviceType", "touchScreen");
5801 prepareDisplay(DISPLAY_ORIENTATION_0);
5802 prepareButtons();
5803 prepareAxes(POSITION);
5804 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005805 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005806
5807 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005808 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005809
5810 // Virtual key is down.
5811 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5812 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5813 processDown(mapper, x, y);
5814 processSync(mapper);
5815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5816
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005817 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005818
5819 // Virtual key is up.
5820 processUp(mapper);
5821 processSync(mapper);
5822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5823
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005824 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005825}
5826
5827TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005828 addConfigurationProperty("touch.deviceType", "touchScreen");
5829 prepareDisplay(DISPLAY_ORIENTATION_0);
5830 prepareButtons();
5831 prepareAxes(POSITION);
5832 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005833 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005834
Michael Wrightd02c5b62014-02-10 15:10:22 -08005835 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005836 ASSERT_TRUE(
5837 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005838 ASSERT_TRUE(flags[0]);
5839 ASSERT_FALSE(flags[1]);
5840}
5841
5842TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005843 addConfigurationProperty("touch.deviceType", "touchScreen");
5844 prepareDisplay(DISPLAY_ORIENTATION_0);
5845 prepareButtons();
5846 prepareAxes(POSITION);
5847 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005848 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005849
arthurhungdcef2dc2020-08-11 14:47:50 +08005850 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005851
5852 NotifyKeyArgs args;
5853
5854 // Press virtual key.
5855 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5856 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5857 processDown(mapper, x, y);
5858 processSync(mapper);
5859
5860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5861 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5862 ASSERT_EQ(DEVICE_ID, args.deviceId);
5863 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5864 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5865 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5866 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5867 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5868 ASSERT_EQ(KEY_HOME, args.scanCode);
5869 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5870 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5871
5872 // Release virtual key.
5873 processUp(mapper);
5874 processSync(mapper);
5875
5876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5877 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5878 ASSERT_EQ(DEVICE_ID, args.deviceId);
5879 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5880 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5881 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5882 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5883 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5884 ASSERT_EQ(KEY_HOME, args.scanCode);
5885 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5886 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5887
5888 // Should not have sent any motions.
5889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5890}
5891
5892TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005893 addConfigurationProperty("touch.deviceType", "touchScreen");
5894 prepareDisplay(DISPLAY_ORIENTATION_0);
5895 prepareButtons();
5896 prepareAxes(POSITION);
5897 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005898 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005899
arthurhungdcef2dc2020-08-11 14:47:50 +08005900 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005901
5902 NotifyKeyArgs keyArgs;
5903
5904 // Press virtual key.
5905 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5906 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5907 processDown(mapper, x, y);
5908 processSync(mapper);
5909
5910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5911 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5912 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5913 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5914 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5915 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5916 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5917 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5918 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5919 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5920 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5921
5922 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5923 // into the display area.
5924 y -= 100;
5925 processMove(mapper, x, y);
5926 processSync(mapper);
5927
5928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5929 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5930 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5931 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5932 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5933 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5934 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5935 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5936 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5937 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5938 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5939 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5940
5941 NotifyMotionArgs motionArgs;
5942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5943 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5944 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5945 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5946 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5947 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5948 ASSERT_EQ(0, motionArgs.flags);
5949 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5950 ASSERT_EQ(0, motionArgs.buttonState);
5951 ASSERT_EQ(0, motionArgs.edgeFlags);
5952 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5953 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5954 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5955 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5956 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5957 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5958 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5959 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5960
5961 // Keep moving out of bounds. Should generate a pointer move.
5962 y -= 50;
5963 processMove(mapper, x, y);
5964 processSync(mapper);
5965
5966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5967 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5968 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5969 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5970 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5971 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5972 ASSERT_EQ(0, motionArgs.flags);
5973 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5974 ASSERT_EQ(0, motionArgs.buttonState);
5975 ASSERT_EQ(0, motionArgs.edgeFlags);
5976 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5977 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5978 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5979 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5980 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5981 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5982 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5983 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5984
5985 // Release out of bounds. Should generate a pointer up.
5986 processUp(mapper);
5987 processSync(mapper);
5988
5989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5990 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5991 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5992 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5993 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5994 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5995 ASSERT_EQ(0, motionArgs.flags);
5996 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5997 ASSERT_EQ(0, motionArgs.buttonState);
5998 ASSERT_EQ(0, motionArgs.edgeFlags);
5999 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6000 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6001 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6003 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6004 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6005 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6006 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6007
6008 // Should not have sent any more keys or motions.
6009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6011}
6012
6013TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006014 addConfigurationProperty("touch.deviceType", "touchScreen");
6015 prepareDisplay(DISPLAY_ORIENTATION_0);
6016 prepareButtons();
6017 prepareAxes(POSITION);
6018 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006019 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006020
arthurhungdcef2dc2020-08-11 14:47:50 +08006021 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006022
6023 NotifyMotionArgs motionArgs;
6024
6025 // Initially go down out of bounds.
6026 int32_t x = -10;
6027 int32_t y = -10;
6028 processDown(mapper, x, y);
6029 processSync(mapper);
6030
6031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6032
6033 // Move into the display area. Should generate a pointer down.
6034 x = 50;
6035 y = 75;
6036 processMove(mapper, x, y);
6037 processSync(mapper);
6038
6039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6040 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6041 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6042 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6043 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6044 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6045 ASSERT_EQ(0, motionArgs.flags);
6046 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6047 ASSERT_EQ(0, motionArgs.buttonState);
6048 ASSERT_EQ(0, motionArgs.edgeFlags);
6049 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6050 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6053 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6054 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6055 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6056 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6057
6058 // Release. Should generate a pointer up.
6059 processUp(mapper);
6060 processSync(mapper);
6061
6062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6063 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6064 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6065 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6066 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6067 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6068 ASSERT_EQ(0, motionArgs.flags);
6069 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6070 ASSERT_EQ(0, motionArgs.buttonState);
6071 ASSERT_EQ(0, motionArgs.edgeFlags);
6072 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6073 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6074 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6075 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6076 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6077 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6078 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6079 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6080
6081 // Should not have sent any more keys or motions.
6082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6084}
6085
Santos Cordonfa5cf462017-04-05 10:37:00 -07006086TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07006087 addConfigurationProperty("touch.deviceType", "touchScreen");
6088 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
6089
6090 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
6091 prepareButtons();
6092 prepareAxes(POSITION);
6093 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006094 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07006095
arthurhungdcef2dc2020-08-11 14:47:50 +08006096 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006097
6098 NotifyMotionArgs motionArgs;
6099
6100 // Down.
6101 int32_t x = 100;
6102 int32_t y = 125;
6103 processDown(mapper, x, y);
6104 processSync(mapper);
6105
6106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6107 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6108 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6109 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6110 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6111 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6112 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6113 ASSERT_EQ(0, motionArgs.flags);
6114 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6115 ASSERT_EQ(0, motionArgs.buttonState);
6116 ASSERT_EQ(0, motionArgs.edgeFlags);
6117 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6118 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6119 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6121 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6122 1, 0, 0, 0, 0, 0, 0, 0));
6123 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6124 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6125 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6126
6127 // Move.
6128 x += 50;
6129 y += 75;
6130 processMove(mapper, x, y);
6131 processSync(mapper);
6132
6133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6134 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6135 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6136 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6137 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6138 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6140 ASSERT_EQ(0, motionArgs.flags);
6141 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6142 ASSERT_EQ(0, motionArgs.buttonState);
6143 ASSERT_EQ(0, motionArgs.edgeFlags);
6144 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6145 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6146 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6147 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6148 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6149 1, 0, 0, 0, 0, 0, 0, 0));
6150 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6151 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6152 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6153
6154 // Up.
6155 processUp(mapper);
6156 processSync(mapper);
6157
6158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6159 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6160 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6161 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6162 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6163 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6164 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6165 ASSERT_EQ(0, motionArgs.flags);
6166 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6167 ASSERT_EQ(0, motionArgs.buttonState);
6168 ASSERT_EQ(0, motionArgs.edgeFlags);
6169 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6170 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6171 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6172 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6173 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6174 1, 0, 0, 0, 0, 0, 0, 0));
6175 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6176 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6177 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6178
6179 // Should not have sent any more keys or motions.
6180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6182}
6183
Michael Wrightd02c5b62014-02-10 15:10:22 -08006184TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006185 addConfigurationProperty("touch.deviceType", "touchScreen");
6186 prepareDisplay(DISPLAY_ORIENTATION_0);
6187 prepareButtons();
6188 prepareAxes(POSITION);
6189 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006190 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006191
arthurhungdcef2dc2020-08-11 14:47:50 +08006192 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006193
6194 NotifyMotionArgs motionArgs;
6195
6196 // Down.
6197 int32_t x = 100;
6198 int32_t y = 125;
6199 processDown(mapper, x, y);
6200 processSync(mapper);
6201
6202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6203 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6204 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6205 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6206 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6207 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6208 ASSERT_EQ(0, motionArgs.flags);
6209 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6210 ASSERT_EQ(0, motionArgs.buttonState);
6211 ASSERT_EQ(0, motionArgs.edgeFlags);
6212 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6213 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6214 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6215 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6216 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6217 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6218 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6219 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6220
6221 // Move.
6222 x += 50;
6223 y += 75;
6224 processMove(mapper, x, y);
6225 processSync(mapper);
6226
6227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6228 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6229 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6230 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6231 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6233 ASSERT_EQ(0, motionArgs.flags);
6234 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6235 ASSERT_EQ(0, motionArgs.buttonState);
6236 ASSERT_EQ(0, motionArgs.edgeFlags);
6237 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6238 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6239 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6241 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6242 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6243 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6244 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6245
6246 // Up.
6247 processUp(mapper);
6248 processSync(mapper);
6249
6250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6251 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6252 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6253 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6254 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6255 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6256 ASSERT_EQ(0, motionArgs.flags);
6257 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6258 ASSERT_EQ(0, motionArgs.buttonState);
6259 ASSERT_EQ(0, motionArgs.edgeFlags);
6260 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6261 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6262 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6264 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6265 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6266 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6267 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6268
6269 // Should not have sent any more keys or motions.
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6272}
6273
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006274TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006275 addConfigurationProperty("touch.deviceType", "touchScreen");
6276 prepareButtons();
6277 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006278 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
6279 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006280 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006281
6282 NotifyMotionArgs args;
6283
6284 // Rotation 90.
6285 prepareDisplay(DISPLAY_ORIENTATION_90);
6286 processDown(mapper, toRawX(50), toRawY(75));
6287 processSync(mapper);
6288
6289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6290 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6291 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6292
6293 processUp(mapper);
6294 processSync(mapper);
6295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6296}
6297
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006298TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006299 addConfigurationProperty("touch.deviceType", "touchScreen");
6300 prepareButtons();
6301 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006302 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6303 // orientation-aware are affected by display rotation.
6304 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006305 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006306
6307 NotifyMotionArgs args;
6308
6309 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006310 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006311 prepareDisplay(DISPLAY_ORIENTATION_0);
6312 processDown(mapper, toRawX(50), toRawY(75));
6313 processSync(mapper);
6314
6315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6316 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6317 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6318
6319 processUp(mapper);
6320 processSync(mapper);
6321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6322
6323 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006324 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006325 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006326 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006327 processSync(mapper);
6328
6329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6330 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6331 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6332
6333 processUp(mapper);
6334 processSync(mapper);
6335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6336
6337 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006338 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006339 prepareDisplay(DISPLAY_ORIENTATION_180);
6340 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6341 processSync(mapper);
6342
6343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6344 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6345 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6346
6347 processUp(mapper);
6348 processSync(mapper);
6349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6350
6351 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006352 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006353 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006354 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006355 processSync(mapper);
6356
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6358 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6359 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6360
6361 processUp(mapper);
6362 processSync(mapper);
6363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6364}
6365
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006366TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6367 addConfigurationProperty("touch.deviceType", "touchScreen");
6368 prepareButtons();
6369 prepareAxes(POSITION);
6370 addConfigurationProperty("touch.orientationAware", "1");
6371 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6372 clearViewports();
6373 prepareDisplay(DISPLAY_ORIENTATION_0);
6374 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6375 NotifyMotionArgs args;
6376
6377 // Orientation 0.
6378 processDown(mapper, toRawX(50), toRawY(75));
6379 processSync(mapper);
6380
6381 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6382 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6383 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6384
6385 processUp(mapper);
6386 processSync(mapper);
6387 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6388}
6389
6390TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6391 addConfigurationProperty("touch.deviceType", "touchScreen");
6392 prepareButtons();
6393 prepareAxes(POSITION);
6394 addConfigurationProperty("touch.orientationAware", "1");
6395 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6396 clearViewports();
6397 prepareDisplay(DISPLAY_ORIENTATION_0);
6398 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6399 NotifyMotionArgs args;
6400
6401 // Orientation 90.
6402 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6403 processSync(mapper);
6404
6405 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6406 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6407 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6408
6409 processUp(mapper);
6410 processSync(mapper);
6411 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6412}
6413
6414TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6415 addConfigurationProperty("touch.deviceType", "touchScreen");
6416 prepareButtons();
6417 prepareAxes(POSITION);
6418 addConfigurationProperty("touch.orientationAware", "1");
6419 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6420 clearViewports();
6421 prepareDisplay(DISPLAY_ORIENTATION_0);
6422 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6423 NotifyMotionArgs args;
6424
6425 // Orientation 180.
6426 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6427 processSync(mapper);
6428
6429 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6430 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6431 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6432
6433 processUp(mapper);
6434 processSync(mapper);
6435 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6436}
6437
6438TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6439 addConfigurationProperty("touch.deviceType", "touchScreen");
6440 prepareButtons();
6441 prepareAxes(POSITION);
6442 addConfigurationProperty("touch.orientationAware", "1");
6443 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6444 clearViewports();
6445 prepareDisplay(DISPLAY_ORIENTATION_0);
6446 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6447 NotifyMotionArgs args;
6448
6449 // Orientation 270.
6450 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6451 processSync(mapper);
6452
6453 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6454 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6455 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6456
6457 processUp(mapper);
6458 processSync(mapper);
6459 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6460}
6461
6462TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6463 addConfigurationProperty("touch.deviceType", "touchScreen");
6464 prepareButtons();
6465 prepareAxes(POSITION);
6466 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6467 // orientation-aware are affected by display rotation.
6468 addConfigurationProperty("touch.orientationAware", "0");
6469 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6470 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6471
6472 NotifyMotionArgs args;
6473
6474 // Orientation 90, Rotation 0.
6475 clearViewports();
6476 prepareDisplay(DISPLAY_ORIENTATION_0);
6477 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6478 processSync(mapper);
6479
6480 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6481 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6482 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6483
6484 processUp(mapper);
6485 processSync(mapper);
6486 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6487
6488 // Orientation 90, Rotation 90.
6489 clearViewports();
6490 prepareDisplay(DISPLAY_ORIENTATION_90);
6491 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6492 processSync(mapper);
6493
6494 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6495 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6496 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6497
6498 processUp(mapper);
6499 processSync(mapper);
6500 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6501
6502 // Orientation 90, Rotation 180.
6503 clearViewports();
6504 prepareDisplay(DISPLAY_ORIENTATION_180);
6505 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6506 processSync(mapper);
6507
6508 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6509 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6510 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6511
6512 processUp(mapper);
6513 processSync(mapper);
6514 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6515
6516 // Orientation 90, Rotation 270.
6517 clearViewports();
6518 prepareDisplay(DISPLAY_ORIENTATION_270);
6519 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6520 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6521 processSync(mapper);
6522
6523 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6524 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6525 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6526
6527 processUp(mapper);
6528 processSync(mapper);
6529 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6530}
6531
Michael Wrightd02c5b62014-02-10 15:10:22 -08006532TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006533 addConfigurationProperty("touch.deviceType", "touchScreen");
6534 prepareDisplay(DISPLAY_ORIENTATION_0);
6535 prepareButtons();
6536 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006537 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006538
6539 // These calculations are based on the input device calibration documentation.
6540 int32_t rawX = 100;
6541 int32_t rawY = 200;
6542 int32_t rawPressure = 10;
6543 int32_t rawToolMajor = 12;
6544 int32_t rawDistance = 2;
6545 int32_t rawTiltX = 30;
6546 int32_t rawTiltY = 110;
6547
6548 float x = toDisplayX(rawX);
6549 float y = toDisplayY(rawY);
6550 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6551 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6552 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6553 float distance = float(rawDistance);
6554
6555 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6556 float tiltScale = M_PI / 180;
6557 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6558 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6559 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6560 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6561
6562 processDown(mapper, rawX, rawY);
6563 processPressure(mapper, rawPressure);
6564 processToolMajor(mapper, rawToolMajor);
6565 processDistance(mapper, rawDistance);
6566 processTilt(mapper, rawTiltX, rawTiltY);
6567 processSync(mapper);
6568
6569 NotifyMotionArgs args;
6570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6572 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6573 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6574}
6575
Jason Gerecke489fda82012-09-07 17:19:40 -07006576TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006577 addConfigurationProperty("touch.deviceType", "touchScreen");
6578 prepareDisplay(DISPLAY_ORIENTATION_0);
6579 prepareLocationCalibration();
6580 prepareButtons();
6581 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006582 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006583
6584 int32_t rawX = 100;
6585 int32_t rawY = 200;
6586
6587 float x = toDisplayX(toCookedX(rawX, rawY));
6588 float y = toDisplayY(toCookedY(rawX, rawY));
6589
6590 processDown(mapper, rawX, rawY);
6591 processSync(mapper);
6592
6593 NotifyMotionArgs args;
6594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6596 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6597}
6598
Michael Wrightd02c5b62014-02-10 15:10:22 -08006599TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006600 addConfigurationProperty("touch.deviceType", "touchScreen");
6601 prepareDisplay(DISPLAY_ORIENTATION_0);
6602 prepareButtons();
6603 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006604 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006605
6606 NotifyMotionArgs motionArgs;
6607 NotifyKeyArgs keyArgs;
6608
6609 processDown(mapper, 100, 200);
6610 processSync(mapper);
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6612 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6613 ASSERT_EQ(0, motionArgs.buttonState);
6614
6615 // press BTN_LEFT, release BTN_LEFT
6616 processKey(mapper, BTN_LEFT, 1);
6617 processSync(mapper);
6618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6619 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6620 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6621
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6623 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6624 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6625
Michael Wrightd02c5b62014-02-10 15:10:22 -08006626 processKey(mapper, BTN_LEFT, 0);
6627 processSync(mapper);
6628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006629 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006630 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006631
6632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006633 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006634 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006635
6636 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6637 processKey(mapper, BTN_RIGHT, 1);
6638 processKey(mapper, BTN_MIDDLE, 1);
6639 processSync(mapper);
6640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6641 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6642 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6643 motionArgs.buttonState);
6644
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6646 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6647 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6648
6649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6650 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6651 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6652 motionArgs.buttonState);
6653
Michael Wrightd02c5b62014-02-10 15:10:22 -08006654 processKey(mapper, BTN_RIGHT, 0);
6655 processSync(mapper);
6656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006657 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006658 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006659
6660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006661 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006662 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006663
6664 processKey(mapper, BTN_MIDDLE, 0);
6665 processSync(mapper);
6666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006667 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006668 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006669
6670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006671 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006672 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006673
6674 // press BTN_BACK, release BTN_BACK
6675 processKey(mapper, BTN_BACK, 1);
6676 processSync(mapper);
6677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6678 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6679 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006680
Michael Wrightd02c5b62014-02-10 15:10:22 -08006681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006682 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006683 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6684
6685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6686 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6687 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006688
6689 processKey(mapper, BTN_BACK, 0);
6690 processSync(mapper);
6691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006692 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006693 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006694
6695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006697 ASSERT_EQ(0, motionArgs.buttonState);
6698
Michael Wrightd02c5b62014-02-10 15:10:22 -08006699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6700 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6701 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6702
6703 // press BTN_SIDE, release BTN_SIDE
6704 processKey(mapper, BTN_SIDE, 1);
6705 processSync(mapper);
6706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6707 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6708 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006709
Michael Wrightd02c5b62014-02-10 15:10:22 -08006710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006711 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006712 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6713
6714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6715 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6716 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006717
6718 processKey(mapper, BTN_SIDE, 0);
6719 processSync(mapper);
6720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006721 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006722 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006723
6724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006726 ASSERT_EQ(0, motionArgs.buttonState);
6727
Michael Wrightd02c5b62014-02-10 15:10:22 -08006728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6729 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6730 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6731
6732 // press BTN_FORWARD, release BTN_FORWARD
6733 processKey(mapper, BTN_FORWARD, 1);
6734 processSync(mapper);
6735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6736 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6737 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006738
Michael Wrightd02c5b62014-02-10 15:10:22 -08006739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006740 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006741 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6742
6743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6744 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6745 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006746
6747 processKey(mapper, BTN_FORWARD, 0);
6748 processSync(mapper);
6749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006750 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006751 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006752
6753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006754 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006755 ASSERT_EQ(0, motionArgs.buttonState);
6756
Michael Wrightd02c5b62014-02-10 15:10:22 -08006757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6758 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6759 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6760
6761 // press BTN_EXTRA, release BTN_EXTRA
6762 processKey(mapper, BTN_EXTRA, 1);
6763 processSync(mapper);
6764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6765 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6766 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006767
Michael Wrightd02c5b62014-02-10 15:10:22 -08006768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006769 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006770 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6771
6772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6773 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6774 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006775
6776 processKey(mapper, BTN_EXTRA, 0);
6777 processSync(mapper);
6778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006779 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006780 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006781
6782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006783 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006784 ASSERT_EQ(0, motionArgs.buttonState);
6785
Michael Wrightd02c5b62014-02-10 15:10:22 -08006786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6787 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6788 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6789
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6791
Michael Wrightd02c5b62014-02-10 15:10:22 -08006792 // press BTN_STYLUS, release BTN_STYLUS
6793 processKey(mapper, BTN_STYLUS, 1);
6794 processSync(mapper);
6795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6796 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006797 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6798
6799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6800 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6801 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006802
6803 processKey(mapper, BTN_STYLUS, 0);
6804 processSync(mapper);
6805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006806 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006807 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006808
6809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006811 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006812
6813 // press BTN_STYLUS2, release BTN_STYLUS2
6814 processKey(mapper, BTN_STYLUS2, 1);
6815 processSync(mapper);
6816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6817 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006818 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6819
6820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6821 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6822 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006823
6824 processKey(mapper, BTN_STYLUS2, 0);
6825 processSync(mapper);
6826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006827 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006828 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006829
6830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006832 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006833
6834 // release touch
6835 processUp(mapper);
6836 processSync(mapper);
6837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6838 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6839 ASSERT_EQ(0, motionArgs.buttonState);
6840}
6841
6842TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006843 addConfigurationProperty("touch.deviceType", "touchScreen");
6844 prepareDisplay(DISPLAY_ORIENTATION_0);
6845 prepareButtons();
6846 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006847 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006848
6849 NotifyMotionArgs motionArgs;
6850
6851 // default tool type is finger
6852 processDown(mapper, 100, 200);
6853 processSync(mapper);
6854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6855 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6856 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6857
6858 // eraser
6859 processKey(mapper, BTN_TOOL_RUBBER, 1);
6860 processSync(mapper);
6861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6863 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6864
6865 // stylus
6866 processKey(mapper, BTN_TOOL_RUBBER, 0);
6867 processKey(mapper, BTN_TOOL_PEN, 1);
6868 processSync(mapper);
6869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6870 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6871 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6872
6873 // brush
6874 processKey(mapper, BTN_TOOL_PEN, 0);
6875 processKey(mapper, BTN_TOOL_BRUSH, 1);
6876 processSync(mapper);
6877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6878 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6879 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6880
6881 // pencil
6882 processKey(mapper, BTN_TOOL_BRUSH, 0);
6883 processKey(mapper, BTN_TOOL_PENCIL, 1);
6884 processSync(mapper);
6885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6886 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6887 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6888
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006889 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006890 processKey(mapper, BTN_TOOL_PENCIL, 0);
6891 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6892 processSync(mapper);
6893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6894 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6895 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6896
6897 // mouse
6898 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6899 processKey(mapper, BTN_TOOL_MOUSE, 1);
6900 processSync(mapper);
6901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6902 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6903 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6904
6905 // lens
6906 processKey(mapper, BTN_TOOL_MOUSE, 0);
6907 processKey(mapper, BTN_TOOL_LENS, 1);
6908 processSync(mapper);
6909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6910 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6911 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6912
6913 // double-tap
6914 processKey(mapper, BTN_TOOL_LENS, 0);
6915 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6916 processSync(mapper);
6917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6918 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6919 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6920
6921 // triple-tap
6922 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6923 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6924 processSync(mapper);
6925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6926 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6927 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6928
6929 // quad-tap
6930 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6931 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6932 processSync(mapper);
6933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6934 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6935 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6936
6937 // finger
6938 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6939 processKey(mapper, BTN_TOOL_FINGER, 1);
6940 processSync(mapper);
6941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6942 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6943 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6944
6945 // stylus trumps finger
6946 processKey(mapper, BTN_TOOL_PEN, 1);
6947 processSync(mapper);
6948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6949 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6950 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6951
6952 // eraser trumps stylus
6953 processKey(mapper, BTN_TOOL_RUBBER, 1);
6954 processSync(mapper);
6955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6956 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6957 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6958
6959 // mouse trumps eraser
6960 processKey(mapper, BTN_TOOL_MOUSE, 1);
6961 processSync(mapper);
6962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6963 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6964 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6965
6966 // back to default tool type
6967 processKey(mapper, BTN_TOOL_MOUSE, 0);
6968 processKey(mapper, BTN_TOOL_RUBBER, 0);
6969 processKey(mapper, BTN_TOOL_PEN, 0);
6970 processKey(mapper, BTN_TOOL_FINGER, 0);
6971 processSync(mapper);
6972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6973 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6974 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6975}
6976
6977TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006978 addConfigurationProperty("touch.deviceType", "touchScreen");
6979 prepareDisplay(DISPLAY_ORIENTATION_0);
6980 prepareButtons();
6981 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006982 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006983 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006984
6985 NotifyMotionArgs motionArgs;
6986
6987 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6988 processKey(mapper, BTN_TOOL_FINGER, 1);
6989 processMove(mapper, 100, 200);
6990 processSync(mapper);
6991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6992 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6994 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6995
6996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6997 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6998 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6999 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7000
7001 // move a little
7002 processMove(mapper, 150, 250);
7003 processSync(mapper);
7004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7005 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7006 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7007 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7008
7009 // down when BTN_TOUCH is pressed, pressure defaults to 1
7010 processKey(mapper, BTN_TOUCH, 1);
7011 processSync(mapper);
7012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7013 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7014 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7015 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7016
7017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7018 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7020 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7021
7022 // up when BTN_TOUCH is released, hover restored
7023 processKey(mapper, BTN_TOUCH, 0);
7024 processSync(mapper);
7025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7026 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7028 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7029
7030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7031 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7033 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7034
7035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7036 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7037 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7038 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7039
7040 // exit hover when pointer goes away
7041 processKey(mapper, BTN_TOOL_FINGER, 0);
7042 processSync(mapper);
7043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7044 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7045 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7046 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7047}
7048
7049TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007050 addConfigurationProperty("touch.deviceType", "touchScreen");
7051 prepareDisplay(DISPLAY_ORIENTATION_0);
7052 prepareButtons();
7053 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007054 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007055
7056 NotifyMotionArgs motionArgs;
7057
7058 // initially hovering because pressure is 0
7059 processDown(mapper, 100, 200);
7060 processPressure(mapper, 0);
7061 processSync(mapper);
7062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7063 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7065 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7066
7067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7068 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7069 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7070 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7071
7072 // move a little
7073 processMove(mapper, 150, 250);
7074 processSync(mapper);
7075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7076 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7078 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7079
7080 // down when pressure is non-zero
7081 processPressure(mapper, RAW_PRESSURE_MAX);
7082 processSync(mapper);
7083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7084 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7085 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7086 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7087
7088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7089 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7090 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7091 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7092
7093 // up when pressure becomes 0, hover restored
7094 processPressure(mapper, 0);
7095 processSync(mapper);
7096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7097 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7098 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7099 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7100
7101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7102 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7103 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7104 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7105
7106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7107 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7109 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7110
7111 // exit hover when pointer goes away
7112 processUp(mapper);
7113 processSync(mapper);
7114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7115 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7116 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7117 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7118}
7119
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007120TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
7121 addConfigurationProperty("touch.deviceType", "touchScreen");
7122 prepareDisplay(DISPLAY_ORIENTATION_0);
7123 prepareButtons();
7124 prepareAxes(POSITION | PRESSURE);
7125 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7126
7127 // Touch down.
7128 processDown(mapper, 100, 200);
7129 processPressure(mapper, 1);
7130 processSync(mapper);
7131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7132 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
7133
7134 // Reset the mapper. This should cancel the ongoing gesture.
7135 resetMapper(mapper, ARBITRARY_TIME);
7136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7137 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
7138
7139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7140}
7141
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007142TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
7143 addConfigurationProperty("touch.deviceType", "touchScreen");
7144 prepareDisplay(DISPLAY_ORIENTATION_0);
7145 prepareButtons();
7146 prepareAxes(POSITION | PRESSURE);
7147 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7148
7149 // Set the initial state for the touch pointer.
7150 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
7151 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
7152 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
7153 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7154
7155 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007156 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
7157 // does not generate any events.
7158 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007159
7160 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
7161 // the recreated touch state to generate a down event.
7162 processSync(mapper);
7163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7164 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
7165
7166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7167}
7168
lilinnan687e58f2022-07-19 16:00:50 +08007169TEST_F(SingleTouchInputMapperTest,
7170 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
7171 addConfigurationProperty("touch.deviceType", "touchScreen");
7172 prepareDisplay(DISPLAY_ORIENTATION_0);
7173 prepareButtons();
7174 prepareAxes(POSITION);
7175 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7176 NotifyMotionArgs motionArgs;
7177
7178 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00007179 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08007180 processSync(mapper);
7181
7182 // We should receive a down event
7183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7184 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7185
7186 // Change display id
7187 clearViewports();
7188 prepareSecondaryDisplay(ViewportType::INTERNAL);
7189
7190 // We should receive a cancel event
7191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7192 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7193 // Then receive reset called
7194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7195}
7196
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007197TEST_F(SingleTouchInputMapperTest,
7198 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
7199 addConfigurationProperty("touch.deviceType", "touchScreen");
7200 prepareDisplay(DISPLAY_ORIENTATION_0);
7201 prepareButtons();
7202 prepareAxes(POSITION);
7203 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7205 NotifyMotionArgs motionArgs;
7206
7207 // Start a new gesture.
7208 processDown(mapper, 100, 200);
7209 processSync(mapper);
7210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7211 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7212
7213 // Make the viewport inactive. This will put the device in disabled mode.
7214 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7215 viewport->isActive = false;
7216 mFakePolicy->updateViewport(*viewport);
7217 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7218
7219 // We should receive a cancel event for the ongoing gesture.
7220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7221 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7222 // Then we should be notified that the device was reset.
7223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7224
7225 // No events are generated while the viewport is inactive.
7226 processMove(mapper, 101, 201);
7227 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007228 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007229 processSync(mapper);
7230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7231
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007232 // Start a new gesture while the viewport is still inactive.
7233 processDown(mapper, 300, 400);
7234 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
7235 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
7236 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7237 processSync(mapper);
7238
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007239 // Make the viewport active again. The device should resume processing events.
7240 viewport->isActive = true;
7241 mFakePolicy->updateViewport(*viewport);
7242 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7243
7244 // The device is reset because it changes back to direct mode, without generating any events.
7245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7247
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007248 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007249 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7251 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007252
7253 // No more events.
7254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
7256}
7257
Prabir Pradhan211ba622022-10-31 21:09:21 +00007258TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
7259 addConfigurationProperty("touch.deviceType", "touchScreen");
7260 prepareDisplay(DISPLAY_ORIENTATION_0);
7261 prepareButtons();
7262 prepareAxes(POSITION);
7263 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7265
7266 // Press a stylus button.
7267 processKey(mapper, BTN_STYLUS, 1);
7268 processSync(mapper);
7269
7270 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
7271 processDown(mapper, 100, 200);
7272 processSync(mapper);
7273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7274 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7275 WithCoords(toDisplayX(100), toDisplayY(200)),
7276 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7278 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7279 WithCoords(toDisplayX(100), toDisplayY(200)),
7280 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7281
7282 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
7283 // the button has not actually been released, since there will be no pointers through which the
7284 // button state can be reported. The event is generated at the location of the pointer before
7285 // it went up.
7286 processUp(mapper);
7287 processSync(mapper);
7288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7289 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7290 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7292 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7293 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7294}
7295
Prabir Pradhan5632d622021-09-06 07:57:20 -07007296// --- TouchDisplayProjectionTest ---
7297
7298class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7299public:
7300 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7301 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7302 // rotated equivalent of the given un-rotated physical display bounds.
7303 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
7304 uint32_t inverseRotationFlags;
7305 auto width = DISPLAY_WIDTH;
7306 auto height = DISPLAY_HEIGHT;
7307 switch (orientation) {
7308 case DISPLAY_ORIENTATION_90:
7309 inverseRotationFlags = ui::Transform::ROT_270;
7310 std::swap(width, height);
7311 break;
7312 case DISPLAY_ORIENTATION_180:
7313 inverseRotationFlags = ui::Transform::ROT_180;
7314 break;
7315 case DISPLAY_ORIENTATION_270:
7316 inverseRotationFlags = ui::Transform::ROT_90;
7317 std::swap(width, height);
7318 break;
7319 case DISPLAY_ORIENTATION_0:
7320 inverseRotationFlags = ui::Transform::ROT_0;
7321 break;
7322 default:
7323 FAIL() << "Invalid orientation: " << orientation;
7324 }
7325
7326 const ui::Transform rotation(inverseRotationFlags, width, height);
7327 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7328
7329 std::optional<DisplayViewport> internalViewport =
7330 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7331 DisplayViewport& v = *internalViewport;
7332 v.displayId = DISPLAY_ID;
7333 v.orientation = orientation;
7334
7335 v.logicalLeft = 0;
7336 v.logicalTop = 0;
7337 v.logicalRight = 100;
7338 v.logicalBottom = 100;
7339
7340 v.physicalLeft = rotatedPhysicalDisplay.left;
7341 v.physicalTop = rotatedPhysicalDisplay.top;
7342 v.physicalRight = rotatedPhysicalDisplay.right;
7343 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7344
7345 v.deviceWidth = width;
7346 v.deviceHeight = height;
7347
7348 v.isActive = true;
7349 v.uniqueId = UNIQUE_ID;
7350 v.type = ViewportType::INTERNAL;
7351 mFakePolicy->updateViewport(v);
7352 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7353 }
7354
7355 void assertReceivedMove(const Point& point) {
7356 NotifyMotionArgs motionArgs;
7357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7359 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7361 1, 0, 0, 0, 0, 0, 0, 0));
7362 }
7363};
7364
7365TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7366 addConfigurationProperty("touch.deviceType", "touchScreen");
7367 prepareDisplay(DISPLAY_ORIENTATION_0);
7368
7369 prepareButtons();
7370 prepareAxes(POSITION);
7371 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7372
7373 NotifyMotionArgs motionArgs;
7374
7375 // Configure the DisplayViewport such that the logical display maps to a subsection of
7376 // the display panel called the physical display. Here, the physical display is bounded by the
7377 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7378 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7379 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7380 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7381
7382 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7383 DISPLAY_ORIENTATION_270}) {
7384 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7385
7386 // Touches outside the physical display should be ignored, and should not generate any
7387 // events. Ensure touches at the following points that lie outside of the physical display
7388 // area do not generate any events.
7389 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7390 processDown(mapper, toRawX(point.x), toRawY(point.y));
7391 processSync(mapper);
7392 processUp(mapper);
7393 processSync(mapper);
7394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7395 << "Unexpected event generated for touch outside physical display at point: "
7396 << point.x << ", " << point.y;
7397 }
7398 }
7399}
7400
7401TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7402 addConfigurationProperty("touch.deviceType", "touchScreen");
7403 prepareDisplay(DISPLAY_ORIENTATION_0);
7404
7405 prepareButtons();
7406 prepareAxes(POSITION);
7407 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7408
7409 NotifyMotionArgs motionArgs;
7410
7411 // Configure the DisplayViewport such that the logical display maps to a subsection of
7412 // the display panel called the physical display. Here, the physical display is bounded by the
7413 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7414 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7415
7416 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7417 DISPLAY_ORIENTATION_270}) {
7418 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7419
7420 // Touches that start outside the physical display should be ignored until it enters the
7421 // physical display bounds, at which point it should generate a down event. Start a touch at
7422 // the point (5, 100), which is outside the physical display bounds.
7423 static const Point kOutsidePoint{5, 100};
7424 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7425 processSync(mapper);
7426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7427
7428 // Move the touch into the physical display area. This should generate a pointer down.
7429 processMove(mapper, toRawX(11), toRawY(21));
7430 processSync(mapper);
7431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7432 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7433 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7434 ASSERT_NO_FATAL_FAILURE(
7435 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7436
7437 // Move the touch inside the physical display area. This should generate a pointer move.
7438 processMove(mapper, toRawX(69), toRawY(159));
7439 processSync(mapper);
7440 assertReceivedMove({69, 159});
7441
7442 // Move outside the physical display area. Since the pointer is already down, this should
7443 // now continue generating events.
7444 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7445 processSync(mapper);
7446 assertReceivedMove(kOutsidePoint);
7447
7448 // Release. This should generate a pointer up.
7449 processUp(mapper);
7450 processSync(mapper);
7451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7452 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7454 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7455
7456 // Ensure no more events were generated.
7457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7459 }
7460}
7461
Michael Wrightd02c5b62014-02-10 15:10:22 -08007462// --- MultiTouchInputMapperTest ---
7463
7464class MultiTouchInputMapperTest : public TouchInputMapperTest {
7465protected:
7466 void prepareAxes(int axes);
7467
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007468 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7469 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7470 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7471 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7472 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7473 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7474 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7475 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7476 void processId(MultiTouchInputMapper& mapper, int32_t id);
7477 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7478 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7479 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007480 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007481 void processMTSync(MultiTouchInputMapper& mapper);
7482 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007483};
7484
7485void MultiTouchInputMapperTest::prepareAxes(int axes) {
7486 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007487 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7488 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007489 }
7490 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007491 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7492 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007493 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007494 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7495 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007496 }
7497 }
7498 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007499 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7500 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007501 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007502 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007503 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007504 }
7505 }
7506 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007507 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7508 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007509 }
7510 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007511 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7512 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007513 }
7514 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007515 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7516 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007517 }
7518 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007519 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7520 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007521 }
7522 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007523 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7524 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007525 }
7526 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007527 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007528 }
7529}
7530
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007531void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7532 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007533 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7534 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007535}
7536
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007537void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7538 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007539 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007540}
7541
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007542void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7543 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007545}
7546
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007547void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007549}
7550
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007551void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007552 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007553}
7554
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007555void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7556 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007557 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007558}
7559
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007560void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007561 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007562}
7563
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007564void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007565 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007566}
7567
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007568void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007570}
7571
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007572void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007573 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007574}
7575
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007576void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007578}
7579
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007580void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7581 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007582 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007583}
7584
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007585void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7586 int32_t value) {
7587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7588 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7589}
7590
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007591void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007593}
7594
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007595void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007596 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007597}
7598
Michael Wrightd02c5b62014-02-10 15:10:22 -08007599TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007600 addConfigurationProperty("touch.deviceType", "touchScreen");
7601 prepareDisplay(DISPLAY_ORIENTATION_0);
7602 prepareAxes(POSITION);
7603 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007604 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007605
arthurhungdcef2dc2020-08-11 14:47:50 +08007606 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007607
7608 NotifyMotionArgs motionArgs;
7609
7610 // Two fingers down at once.
7611 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7612 processPosition(mapper, x1, y1);
7613 processMTSync(mapper);
7614 processPosition(mapper, x2, y2);
7615 processMTSync(mapper);
7616 processSync(mapper);
7617
7618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7619 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7620 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7621 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7622 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7623 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7624 ASSERT_EQ(0, motionArgs.flags);
7625 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7626 ASSERT_EQ(0, motionArgs.buttonState);
7627 ASSERT_EQ(0, motionArgs.edgeFlags);
7628 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7629 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7630 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7632 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7633 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7634 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7635 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7636
7637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7638 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7639 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7640 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7641 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007642 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007643 ASSERT_EQ(0, motionArgs.flags);
7644 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7645 ASSERT_EQ(0, motionArgs.buttonState);
7646 ASSERT_EQ(0, motionArgs.edgeFlags);
7647 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7648 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7649 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7650 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7651 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7652 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7653 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7655 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7656 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7657 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7658 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7659
7660 // Move.
7661 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7662 processPosition(mapper, x1, y1);
7663 processMTSync(mapper);
7664 processPosition(mapper, x2, y2);
7665 processMTSync(mapper);
7666 processSync(mapper);
7667
7668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7669 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7670 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7671 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7672 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7673 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7674 ASSERT_EQ(0, motionArgs.flags);
7675 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7676 ASSERT_EQ(0, motionArgs.buttonState);
7677 ASSERT_EQ(0, motionArgs.edgeFlags);
7678 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7679 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7680 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7681 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7682 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7683 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7684 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7686 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7687 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7688 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7689 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7690
7691 // First finger up.
7692 x2 += 15; y2 -= 20;
7693 processPosition(mapper, x2, y2);
7694 processMTSync(mapper);
7695 processSync(mapper);
7696
7697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7698 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7699 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7700 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7701 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007702 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007703 ASSERT_EQ(0, motionArgs.flags);
7704 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7705 ASSERT_EQ(0, motionArgs.buttonState);
7706 ASSERT_EQ(0, motionArgs.edgeFlags);
7707 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7708 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7709 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7710 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7711 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7712 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7713 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7714 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7715 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7716 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7717 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7718 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7719
7720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7721 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7722 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7723 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7724 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7726 ASSERT_EQ(0, motionArgs.flags);
7727 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7728 ASSERT_EQ(0, motionArgs.buttonState);
7729 ASSERT_EQ(0, motionArgs.edgeFlags);
7730 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7731 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7732 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7734 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7735 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7736 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7737 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7738
7739 // Move.
7740 x2 += 20; y2 -= 25;
7741 processPosition(mapper, x2, y2);
7742 processMTSync(mapper);
7743 processSync(mapper);
7744
7745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7746 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7747 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7748 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7749 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7750 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7751 ASSERT_EQ(0, motionArgs.flags);
7752 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7753 ASSERT_EQ(0, motionArgs.buttonState);
7754 ASSERT_EQ(0, motionArgs.edgeFlags);
7755 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7756 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7757 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7759 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7760 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7761 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7762 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7763
7764 // New finger down.
7765 int32_t x3 = 700, y3 = 300;
7766 processPosition(mapper, x2, y2);
7767 processMTSync(mapper);
7768 processPosition(mapper, x3, y3);
7769 processMTSync(mapper);
7770 processSync(mapper);
7771
7772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7773 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7774 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7775 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7776 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007777 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007778 ASSERT_EQ(0, motionArgs.flags);
7779 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7780 ASSERT_EQ(0, motionArgs.buttonState);
7781 ASSERT_EQ(0, motionArgs.edgeFlags);
7782 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7783 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7784 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7785 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7786 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7788 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7790 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7791 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7792 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7793 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7794
7795 // Second finger up.
7796 x3 += 30; y3 -= 20;
7797 processPosition(mapper, x3, y3);
7798 processMTSync(mapper);
7799 processSync(mapper);
7800
7801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7802 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7803 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7804 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7805 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007806 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007807 ASSERT_EQ(0, motionArgs.flags);
7808 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7809 ASSERT_EQ(0, motionArgs.buttonState);
7810 ASSERT_EQ(0, motionArgs.edgeFlags);
7811 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7812 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7813 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7814 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7815 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7817 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7818 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7819 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7820 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7821 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7822 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7823
7824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7825 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7826 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7827 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7828 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7829 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7830 ASSERT_EQ(0, motionArgs.flags);
7831 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7832 ASSERT_EQ(0, motionArgs.buttonState);
7833 ASSERT_EQ(0, motionArgs.edgeFlags);
7834 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7835 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7836 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7837 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7838 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7839 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7840 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7841 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7842
7843 // Last finger up.
7844 processMTSync(mapper);
7845 processSync(mapper);
7846
7847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7848 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7849 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7850 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7851 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7852 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7853 ASSERT_EQ(0, motionArgs.flags);
7854 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7855 ASSERT_EQ(0, motionArgs.buttonState);
7856 ASSERT_EQ(0, motionArgs.edgeFlags);
7857 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7858 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7859 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7860 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7861 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7862 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7863 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7864 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7865
7866 // Should not have sent any more keys or motions.
7867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7869}
7870
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007871TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7872 addConfigurationProperty("touch.deviceType", "touchScreen");
7873 prepareDisplay(DISPLAY_ORIENTATION_0);
7874
7875 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7876 /*fuzz*/ 0, /*resolution*/ 10);
7877 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7878 /*fuzz*/ 0, /*resolution*/ 11);
7879 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7880 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7881 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7882 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7883 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7884 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7885 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7886 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7887
7888 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7889
7890 // X and Y axes
7891 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7892 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7893 // Touch major and minor
7894 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7895 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7896 // Tool major and minor
7897 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7898 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7899}
7900
7901TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7902 addConfigurationProperty("touch.deviceType", "touchScreen");
7903 prepareDisplay(DISPLAY_ORIENTATION_0);
7904
7905 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7906 /*fuzz*/ 0, /*resolution*/ 10);
7907 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7908 /*fuzz*/ 0, /*resolution*/ 11);
7909
7910 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7911
7912 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7913
7914 // Touch major and minor
7915 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7916 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7917 // Tool major and minor
7918 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7919 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7920}
7921
Michael Wrightd02c5b62014-02-10 15:10:22 -08007922TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007923 addConfigurationProperty("touch.deviceType", "touchScreen");
7924 prepareDisplay(DISPLAY_ORIENTATION_0);
7925 prepareAxes(POSITION | ID);
7926 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007927 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007928
arthurhungdcef2dc2020-08-11 14:47:50 +08007929 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007930
7931 NotifyMotionArgs motionArgs;
7932
7933 // Two fingers down at once.
7934 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7935 processPosition(mapper, x1, y1);
7936 processId(mapper, 1);
7937 processMTSync(mapper);
7938 processPosition(mapper, x2, y2);
7939 processId(mapper, 2);
7940 processMTSync(mapper);
7941 processSync(mapper);
7942
7943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7944 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7945 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7946 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7947 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7949 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7950
7951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007952 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007953 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7954 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7955 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7956 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7957 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7959 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7960 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7961 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7962
7963 // Move.
7964 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7965 processPosition(mapper, x1, y1);
7966 processId(mapper, 1);
7967 processMTSync(mapper);
7968 processPosition(mapper, x2, y2);
7969 processId(mapper, 2);
7970 processMTSync(mapper);
7971 processSync(mapper);
7972
7973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7974 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7975 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7976 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7977 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7978 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7979 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7981 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7983 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7984
7985 // First finger up.
7986 x2 += 15; y2 -= 20;
7987 processPosition(mapper, x2, y2);
7988 processId(mapper, 2);
7989 processMTSync(mapper);
7990 processSync(mapper);
7991
7992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007993 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007994 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7995 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7996 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7997 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7998 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7999 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8000 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8001 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8002 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8003
8004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8005 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8006 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8007 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8008 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8010 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8011
8012 // Move.
8013 x2 += 20; y2 -= 25;
8014 processPosition(mapper, x2, y2);
8015 processId(mapper, 2);
8016 processMTSync(mapper);
8017 processSync(mapper);
8018
8019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8021 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8022 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8023 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8024 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8025 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8026
8027 // New finger down.
8028 int32_t x3 = 700, y3 = 300;
8029 processPosition(mapper, x2, y2);
8030 processId(mapper, 2);
8031 processMTSync(mapper);
8032 processPosition(mapper, x3, y3);
8033 processId(mapper, 3);
8034 processMTSync(mapper);
8035 processSync(mapper);
8036
8037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008038 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008039 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8040 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8042 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8043 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8045 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8046 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8047 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8048
8049 // Second finger up.
8050 x3 += 30; y3 -= 20;
8051 processPosition(mapper, x3, y3);
8052 processId(mapper, 3);
8053 processMTSync(mapper);
8054 processSync(mapper);
8055
8056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008057 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008058 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8059 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8060 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8061 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8062 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8064 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8065 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8066 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8067
8068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8069 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8070 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8071 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8072 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8073 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8074 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8075
8076 // Last finger up.
8077 processMTSync(mapper);
8078 processSync(mapper);
8079
8080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8081 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8082 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8083 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8084 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8085 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8086 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8087
8088 // Should not have sent any more keys or motions.
8089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8091}
8092
8093TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008094 addConfigurationProperty("touch.deviceType", "touchScreen");
8095 prepareDisplay(DISPLAY_ORIENTATION_0);
8096 prepareAxes(POSITION | ID | SLOT);
8097 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008098 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008099
arthurhungdcef2dc2020-08-11 14:47:50 +08008100 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008101
8102 NotifyMotionArgs motionArgs;
8103
8104 // Two fingers down at once.
8105 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8106 processPosition(mapper, x1, y1);
8107 processId(mapper, 1);
8108 processSlot(mapper, 1);
8109 processPosition(mapper, x2, y2);
8110 processId(mapper, 2);
8111 processSync(mapper);
8112
8113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8114 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8115 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8116 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8117 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8118 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8119 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8120
8121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008122 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008123 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8124 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8125 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8126 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8127 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8128 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8129 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8130 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8131 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8132
8133 // Move.
8134 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8135 processSlot(mapper, 0);
8136 processPosition(mapper, x1, y1);
8137 processSlot(mapper, 1);
8138 processPosition(mapper, x2, y2);
8139 processSync(mapper);
8140
8141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8142 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8143 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8144 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8145 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8146 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8147 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8149 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8150 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8151 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8152
8153 // First finger up.
8154 x2 += 15; y2 -= 20;
8155 processSlot(mapper, 0);
8156 processId(mapper, -1);
8157 processSlot(mapper, 1);
8158 processPosition(mapper, x2, y2);
8159 processSync(mapper);
8160
8161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008162 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008163 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8164 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8166 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8169 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8171 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8172
8173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8175 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8176 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8177 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8178 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8179 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8180
8181 // Move.
8182 x2 += 20; y2 -= 25;
8183 processPosition(mapper, x2, y2);
8184 processSync(mapper);
8185
8186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8187 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8188 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8189 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8190 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8191 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8192 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8193
8194 // New finger down.
8195 int32_t x3 = 700, y3 = 300;
8196 processPosition(mapper, x2, y2);
8197 processSlot(mapper, 0);
8198 processId(mapper, 3);
8199 processPosition(mapper, x3, y3);
8200 processSync(mapper);
8201
8202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008203 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008204 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8205 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8206 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8207 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8208 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8210 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8211 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8212 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8213
8214 // Second finger up.
8215 x3 += 30; y3 -= 20;
8216 processSlot(mapper, 1);
8217 processId(mapper, -1);
8218 processSlot(mapper, 0);
8219 processPosition(mapper, x3, y3);
8220 processSync(mapper);
8221
8222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008223 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008224 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8225 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8226 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8227 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8228 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8229 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8230 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8232 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8233
8234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8236 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8237 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8238 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8239 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8240 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8241
8242 // Last finger up.
8243 processId(mapper, -1);
8244 processSync(mapper);
8245
8246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8247 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8248 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8249 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8250 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8251 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8252 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8253
8254 // Should not have sent any more keys or motions.
8255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8257}
8258
8259TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008260 addConfigurationProperty("touch.deviceType", "touchScreen");
8261 prepareDisplay(DISPLAY_ORIENTATION_0);
8262 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008263 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008264
8265 // These calculations are based on the input device calibration documentation.
8266 int32_t rawX = 100;
8267 int32_t rawY = 200;
8268 int32_t rawTouchMajor = 7;
8269 int32_t rawTouchMinor = 6;
8270 int32_t rawToolMajor = 9;
8271 int32_t rawToolMinor = 8;
8272 int32_t rawPressure = 11;
8273 int32_t rawDistance = 0;
8274 int32_t rawOrientation = 3;
8275 int32_t id = 5;
8276
8277 float x = toDisplayX(rawX);
8278 float y = toDisplayY(rawY);
8279 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8280 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8281 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8282 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8283 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8284 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8285 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8286 float distance = float(rawDistance);
8287
8288 processPosition(mapper, rawX, rawY);
8289 processTouchMajor(mapper, rawTouchMajor);
8290 processTouchMinor(mapper, rawTouchMinor);
8291 processToolMajor(mapper, rawToolMajor);
8292 processToolMinor(mapper, rawToolMinor);
8293 processPressure(mapper, rawPressure);
8294 processOrientation(mapper, rawOrientation);
8295 processDistance(mapper, rawDistance);
8296 processId(mapper, id);
8297 processMTSync(mapper);
8298 processSync(mapper);
8299
8300 NotifyMotionArgs args;
8301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8302 ASSERT_EQ(0, args.pointerProperties[0].id);
8303 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8304 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8305 orientation, distance));
8306}
8307
8308TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008309 addConfigurationProperty("touch.deviceType", "touchScreen");
8310 prepareDisplay(DISPLAY_ORIENTATION_0);
8311 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8312 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008313 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008314
8315 // These calculations are based on the input device calibration documentation.
8316 int32_t rawX = 100;
8317 int32_t rawY = 200;
8318 int32_t rawTouchMajor = 140;
8319 int32_t rawTouchMinor = 120;
8320 int32_t rawToolMajor = 180;
8321 int32_t rawToolMinor = 160;
8322
8323 float x = toDisplayX(rawX);
8324 float y = toDisplayY(rawY);
8325 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8326 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8327 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8328 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8329 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8330
8331 processPosition(mapper, rawX, rawY);
8332 processTouchMajor(mapper, rawTouchMajor);
8333 processTouchMinor(mapper, rawTouchMinor);
8334 processToolMajor(mapper, rawToolMajor);
8335 processToolMinor(mapper, rawToolMinor);
8336 processMTSync(mapper);
8337 processSync(mapper);
8338
8339 NotifyMotionArgs args;
8340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8342 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8343}
8344
8345TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008346 addConfigurationProperty("touch.deviceType", "touchScreen");
8347 prepareDisplay(DISPLAY_ORIENTATION_0);
8348 prepareAxes(POSITION | TOUCH | TOOL);
8349 addConfigurationProperty("touch.size.calibration", "diameter");
8350 addConfigurationProperty("touch.size.scale", "10");
8351 addConfigurationProperty("touch.size.bias", "160");
8352 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008353 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008354
8355 // These calculations are based on the input device calibration documentation.
8356 // Note: We only provide a single common touch/tool value because the device is assumed
8357 // not to emit separate values for each pointer (isSummed = 1).
8358 int32_t rawX = 100;
8359 int32_t rawY = 200;
8360 int32_t rawX2 = 150;
8361 int32_t rawY2 = 250;
8362 int32_t rawTouchMajor = 5;
8363 int32_t rawToolMajor = 8;
8364
8365 float x = toDisplayX(rawX);
8366 float y = toDisplayY(rawY);
8367 float x2 = toDisplayX(rawX2);
8368 float y2 = toDisplayY(rawY2);
8369 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8370 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8371 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8372
8373 processPosition(mapper, rawX, rawY);
8374 processTouchMajor(mapper, rawTouchMajor);
8375 processToolMajor(mapper, rawToolMajor);
8376 processMTSync(mapper);
8377 processPosition(mapper, rawX2, rawY2);
8378 processTouchMajor(mapper, rawTouchMajor);
8379 processToolMajor(mapper, rawToolMajor);
8380 processMTSync(mapper);
8381 processSync(mapper);
8382
8383 NotifyMotionArgs args;
8384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8385 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8386
8387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008388 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008389 ASSERT_EQ(size_t(2), args.pointerCount);
8390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8391 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8393 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8394}
8395
8396TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008397 addConfigurationProperty("touch.deviceType", "touchScreen");
8398 prepareDisplay(DISPLAY_ORIENTATION_0);
8399 prepareAxes(POSITION | TOUCH | TOOL);
8400 addConfigurationProperty("touch.size.calibration", "area");
8401 addConfigurationProperty("touch.size.scale", "43");
8402 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008403 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008404
8405 // These calculations are based on the input device calibration documentation.
8406 int32_t rawX = 100;
8407 int32_t rawY = 200;
8408 int32_t rawTouchMajor = 5;
8409 int32_t rawToolMajor = 8;
8410
8411 float x = toDisplayX(rawX);
8412 float y = toDisplayY(rawY);
8413 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8414 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8415 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8416
8417 processPosition(mapper, rawX, rawY);
8418 processTouchMajor(mapper, rawTouchMajor);
8419 processToolMajor(mapper, rawToolMajor);
8420 processMTSync(mapper);
8421 processSync(mapper);
8422
8423 NotifyMotionArgs args;
8424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8426 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8427}
8428
8429TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008430 addConfigurationProperty("touch.deviceType", "touchScreen");
8431 prepareDisplay(DISPLAY_ORIENTATION_0);
8432 prepareAxes(POSITION | PRESSURE);
8433 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8434 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008435 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008436
Michael Wrightaa449c92017-12-13 21:21:43 +00008437 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008438 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008439 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8440 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8441 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8442
Michael Wrightd02c5b62014-02-10 15:10:22 -08008443 // These calculations are based on the input device calibration documentation.
8444 int32_t rawX = 100;
8445 int32_t rawY = 200;
8446 int32_t rawPressure = 60;
8447
8448 float x = toDisplayX(rawX);
8449 float y = toDisplayY(rawY);
8450 float pressure = float(rawPressure) * 0.01f;
8451
8452 processPosition(mapper, rawX, rawY);
8453 processPressure(mapper, rawPressure);
8454 processMTSync(mapper);
8455 processSync(mapper);
8456
8457 NotifyMotionArgs args;
8458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8459 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8460 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8461}
8462
8463TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008464 addConfigurationProperty("touch.deviceType", "touchScreen");
8465 prepareDisplay(DISPLAY_ORIENTATION_0);
8466 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008467 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008468
8469 NotifyMotionArgs motionArgs;
8470 NotifyKeyArgs keyArgs;
8471
8472 processId(mapper, 1);
8473 processPosition(mapper, 100, 200);
8474 processSync(mapper);
8475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8476 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8477 ASSERT_EQ(0, motionArgs.buttonState);
8478
8479 // press BTN_LEFT, release BTN_LEFT
8480 processKey(mapper, BTN_LEFT, 1);
8481 processSync(mapper);
8482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8483 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8484 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8485
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8487 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8488 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8489
Michael Wrightd02c5b62014-02-10 15:10:22 -08008490 processKey(mapper, BTN_LEFT, 0);
8491 processSync(mapper);
8492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008493 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008494 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008495
8496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008497 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008498 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008499
8500 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8501 processKey(mapper, BTN_RIGHT, 1);
8502 processKey(mapper, BTN_MIDDLE, 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_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8507 motionArgs.buttonState);
8508
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8510 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8511 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8512
8513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8514 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8515 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8516 motionArgs.buttonState);
8517
Michael Wrightd02c5b62014-02-10 15:10:22 -08008518 processKey(mapper, BTN_RIGHT, 0);
8519 processSync(mapper);
8520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008521 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008522 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008523
8524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008525 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008526 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008527
8528 processKey(mapper, BTN_MIDDLE, 0);
8529 processSync(mapper);
8530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008531 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008532 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008533
8534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008536 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008537
8538 // press BTN_BACK, release BTN_BACK
8539 processKey(mapper, BTN_BACK, 1);
8540 processSync(mapper);
8541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8542 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8543 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008544
Michael Wrightd02c5b62014-02-10 15:10:22 -08008545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008546 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008547 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8548
8549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8550 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8551 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008552
8553 processKey(mapper, BTN_BACK, 0);
8554 processSync(mapper);
8555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008556 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008557 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008558
8559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008560 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008561 ASSERT_EQ(0, motionArgs.buttonState);
8562
Michael Wrightd02c5b62014-02-10 15:10:22 -08008563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8564 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8565 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8566
8567 // press BTN_SIDE, release BTN_SIDE
8568 processKey(mapper, BTN_SIDE, 1);
8569 processSync(mapper);
8570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8571 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8572 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008573
Michael Wrightd02c5b62014-02-10 15:10:22 -08008574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008575 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008576 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8577
8578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8579 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8580 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008581
8582 processKey(mapper, BTN_SIDE, 0);
8583 processSync(mapper);
8584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008585 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008586 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008587
8588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008589 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008590 ASSERT_EQ(0, motionArgs.buttonState);
8591
Michael Wrightd02c5b62014-02-10 15:10:22 -08008592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8593 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8594 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8595
8596 // press BTN_FORWARD, release BTN_FORWARD
8597 processKey(mapper, BTN_FORWARD, 1);
8598 processSync(mapper);
8599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8600 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8601 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008602
Michael Wrightd02c5b62014-02-10 15:10:22 -08008603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008605 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8606
8607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8608 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8609 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008610
8611 processKey(mapper, BTN_FORWARD, 0);
8612 processSync(mapper);
8613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008614 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008615 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008616
8617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008619 ASSERT_EQ(0, motionArgs.buttonState);
8620
Michael Wrightd02c5b62014-02-10 15:10:22 -08008621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8622 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8623 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8624
8625 // press BTN_EXTRA, release BTN_EXTRA
8626 processKey(mapper, BTN_EXTRA, 1);
8627 processSync(mapper);
8628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8629 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8630 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008631
Michael Wrightd02c5b62014-02-10 15:10:22 -08008632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008633 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008634 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8635
8636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8637 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8638 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008639
8640 processKey(mapper, BTN_EXTRA, 0);
8641 processSync(mapper);
8642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008643 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008644 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008645
8646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008647 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008648 ASSERT_EQ(0, motionArgs.buttonState);
8649
Michael Wrightd02c5b62014-02-10 15:10:22 -08008650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8651 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8652 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8653
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8655
Michael Wrightd02c5b62014-02-10 15:10:22 -08008656 // press BTN_STYLUS, release BTN_STYLUS
8657 processKey(mapper, BTN_STYLUS, 1);
8658 processSync(mapper);
8659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8660 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008661 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8662
8663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8664 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8665 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008666
8667 processKey(mapper, BTN_STYLUS, 0);
8668 processSync(mapper);
8669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008670 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008671 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008672
8673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008674 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008675 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008676
8677 // press BTN_STYLUS2, release BTN_STYLUS2
8678 processKey(mapper, BTN_STYLUS2, 1);
8679 processSync(mapper);
8680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8681 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008682 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8683
8684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8685 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8686 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008687
8688 processKey(mapper, BTN_STYLUS2, 0);
8689 processSync(mapper);
8690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008691 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008692 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008693
8694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008695 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008696 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008697
8698 // release touch
8699 processId(mapper, -1);
8700 processSync(mapper);
8701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8702 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8703 ASSERT_EQ(0, motionArgs.buttonState);
8704}
8705
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008706TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8707 addConfigurationProperty("touch.deviceType", "touchScreen");
8708 prepareDisplay(DISPLAY_ORIENTATION_0);
8709 prepareAxes(POSITION | ID | SLOT);
8710 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8711
8712 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8713 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8714
8715 // Touch down.
8716 processId(mapper, 1);
8717 processPosition(mapper, 100, 200);
8718 processSync(mapper);
8719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8720 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8721
8722 // Press and release button mapped to the primary stylus button.
8723 processKey(mapper, BTN_A, 1);
8724 processSync(mapper);
8725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8726 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8727 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8729 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8730 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8731
8732 processKey(mapper, BTN_A, 0);
8733 processSync(mapper);
8734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8735 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8737 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8738
8739 // Press and release the HID usage mapped to the secondary stylus button.
8740 processHidUsage(mapper, 0xabcd, 1);
8741 processSync(mapper);
8742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8743 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8744 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8746 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8747 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8748
8749 processHidUsage(mapper, 0xabcd, 0);
8750 processSync(mapper);
8751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8752 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8754 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8755
8756 // Release touch.
8757 processId(mapper, -1);
8758 processSync(mapper);
8759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8760 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8761}
8762
Michael Wrightd02c5b62014-02-10 15:10:22 -08008763TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008764 addConfigurationProperty("touch.deviceType", "touchScreen");
8765 prepareDisplay(DISPLAY_ORIENTATION_0);
8766 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008767 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008768
8769 NotifyMotionArgs motionArgs;
8770
8771 // default tool type is finger
8772 processId(mapper, 1);
8773 processPosition(mapper, 100, 200);
8774 processSync(mapper);
8775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8776 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8777 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8778
8779 // eraser
8780 processKey(mapper, BTN_TOOL_RUBBER, 1);
8781 processSync(mapper);
8782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8783 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8784 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8785
8786 // stylus
8787 processKey(mapper, BTN_TOOL_RUBBER, 0);
8788 processKey(mapper, BTN_TOOL_PEN, 1);
8789 processSync(mapper);
8790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8792 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8793
8794 // brush
8795 processKey(mapper, BTN_TOOL_PEN, 0);
8796 processKey(mapper, BTN_TOOL_BRUSH, 1);
8797 processSync(mapper);
8798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8799 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8800 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8801
8802 // pencil
8803 processKey(mapper, BTN_TOOL_BRUSH, 0);
8804 processKey(mapper, BTN_TOOL_PENCIL, 1);
8805 processSync(mapper);
8806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8808 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8809
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008810 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008811 processKey(mapper, BTN_TOOL_PENCIL, 0);
8812 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8813 processSync(mapper);
8814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8815 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8816 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8817
8818 // mouse
8819 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8820 processKey(mapper, BTN_TOOL_MOUSE, 1);
8821 processSync(mapper);
8822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8823 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8824 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8825
8826 // lens
8827 processKey(mapper, BTN_TOOL_MOUSE, 0);
8828 processKey(mapper, BTN_TOOL_LENS, 1);
8829 processSync(mapper);
8830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8832 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8833
8834 // double-tap
8835 processKey(mapper, BTN_TOOL_LENS, 0);
8836 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8837 processSync(mapper);
8838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8839 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8840 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8841
8842 // triple-tap
8843 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8844 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8845 processSync(mapper);
8846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8847 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8848 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8849
8850 // quad-tap
8851 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8852 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8853 processSync(mapper);
8854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8855 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8856 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8857
8858 // finger
8859 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8860 processKey(mapper, BTN_TOOL_FINGER, 1);
8861 processSync(mapper);
8862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8863 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8864 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8865
8866 // stylus trumps finger
8867 processKey(mapper, BTN_TOOL_PEN, 1);
8868 processSync(mapper);
8869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8870 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8871 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8872
8873 // eraser trumps stylus
8874 processKey(mapper, BTN_TOOL_RUBBER, 1);
8875 processSync(mapper);
8876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8877 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8878 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8879
8880 // mouse trumps eraser
8881 processKey(mapper, BTN_TOOL_MOUSE, 1);
8882 processSync(mapper);
8883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8884 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8885 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8886
8887 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8888 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8889 processSync(mapper);
8890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8891 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8892 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8893
8894 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8895 processToolType(mapper, MT_TOOL_PEN);
8896 processSync(mapper);
8897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8898 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8899 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8900
8901 // back to default tool type
8902 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8903 processKey(mapper, BTN_TOOL_MOUSE, 0);
8904 processKey(mapper, BTN_TOOL_RUBBER, 0);
8905 processKey(mapper, BTN_TOOL_PEN, 0);
8906 processKey(mapper, BTN_TOOL_FINGER, 0);
8907 processSync(mapper);
8908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8909 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8910 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8911}
8912
8913TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008914 addConfigurationProperty("touch.deviceType", "touchScreen");
8915 prepareDisplay(DISPLAY_ORIENTATION_0);
8916 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008917 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008918 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008919
8920 NotifyMotionArgs motionArgs;
8921
8922 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8923 processId(mapper, 1);
8924 processPosition(mapper, 100, 200);
8925 processSync(mapper);
8926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8927 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8928 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8929 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8930
8931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8932 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8934 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8935
8936 // move a little
8937 processPosition(mapper, 150, 250);
8938 processSync(mapper);
8939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8940 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8942 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8943
8944 // down when BTN_TOUCH is pressed, pressure defaults to 1
8945 processKey(mapper, BTN_TOUCH, 1);
8946 processSync(mapper);
8947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8948 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8949 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8950 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8951
8952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8953 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8954 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8955 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8956
8957 // up when BTN_TOUCH is released, hover restored
8958 processKey(mapper, BTN_TOUCH, 0);
8959 processSync(mapper);
8960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8961 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8963 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8964
8965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8966 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8967 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8968 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8969
8970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8971 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8972 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8973 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8974
8975 // exit hover when pointer goes away
8976 processId(mapper, -1);
8977 processSync(mapper);
8978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8979 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8981 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8982}
8983
8984TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008985 addConfigurationProperty("touch.deviceType", "touchScreen");
8986 prepareDisplay(DISPLAY_ORIENTATION_0);
8987 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008988 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008989
8990 NotifyMotionArgs motionArgs;
8991
8992 // initially hovering because pressure is 0
8993 processId(mapper, 1);
8994 processPosition(mapper, 100, 200);
8995 processPressure(mapper, 0);
8996 processSync(mapper);
8997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8998 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8999 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9000 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9001
9002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9003 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9004 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9005 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9006
9007 // move a little
9008 processPosition(mapper, 150, 250);
9009 processSync(mapper);
9010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9011 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9012 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9013 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9014
9015 // down when pressure becomes non-zero
9016 processPressure(mapper, RAW_PRESSURE_MAX);
9017 processSync(mapper);
9018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9019 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9020 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9021 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9022
9023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9024 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9025 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9026 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9027
9028 // up when pressure becomes 0, hover restored
9029 processPressure(mapper, 0);
9030 processSync(mapper);
9031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9032 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9034 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9035
9036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9037 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9038 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9039 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9040
9041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9042 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9044 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9045
9046 // exit hover when pointer goes away
9047 processId(mapper, -1);
9048 processSync(mapper);
9049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9050 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9051 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9052 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9053}
9054
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009055/**
9056 * Set the input device port <--> display port associations, and check that the
9057 * events are routed to the display that matches the display port.
9058 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9059 */
9060TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009061 const std::string usb2 = "USB2";
9062 const uint8_t hdmi1 = 0;
9063 const uint8_t hdmi2 = 1;
9064 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009065 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009066
9067 addConfigurationProperty("touch.deviceType", "touchScreen");
9068 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009069 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009070
9071 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9072 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9073
9074 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9075 // for this input device is specified, and the matching viewport is not present,
9076 // the input device should be disabled (at the mapper level).
9077
9078 // Add viewport for display 2 on hdmi2
9079 prepareSecondaryDisplay(type, hdmi2);
9080 // Send a touch event
9081 processPosition(mapper, 100, 100);
9082 processSync(mapper);
9083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9084
9085 // Add viewport for display 1 on hdmi1
9086 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
9087 // Send a touch event again
9088 processPosition(mapper, 100, 100);
9089 processSync(mapper);
9090
9091 NotifyMotionArgs args;
9092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9093 ASSERT_EQ(DISPLAY_ID, args.displayId);
9094}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009095
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009096TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9097 addConfigurationProperty("touch.deviceType", "touchScreen");
9098 prepareAxes(POSITION);
9099 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9100
9101 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9102
9103 prepareDisplay(DISPLAY_ORIENTATION_0);
9104 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9105
9106 // Send a touch event
9107 processPosition(mapper, 100, 100);
9108 processSync(mapper);
9109
9110 NotifyMotionArgs args;
9111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9112 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9113}
9114
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009115TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009116 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009117 std::shared_ptr<FakePointerController> fakePointerController =
9118 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009119 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009120 fakePointerController->setPosition(100, 200);
9121 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009122 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009123
Garfield Tan888a6a42020-01-09 11:39:16 -08009124 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009125 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009126
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009127 prepareDisplay(DISPLAY_ORIENTATION_0);
9128 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009129 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009130
9131 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009132 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009133
9134 NotifyMotionArgs motionArgs;
9135 processPosition(mapper, 100, 100);
9136 processSync(mapper);
9137
9138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9139 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9140 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9141}
9142
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009143/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009144 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9145 */
9146TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9147 addConfigurationProperty("touch.deviceType", "touchScreen");
9148 prepareAxes(POSITION);
9149 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9150
9151 prepareDisplay(DISPLAY_ORIENTATION_0);
9152 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
9153 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
9154 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
9155 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9156
9157 NotifyMotionArgs args;
9158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9159 ASSERT_EQ(26, args.readTime);
9160
9161 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
9162 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
9163 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9164
9165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9166 ASSERT_EQ(33, args.readTime);
9167}
9168
9169/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009170 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9171 * events should not be delivered to the listener.
9172 */
9173TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9174 addConfigurationProperty("touch.deviceType", "touchScreen");
9175 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9176 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
9177 ViewportType::INTERNAL);
9178 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9179 prepareAxes(POSITION);
9180 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9181
9182 NotifyMotionArgs motionArgs;
9183 processPosition(mapper, 100, 100);
9184 processSync(mapper);
9185
9186 mFakeListener->assertNotifyMotionWasNotCalled();
9187}
9188
Garfield Tanc734e4f2021-01-15 20:01:39 -08009189TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9190 addConfigurationProperty("touch.deviceType", "touchScreen");
9191 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9192 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
9193 ViewportType::INTERNAL);
9194 std::optional<DisplayViewport> optionalDisplayViewport =
9195 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9196 ASSERT_TRUE(optionalDisplayViewport.has_value());
9197 DisplayViewport displayViewport = *optionalDisplayViewport;
9198
9199 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9200 prepareAxes(POSITION);
9201 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9202
9203 // Finger down
9204 int32_t x = 100, y = 100;
9205 processPosition(mapper, x, y);
9206 processSync(mapper);
9207
9208 NotifyMotionArgs motionArgs;
9209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9210 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9211
9212 // Deactivate display viewport
9213 displayViewport.isActive = false;
9214 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9215 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9216
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009217 // The ongoing touch should be canceled immediately
9218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9219 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9220
9221 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009222 x += 10, y += 10;
9223 processPosition(mapper, x, y);
9224 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009226
9227 // Reactivate display viewport
9228 displayViewport.isActive = true;
9229 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9230 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9231
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009232 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009233 x += 10, y += 10;
9234 processPosition(mapper, x, y);
9235 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9237 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009238}
9239
Arthur Hung7c645402019-01-25 17:45:42 +08009240TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9241 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009242 prepareAxes(POSITION | ID | SLOT);
9243 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009244 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009245
9246 // Create the second touch screen device, and enable multi fingers.
9247 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009248 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009249 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009250 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009251 std::shared_ptr<InputDevice> device2 =
9252 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009253 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009254
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009255 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9256 0 /*flat*/, 0 /*fuzz*/);
9257 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9258 0 /*flat*/, 0 /*fuzz*/);
9259 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
9260 0 /*flat*/, 0 /*fuzz*/);
9261 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
9262 0 /*flat*/, 0 /*fuzz*/);
9263 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
9264 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9265 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009266
9267 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009268 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009269 std::list<NotifyArgs> unused =
9270 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9271 0 /*changes*/);
9272 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009273
9274 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009275 std::shared_ptr<FakePointerController> fakePointerController =
9276 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009277 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009278
9279 // Setup policy for associated displays and show touches.
9280 const uint8_t hdmi1 = 0;
9281 const uint8_t hdmi2 = 1;
9282 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9283 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9284 mFakePolicy->setShowTouches(true);
9285
9286 // Create displays.
9287 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009288 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009289
9290 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009291 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9292 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9293 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009294
9295 // Two fingers down at default display.
9296 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9297 processPosition(mapper, x1, y1);
9298 processId(mapper, 1);
9299 processSlot(mapper, 1);
9300 processPosition(mapper, x2, y2);
9301 processId(mapper, 2);
9302 processSync(mapper);
9303
9304 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9305 fakePointerController->getSpots().find(DISPLAY_ID);
9306 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9307 ASSERT_EQ(size_t(2), iter->second.size());
9308
9309 // Two fingers down at second display.
9310 processPosition(mapper2, x1, y1);
9311 processId(mapper2, 1);
9312 processSlot(mapper2, 1);
9313 processPosition(mapper2, x2, y2);
9314 processId(mapper2, 2);
9315 processSync(mapper2);
9316
9317 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9318 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9319 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009320
9321 // Disable the show touches configuration and ensure the spots are cleared.
9322 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009323 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9324 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009325
9326 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009327}
9328
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009329TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009330 prepareAxes(POSITION);
9331 addConfigurationProperty("touch.deviceType", "touchScreen");
9332 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009333 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009334
9335 NotifyMotionArgs motionArgs;
9336 // Unrotated video frame
9337 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9338 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009339 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009340 processPosition(mapper, 100, 200);
9341 processSync(mapper);
9342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9343 ASSERT_EQ(frames, motionArgs.videoFrames);
9344
9345 // Subsequent touch events should not have any videoframes
9346 // This is implemented separately in FakeEventHub,
9347 // but that should match the behaviour of TouchVideoDevice.
9348 processPosition(mapper, 200, 200);
9349 processSync(mapper);
9350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9351 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9352}
9353
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009354TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009355 prepareAxes(POSITION);
9356 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009357 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009358 // Unrotated video frame
9359 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9360 NotifyMotionArgs motionArgs;
9361
9362 // Test all 4 orientations
9363 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009364 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9365 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9366 clearViewports();
9367 prepareDisplay(orientation);
9368 std::vector<TouchVideoFrame> frames{frame};
9369 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9370 processPosition(mapper, 100, 200);
9371 processSync(mapper);
9372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9373 ASSERT_EQ(frames, motionArgs.videoFrames);
9374 }
9375}
9376
9377TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9378 prepareAxes(POSITION);
9379 addConfigurationProperty("touch.deviceType", "touchScreen");
9380 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9381 // orientation-aware are affected by display rotation.
9382 addConfigurationProperty("touch.orientationAware", "0");
9383 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9384 // Unrotated video frame
9385 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9386 NotifyMotionArgs motionArgs;
9387
9388 // Test all 4 orientations
9389 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009390 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9391 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9392 clearViewports();
9393 prepareDisplay(orientation);
9394 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009395 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009396 processPosition(mapper, 100, 200);
9397 processSync(mapper);
9398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009399 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9400 // compared to the display. This is so that when the window transform (which contains the
9401 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9402 // window's coordinate space.
9403 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009404 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009405
9406 // Release finger.
9407 processSync(mapper);
9408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009409 }
9410}
9411
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009412TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009413 prepareAxes(POSITION);
9414 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009415 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009416 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9417 // so mix these.
9418 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9419 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9420 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9421 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9422 NotifyMotionArgs motionArgs;
9423
9424 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009425 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009426 processPosition(mapper, 100, 200);
9427 processSync(mapper);
9428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009429 ASSERT_EQ(frames, motionArgs.videoFrames);
9430}
9431
9432TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9433 prepareAxes(POSITION);
9434 addConfigurationProperty("touch.deviceType", "touchScreen");
9435 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9436 // orientation-aware are affected by display rotation.
9437 addConfigurationProperty("touch.orientationAware", "0");
9438 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9439 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9440 // so mix these.
9441 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9442 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9443 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9444 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9445 NotifyMotionArgs motionArgs;
9446
9447 prepareDisplay(DISPLAY_ORIENTATION_90);
9448 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9449 processPosition(mapper, 100, 200);
9450 processSync(mapper);
9451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9452 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9453 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9454 // compared to the display. This is so that when the window transform (which contains the
9455 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9456 // window's coordinate space.
9457 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
9458 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009459 ASSERT_EQ(frames, motionArgs.videoFrames);
9460}
9461
Arthur Hung9da14732019-09-02 16:16:58 +08009462/**
9463 * If we had defined port associations, but the viewport is not ready, the touch device would be
9464 * expected to be disabled, and it should be enabled after the viewport has found.
9465 */
9466TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009467 constexpr uint8_t hdmi2 = 1;
9468 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009469 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009470
9471 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9472
9473 addConfigurationProperty("touch.deviceType", "touchScreen");
9474 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009475 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009476
9477 ASSERT_EQ(mDevice->isEnabled(), false);
9478
9479 // Add display on hdmi2, the device should be enabled and can receive touch event.
9480 prepareSecondaryDisplay(type, hdmi2);
9481 ASSERT_EQ(mDevice->isEnabled(), true);
9482
9483 // Send a touch event.
9484 processPosition(mapper, 100, 100);
9485 processSync(mapper);
9486
9487 NotifyMotionArgs args;
9488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9489 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9490}
9491
Arthur Hung421eb1c2020-01-16 00:09:42 +08009492TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009493 addConfigurationProperty("touch.deviceType", "touchScreen");
9494 prepareDisplay(DISPLAY_ORIENTATION_0);
9495 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009496 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009497
9498 NotifyMotionArgs motionArgs;
9499
9500 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9501 // finger down
9502 processId(mapper, 1);
9503 processPosition(mapper, x1, y1);
9504 processSync(mapper);
9505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9506 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9507 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9508
9509 // finger move
9510 processId(mapper, 1);
9511 processPosition(mapper, x2, y2);
9512 processSync(mapper);
9513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9514 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9515 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9516
9517 // finger up.
9518 processId(mapper, -1);
9519 processSync(mapper);
9520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9521 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9522 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9523
9524 // new finger down
9525 processId(mapper, 1);
9526 processPosition(mapper, x3, y3);
9527 processSync(mapper);
9528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9529 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9530 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9531}
9532
9533/**
arthurhungcc7f9802020-04-30 17:55:40 +08009534 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9535 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009536 */
arthurhungcc7f9802020-04-30 17:55:40 +08009537TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009538 addConfigurationProperty("touch.deviceType", "touchScreen");
9539 prepareDisplay(DISPLAY_ORIENTATION_0);
9540 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009541 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009542
9543 NotifyMotionArgs motionArgs;
9544
9545 // default tool type is finger
9546 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009547 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009548 processPosition(mapper, x1, y1);
9549 processSync(mapper);
9550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9551 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9553
9554 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9555 processToolType(mapper, MT_TOOL_PALM);
9556 processSync(mapper);
9557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9558 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9559
9560 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009561 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009562 processPosition(mapper, x2, y2);
9563 processSync(mapper);
9564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9565
9566 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009567 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009568 processSync(mapper);
9569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9570
9571 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009572 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009573 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009574 processPosition(mapper, x3, y3);
9575 processSync(mapper);
9576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9577 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9578 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9579}
9580
arthurhungbf89a482020-04-17 17:37:55 +08009581/**
arthurhungcc7f9802020-04-30 17:55:40 +08009582 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9583 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009584 */
arthurhungcc7f9802020-04-30 17:55:40 +08009585TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009586 addConfigurationProperty("touch.deviceType", "touchScreen");
9587 prepareDisplay(DISPLAY_ORIENTATION_0);
9588 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9589 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9590
9591 NotifyMotionArgs motionArgs;
9592
9593 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009594 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9595 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009596 processPosition(mapper, x1, y1);
9597 processSync(mapper);
9598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9599 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9600 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9601
9602 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009603 processSlot(mapper, SECOND_SLOT);
9604 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009605 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009606 processSync(mapper);
9607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009608 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009609 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9610
9611 // If the tool type of the first finger changes to MT_TOOL_PALM,
9612 // we expect to receive ACTION_POINTER_UP with cancel flag.
9613 processSlot(mapper, FIRST_SLOT);
9614 processId(mapper, FIRST_TRACKING_ID);
9615 processToolType(mapper, MT_TOOL_PALM);
9616 processSync(mapper);
9617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009618 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009619 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9620
9621 // The following MOVE events of second finger should be processed.
9622 processSlot(mapper, SECOND_SLOT);
9623 processId(mapper, SECOND_TRACKING_ID);
9624 processPosition(mapper, x2 + 1, y2 + 1);
9625 processSync(mapper);
9626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9627 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9628 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9629
9630 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9631 // it. Second finger receive move.
9632 processSlot(mapper, FIRST_SLOT);
9633 processId(mapper, INVALID_TRACKING_ID);
9634 processSync(mapper);
9635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9636 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9637 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9638
9639 // Second finger keeps moving.
9640 processSlot(mapper, SECOND_SLOT);
9641 processId(mapper, SECOND_TRACKING_ID);
9642 processPosition(mapper, x2 + 2, y2 + 2);
9643 processSync(mapper);
9644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9645 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9646 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9647
9648 // Second finger up.
9649 processId(mapper, INVALID_TRACKING_ID);
9650 processSync(mapper);
9651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9652 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9653 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9654}
9655
9656/**
9657 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9658 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9659 */
9660TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9661 addConfigurationProperty("touch.deviceType", "touchScreen");
9662 prepareDisplay(DISPLAY_ORIENTATION_0);
9663 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9664 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9665
9666 NotifyMotionArgs motionArgs;
9667
9668 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9669 // First finger down.
9670 processId(mapper, FIRST_TRACKING_ID);
9671 processPosition(mapper, x1, y1);
9672 processSync(mapper);
9673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9674 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9676
9677 // Second finger down.
9678 processSlot(mapper, SECOND_SLOT);
9679 processId(mapper, SECOND_TRACKING_ID);
9680 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009681 processSync(mapper);
9682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009683 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009684 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9685
arthurhungcc7f9802020-04-30 17:55:40 +08009686 // If the tool type of the first finger changes to MT_TOOL_PALM,
9687 // we expect to receive ACTION_POINTER_UP with cancel flag.
9688 processSlot(mapper, FIRST_SLOT);
9689 processId(mapper, FIRST_TRACKING_ID);
9690 processToolType(mapper, MT_TOOL_PALM);
9691 processSync(mapper);
9692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009693 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009694 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9695
9696 // Second finger keeps moving.
9697 processSlot(mapper, SECOND_SLOT);
9698 processId(mapper, SECOND_TRACKING_ID);
9699 processPosition(mapper, x2 + 1, y2 + 1);
9700 processSync(mapper);
9701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9702 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9703
9704 // second finger becomes palm, receive cancel due to only 1 finger is active.
9705 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009706 processToolType(mapper, MT_TOOL_PALM);
9707 processSync(mapper);
9708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9709 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9710
arthurhungcc7f9802020-04-30 17:55:40 +08009711 // third finger down.
9712 processSlot(mapper, THIRD_SLOT);
9713 processId(mapper, THIRD_TRACKING_ID);
9714 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009715 processPosition(mapper, x3, y3);
9716 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9718 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9719 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009720 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9721
9722 // third finger move
9723 processId(mapper, THIRD_TRACKING_ID);
9724 processPosition(mapper, x3 + 1, y3 + 1);
9725 processSync(mapper);
9726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9727 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9728
9729 // first finger up, third finger receive move.
9730 processSlot(mapper, FIRST_SLOT);
9731 processId(mapper, INVALID_TRACKING_ID);
9732 processSync(mapper);
9733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9734 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9735 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9736
9737 // second finger up, third finger receive move.
9738 processSlot(mapper, SECOND_SLOT);
9739 processId(mapper, INVALID_TRACKING_ID);
9740 processSync(mapper);
9741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9742 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9743 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9744
9745 // third finger up.
9746 processSlot(mapper, THIRD_SLOT);
9747 processId(mapper, INVALID_TRACKING_ID);
9748 processSync(mapper);
9749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9750 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9751 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9752}
9753
9754/**
9755 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9756 * and the active finger could still be allowed to receive the events
9757 */
9758TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9759 addConfigurationProperty("touch.deviceType", "touchScreen");
9760 prepareDisplay(DISPLAY_ORIENTATION_0);
9761 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9762 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9763
9764 NotifyMotionArgs motionArgs;
9765
9766 // default tool type is finger
9767 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9768 processId(mapper, FIRST_TRACKING_ID);
9769 processPosition(mapper, x1, y1);
9770 processSync(mapper);
9771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9772 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9773 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9774
9775 // Second finger down.
9776 processSlot(mapper, SECOND_SLOT);
9777 processId(mapper, SECOND_TRACKING_ID);
9778 processPosition(mapper, x2, y2);
9779 processSync(mapper);
9780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009781 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009782 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9783
9784 // If the tool type of the second finger changes to MT_TOOL_PALM,
9785 // we expect to receive ACTION_POINTER_UP with cancel flag.
9786 processId(mapper, SECOND_TRACKING_ID);
9787 processToolType(mapper, MT_TOOL_PALM);
9788 processSync(mapper);
9789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009790 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009791 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9792
9793 // The following MOVE event should be processed.
9794 processSlot(mapper, FIRST_SLOT);
9795 processId(mapper, FIRST_TRACKING_ID);
9796 processPosition(mapper, x1 + 1, y1 + 1);
9797 processSync(mapper);
9798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9799 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9800 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9801
9802 // second finger up.
9803 processSlot(mapper, SECOND_SLOT);
9804 processId(mapper, INVALID_TRACKING_ID);
9805 processSync(mapper);
9806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9808
9809 // first finger keep moving
9810 processSlot(mapper, FIRST_SLOT);
9811 processId(mapper, FIRST_TRACKING_ID);
9812 processPosition(mapper, x1 + 2, y1 + 2);
9813 processSync(mapper);
9814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9815 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9816
9817 // first finger up.
9818 processId(mapper, INVALID_TRACKING_ID);
9819 processSync(mapper);
9820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9821 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9822 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009823}
9824
Arthur Hung9ad18942021-06-19 02:04:46 +00009825/**
9826 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9827 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9828 * cause slot be valid again.
9829 */
9830TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9831 addConfigurationProperty("touch.deviceType", "touchScreen");
9832 prepareDisplay(DISPLAY_ORIENTATION_0);
9833 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9834 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9835
9836 NotifyMotionArgs motionArgs;
9837
9838 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9839 // First finger down.
9840 processId(mapper, FIRST_TRACKING_ID);
9841 processPosition(mapper, x1, y1);
9842 processPressure(mapper, RAW_PRESSURE_MAX);
9843 processSync(mapper);
9844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9845 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9846 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9847
9848 // First finger move.
9849 processId(mapper, FIRST_TRACKING_ID);
9850 processPosition(mapper, x1 + 1, y1 + 1);
9851 processPressure(mapper, RAW_PRESSURE_MAX);
9852 processSync(mapper);
9853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9854 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9855 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9856
9857 // Second finger down.
9858 processSlot(mapper, SECOND_SLOT);
9859 processId(mapper, SECOND_TRACKING_ID);
9860 processPosition(mapper, x2, y2);
9861 processPressure(mapper, RAW_PRESSURE_MAX);
9862 processSync(mapper);
9863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009864 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009865 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9866
9867 // second finger up with some unexpected data.
9868 processSlot(mapper, SECOND_SLOT);
9869 processId(mapper, INVALID_TRACKING_ID);
9870 processPosition(mapper, x2, y2);
9871 processSync(mapper);
9872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009873 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009874 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9875
9876 // first finger up with some unexpected data.
9877 processSlot(mapper, FIRST_SLOT);
9878 processId(mapper, INVALID_TRACKING_ID);
9879 processPosition(mapper, x2, y2);
9880 processPressure(mapper, RAW_PRESSURE_MAX);
9881 processSync(mapper);
9882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9883 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9884 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9885}
9886
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009887TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9888 addConfigurationProperty("touch.deviceType", "touchScreen");
9889 prepareDisplay(DISPLAY_ORIENTATION_0);
9890 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9891 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9892
9893 // First finger down.
9894 processId(mapper, FIRST_TRACKING_ID);
9895 processPosition(mapper, 100, 200);
9896 processPressure(mapper, RAW_PRESSURE_MAX);
9897 processSync(mapper);
9898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9899 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9900
9901 // Second finger down.
9902 processSlot(mapper, SECOND_SLOT);
9903 processId(mapper, SECOND_TRACKING_ID);
9904 processPosition(mapper, 300, 400);
9905 processPressure(mapper, RAW_PRESSURE_MAX);
9906 processSync(mapper);
9907 ASSERT_NO_FATAL_FAILURE(
9908 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9909
9910 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009911 // preserved. Resetting should cancel the ongoing gesture.
9912 resetMapper(mapper, ARBITRARY_TIME);
9913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9914 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009915
9916 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9917 // the existing touch state to generate a down event.
9918 processPosition(mapper, 301, 302);
9919 processSync(mapper);
9920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9921 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9923 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9924
9925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9926}
9927
9928TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9929 addConfigurationProperty("touch.deviceType", "touchScreen");
9930 prepareDisplay(DISPLAY_ORIENTATION_0);
9931 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9932 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9933
9934 // First finger touches down and releases.
9935 processId(mapper, FIRST_TRACKING_ID);
9936 processPosition(mapper, 100, 200);
9937 processPressure(mapper, RAW_PRESSURE_MAX);
9938 processSync(mapper);
9939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9940 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9941 processId(mapper, INVALID_TRACKING_ID);
9942 processSync(mapper);
9943 ASSERT_NO_FATAL_FAILURE(
9944 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9945
9946 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9947 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009948 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9950
9951 // Send an empty sync frame. Since there are no pointers, no events are generated.
9952 processSync(mapper);
9953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9954}
9955
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009956// --- MultiTouchInputMapperTest_ExternalDevice ---
9957
9958class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9959protected:
Chris Yea52ade12020-08-27 16:49:20 -07009960 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009961};
9962
9963/**
9964 * Expect fallback to internal viewport if device is external and external viewport is not present.
9965 */
9966TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9967 prepareAxes(POSITION);
9968 addConfigurationProperty("touch.deviceType", "touchScreen");
9969 prepareDisplay(DISPLAY_ORIENTATION_0);
9970 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9971
9972 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9973
9974 NotifyMotionArgs motionArgs;
9975
9976 // Expect the event to be sent to the internal viewport,
9977 // because an external viewport is not present.
9978 processPosition(mapper, 100, 100);
9979 processSync(mapper);
9980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9981 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9982
9983 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009984 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009985 processPosition(mapper, 100, 100);
9986 processSync(mapper);
9987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9988 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9989}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009990
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009991TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9992 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9993 std::shared_ptr<FakePointerController> fakePointerController =
9994 std::make_shared<FakePointerController>();
9995 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9996 fakePointerController->setPosition(0, 0);
9997 fakePointerController->setButtonState(0);
9998
9999 // prepare device and capture
10000 prepareDisplay(DISPLAY_ORIENTATION_0);
10001 prepareAxes(POSITION | ID | SLOT);
10002 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10003 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10004 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010005 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010006 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10007
10008 // captured touchpad should be a touchpad source
10009 NotifyDeviceResetArgs resetArgs;
10010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10011 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10012
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010013 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010014
10015 const InputDeviceInfo::MotionRange* relRangeX =
10016 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10017 ASSERT_NE(relRangeX, nullptr);
10018 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10019 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10020 const InputDeviceInfo::MotionRange* relRangeY =
10021 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10022 ASSERT_NE(relRangeY, nullptr);
10023 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10024 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10025
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010026 // run captured pointer tests - note that this is unscaled, so input listener events should be
10027 // identical to what the hardware sends (accounting for any
10028 // calibration).
10029 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010030 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010031 processId(mapper, 1);
10032 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10033 processKey(mapper, BTN_TOUCH, 1);
10034 processSync(mapper);
10035
10036 // expect coord[0] to contain initial location of touch 0
10037 NotifyMotionArgs args;
10038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10039 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10040 ASSERT_EQ(1U, args.pointerCount);
10041 ASSERT_EQ(0, args.pointerProperties[0].id);
10042 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10043 ASSERT_NO_FATAL_FAILURE(
10044 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10045
10046 // FINGER 1 DOWN
10047 processSlot(mapper, 1);
10048 processId(mapper, 2);
10049 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10050 processSync(mapper);
10051
10052 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010054 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010055 ASSERT_EQ(2U, args.pointerCount);
10056 ASSERT_EQ(0, args.pointerProperties[0].id);
10057 ASSERT_EQ(1, args.pointerProperties[1].id);
10058 ASSERT_NO_FATAL_FAILURE(
10059 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10060 ASSERT_NO_FATAL_FAILURE(
10061 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10062
10063 // FINGER 1 MOVE
10064 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10065 processSync(mapper);
10066
10067 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10068 // from move
10069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10070 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10071 ASSERT_NO_FATAL_FAILURE(
10072 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10073 ASSERT_NO_FATAL_FAILURE(
10074 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10075
10076 // FINGER 0 MOVE
10077 processSlot(mapper, 0);
10078 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10079 processSync(mapper);
10080
10081 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10084 ASSERT_NO_FATAL_FAILURE(
10085 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10086 ASSERT_NO_FATAL_FAILURE(
10087 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10088
10089 // BUTTON DOWN
10090 processKey(mapper, BTN_LEFT, 1);
10091 processSync(mapper);
10092
10093 // touchinputmapper design sends a move before button press
10094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10095 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10097 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10098
10099 // BUTTON UP
10100 processKey(mapper, BTN_LEFT, 0);
10101 processSync(mapper);
10102
10103 // touchinputmapper design sends a move after button release
10104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10105 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10108
10109 // FINGER 0 UP
10110 processId(mapper, -1);
10111 processSync(mapper);
10112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10113 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10114
10115 // FINGER 1 MOVE
10116 processSlot(mapper, 1);
10117 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10118 processSync(mapper);
10119
10120 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10123 ASSERT_EQ(1U, args.pointerCount);
10124 ASSERT_EQ(1, args.pointerProperties[0].id);
10125 ASSERT_NO_FATAL_FAILURE(
10126 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10127
10128 // FINGER 1 UP
10129 processId(mapper, -1);
10130 processKey(mapper, BTN_TOUCH, 0);
10131 processSync(mapper);
10132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10133 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10134
10135 // non captured touchpad should be a mouse source
10136 mFakePolicy->setPointerCapture(false);
10137 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10139 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
10140}
10141
10142TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10143 std::shared_ptr<FakePointerController> fakePointerController =
10144 std::make_shared<FakePointerController>();
10145 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10146 fakePointerController->setPosition(0, 0);
10147 fakePointerController->setButtonState(0);
10148
10149 // prepare device and capture
10150 prepareDisplay(DISPLAY_ORIENTATION_0);
10151 prepareAxes(POSITION | ID | SLOT);
10152 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10153 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010154 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010155 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10156 // run uncaptured pointer tests - pushes out generic events
10157 // FINGER 0 DOWN
10158 processId(mapper, 3);
10159 processPosition(mapper, 100, 100);
10160 processKey(mapper, BTN_TOUCH, 1);
10161 processSync(mapper);
10162
10163 // start at (100,100), cursor should be at (0,0) * scale
10164 NotifyMotionArgs args;
10165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10166 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10167 ASSERT_NO_FATAL_FAILURE(
10168 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10169
10170 // FINGER 0 MOVE
10171 processPosition(mapper, 200, 200);
10172 processSync(mapper);
10173
10174 // compute scaling to help with touch position checking
10175 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10176 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10177 float scale =
10178 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10179
10180 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10182 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10183 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10184 0, 0, 0, 0, 0, 0, 0));
10185}
10186
10187TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10188 std::shared_ptr<FakePointerController> fakePointerController =
10189 std::make_shared<FakePointerController>();
10190
10191 prepareDisplay(DISPLAY_ORIENTATION_0);
10192 prepareAxes(POSITION | ID | SLOT);
10193 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010194 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010195 mFakePolicy->setPointerCapture(false);
10196 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10197
10198 // uncaptured touchpad should be a pointer device
10199 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
10200
10201 // captured touchpad should be a touchpad device
10202 mFakePolicy->setPointerCapture(true);
10203 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10204 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10205}
10206
HQ Liue6983c72022-04-19 22:14:56 +000010207class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10208protected:
10209 float mPointerMovementScale;
10210 float mPointerXZoomScale;
10211 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10212 addConfigurationProperty("touch.deviceType", "pointer");
10213 std::shared_ptr<FakePointerController> fakePointerController =
10214 std::make_shared<FakePointerController>();
10215 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10216 fakePointerController->setPosition(0, 0);
10217 fakePointerController->setButtonState(0);
10218 prepareDisplay(DISPLAY_ORIENTATION_0);
10219
10220 prepareAxes(POSITION);
10221 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10222 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10223 // needs to be disabled, and the pointer gesture needs to be enabled.
10224 mFakePolicy->setPointerCapture(false);
10225 mFakePolicy->setPointerGestureEnabled(true);
10226 mFakePolicy->setPointerController(fakePointerController);
10227
10228 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10229 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10230 mPointerMovementScale =
10231 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10232 mPointerXZoomScale =
10233 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10234 }
10235
10236 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10237 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10238 /*flat*/ 0,
10239 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10240 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10241 /*flat*/ 0,
10242 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10243 }
10244};
10245
10246/**
10247 * Two fingers down on a pointer mode touch pad. The width
10248 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10249 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10250 * be greater than the both value to be freeform gesture, so that after two
10251 * fingers start to move downwards, the gesture should be swipe.
10252 */
10253TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10254 // The min freeform gesture width is 25units/mm x 30mm = 750
10255 // which is greater than fraction of the diagnal length of the touchpad (349).
10256 // Thus, MaxSwipWidth is 750.
10257 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10258 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10259 NotifyMotionArgs motionArgs;
10260
10261 // Two fingers down at once.
10262 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10263 // Pointer's initial position is used the [0,0] coordinate.
10264 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10265
10266 processId(mapper, FIRST_TRACKING_ID);
10267 processPosition(mapper, x1, y1);
10268 processMTSync(mapper);
10269 processId(mapper, SECOND_TRACKING_ID);
10270 processPosition(mapper, x2, y2);
10271 processMTSync(mapper);
10272 processSync(mapper);
10273
10274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10275 ASSERT_EQ(1U, motionArgs.pointerCount);
10276 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10277 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010278 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010279 ASSERT_NO_FATAL_FAILURE(
10280 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10281
10282 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10283 // that there should be 1 pointer.
10284 int32_t movingDistance = 200;
10285 y1 += movingDistance;
10286 y2 += movingDistance;
10287
10288 processId(mapper, FIRST_TRACKING_ID);
10289 processPosition(mapper, x1, y1);
10290 processMTSync(mapper);
10291 processId(mapper, SECOND_TRACKING_ID);
10292 processPosition(mapper, x2, y2);
10293 processMTSync(mapper);
10294 processSync(mapper);
10295
10296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10297 ASSERT_EQ(1U, motionArgs.pointerCount);
10298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10299 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010300 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10302 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10303 0, 0, 0, 0));
10304}
10305
10306/**
10307 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10308 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10309 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10310 * value to be freeform gesture, so that after two fingers start to move downwards,
10311 * the gesture should be swipe.
10312 */
10313TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10314 // The min freeform gesture width is 5units/mm x 30mm = 150
10315 // which is greater than fraction of the diagnal length of the touchpad (349).
10316 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
10317 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
10318 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10319 NotifyMotionArgs motionArgs;
10320
10321 // Two fingers down at once.
10322 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10323 // Pointer's initial position is used the [0,0] coordinate.
10324 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10325
10326 processId(mapper, FIRST_TRACKING_ID);
10327 processPosition(mapper, x1, y1);
10328 processMTSync(mapper);
10329 processId(mapper, SECOND_TRACKING_ID);
10330 processPosition(mapper, x2, y2);
10331 processMTSync(mapper);
10332 processSync(mapper);
10333
10334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10335 ASSERT_EQ(1U, motionArgs.pointerCount);
10336 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10337 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010338 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010339 ASSERT_NO_FATAL_FAILURE(
10340 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10341
10342 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10343 // and there should be 1 pointer.
10344 int32_t movingDistance = 200;
10345 y1 += movingDistance;
10346 y2 += movingDistance;
10347
10348 processId(mapper, FIRST_TRACKING_ID);
10349 processPosition(mapper, x1, y1);
10350 processMTSync(mapper);
10351 processId(mapper, SECOND_TRACKING_ID);
10352 processPosition(mapper, x2, y2);
10353 processMTSync(mapper);
10354 processSync(mapper);
10355
10356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10357 ASSERT_EQ(1U, motionArgs.pointerCount);
10358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10359 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010360 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010361 // New coordinate is the scaled relative coordinate from the initial coordinate.
10362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10363 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10364 0, 0, 0, 0));
10365}
10366
10367/**
10368 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10369 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10370 * freeform gestures after two fingers start to move downwards.
10371 */
10372TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
10373 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10374 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10375
10376 NotifyMotionArgs motionArgs;
10377
10378 // Two fingers down at once. Wider than the max swipe width.
10379 // The gesture is expected to be PRESS, then transformed to FREEFORM
10380 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10381
10382 processId(mapper, FIRST_TRACKING_ID);
10383 processPosition(mapper, x1, y1);
10384 processMTSync(mapper);
10385 processId(mapper, SECOND_TRACKING_ID);
10386 processPosition(mapper, x2, y2);
10387 processMTSync(mapper);
10388 processSync(mapper);
10389
10390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10391 ASSERT_EQ(1U, motionArgs.pointerCount);
10392 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010394 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010395 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10396 ASSERT_NO_FATAL_FAILURE(
10397 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10398
10399 int32_t movingDistance = 200;
10400
10401 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10402 // then two down events for two pointers.
10403 y1 += movingDistance;
10404 y2 += movingDistance;
10405
10406 processId(mapper, FIRST_TRACKING_ID);
10407 processPosition(mapper, x1, y1);
10408 processMTSync(mapper);
10409 processId(mapper, SECOND_TRACKING_ID);
10410 processPosition(mapper, x2, y2);
10411 processMTSync(mapper);
10412 processSync(mapper);
10413
10414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10415 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10416 ASSERT_EQ(1U, motionArgs.pointerCount);
10417 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10420 ASSERT_EQ(1U, motionArgs.pointerCount);
10421 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10423 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010424 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010425 ASSERT_EQ(2U, motionArgs.pointerCount);
10426 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10427 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010428 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010429 // Two pointers' scaled relative coordinates from their initial centroid.
10430 // Initial y coordinates are 0 as y1 and y2 have the same value.
10431 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10432 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10433 // When pointers move, the new coordinates equal to the initial coordinates plus
10434 // scaled moving distance.
10435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10436 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10437 0, 0, 0, 0));
10438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10439 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10440 0, 0, 0, 0));
10441
10442 // Move two fingers down again, expect one MOVE motion event.
10443 y1 += movingDistance;
10444 y2 += movingDistance;
10445
10446 processId(mapper, FIRST_TRACKING_ID);
10447 processPosition(mapper, x1, y1);
10448 processMTSync(mapper);
10449 processId(mapper, SECOND_TRACKING_ID);
10450 processPosition(mapper, x2, y2);
10451 processMTSync(mapper);
10452 processSync(mapper);
10453
10454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10455 ASSERT_EQ(2U, motionArgs.pointerCount);
10456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10457 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010458 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010459 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10460 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10461 0, 0, 0, 0, 0));
10462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10463 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10464 0, 0, 0, 0, 0));
10465}
10466
Harry Cutts39b7ca22022-10-05 15:55:48 +000010467TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
10468 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10469 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10470 NotifyMotionArgs motionArgs;
10471
10472 // Place two fingers down.
10473 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10474
10475 processId(mapper, FIRST_TRACKING_ID);
10476 processPosition(mapper, x1, y1);
10477 processMTSync(mapper);
10478 processId(mapper, SECOND_TRACKING_ID);
10479 processPosition(mapper, x2, y2);
10480 processMTSync(mapper);
10481 processSync(mapper);
10482
10483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10484 ASSERT_EQ(1U, motionArgs.pointerCount);
10485 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10486 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10487 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10488 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10489
10490 // Move the two fingers down and to the left.
10491 int32_t movingDistance = 200;
10492 x1 -= movingDistance;
10493 y1 += movingDistance;
10494 x2 -= movingDistance;
10495 y2 += movingDistance;
10496
10497 processId(mapper, FIRST_TRACKING_ID);
10498 processPosition(mapper, x1, y1);
10499 processMTSync(mapper);
10500 processId(mapper, SECOND_TRACKING_ID);
10501 processPosition(mapper, x2, y2);
10502 processMTSync(mapper);
10503 processSync(mapper);
10504
10505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10506 ASSERT_EQ(1U, motionArgs.pointerCount);
10507 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10508 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10509 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10510 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10511}
10512
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010513// --- JoystickInputMapperTest ---
10514
10515class JoystickInputMapperTest : public InputMapperTest {
10516protected:
10517 static const int32_t RAW_X_MIN;
10518 static const int32_t RAW_X_MAX;
10519 static const int32_t RAW_Y_MIN;
10520 static const int32_t RAW_Y_MAX;
10521
10522 void SetUp() override {
10523 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10524 }
10525 void prepareAxes() {
10526 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10527 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10528 }
10529
10530 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10531 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10532 }
10533
10534 void processSync(JoystickInputMapper& mapper) {
10535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10536 }
10537
10538 void prepareVirtualDisplay(int32_t orientation) {
10539 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10540 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10541 NO_PORT, ViewportType::VIRTUAL);
10542 }
10543};
10544
10545const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10546const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10547const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10548const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10549
10550TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10551 prepareAxes();
10552 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10553
10554 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10555
10556 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
10557
10558 // Send an axis event
10559 processAxis(mapper, ABS_X, 100);
10560 processSync(mapper);
10561
10562 NotifyMotionArgs args;
10563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10564 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10565
10566 // Send another axis event
10567 processAxis(mapper, ABS_Y, 100);
10568 processSync(mapper);
10569
10570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10571 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10572}
10573
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010574// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010575
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010576class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010577protected:
10578 static const char* DEVICE_NAME;
10579 static const char* DEVICE_LOCATION;
10580 static const int32_t DEVICE_ID;
10581 static const int32_t DEVICE_GENERATION;
10582 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010583 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010584 static const int32_t EVENTHUB_ID;
10585
10586 std::shared_ptr<FakeEventHub> mFakeEventHub;
10587 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010588 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010589 std::unique_ptr<InstrumentedInputReader> mReader;
10590 std::shared_ptr<InputDevice> mDevice;
10591
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010592 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010593 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010594 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010595 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010596 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010597 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010598 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10599 }
10600
10601 void SetUp() override { SetUp(DEVICE_CLASSES); }
10602
10603 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010604 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010605 mFakePolicy.clear();
10606 }
10607
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010608 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010609 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10610 mReader->requestRefreshConfiguration(changes);
10611 mReader->loopOnce();
10612 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010613 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010614 }
10615
10616 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10617 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010618 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010619 InputDeviceIdentifier identifier;
10620 identifier.name = name;
10621 identifier.location = location;
10622 std::shared_ptr<InputDevice> device =
10623 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10624 identifier);
10625 mReader->pushNextDevice(device);
10626 mFakeEventHub->addDevice(eventHubId, name, classes);
10627 mReader->loopOnce();
10628 return device;
10629 }
10630
10631 template <class T, typename... Args>
10632 T& addControllerAndConfigure(Args... args) {
10633 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10634
10635 return controller;
10636 }
10637};
10638
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010639const char* PeripheralControllerTest::DEVICE_NAME = "device";
10640const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10641const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10642const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10643const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010644const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10645 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010646const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010647
10648// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010649class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010650protected:
10651 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010652 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010653 }
10654};
10655
10656TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010657 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010658
10659 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
10660 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
10661}
10662
10663TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010664 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010665
10666 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
10667 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
10668}
10669
10670// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010671class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010672protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010673 void SetUp() override {
10674 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10675 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010676};
10677
Chris Ye85758332021-05-16 23:05:17 -070010678TEST_F(LightControllerTest, MonoLight) {
10679 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010680 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010681 .maxBrightness = 255,
10682 .flags = InputLightClass::BRIGHTNESS,
10683 .path = ""};
10684 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010685
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010686 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010687 InputDeviceInfo info;
10688 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010689 std::vector<InputDeviceLightInfo> lights = info.getLights();
10690 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010691 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10692 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10693
10694 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10695 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10696}
10697
10698TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10699 RawLightInfo infoMono = {.id = 1,
10700 .name = "mono_keyboard_backlight",
10701 .maxBrightness = 255,
10702 .flags = InputLightClass::BRIGHTNESS |
10703 InputLightClass::KEYBOARD_BACKLIGHT,
10704 .path = ""};
10705 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10706
10707 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10708 InputDeviceInfo info;
10709 controller.populateDeviceInfo(&info);
10710 std::vector<InputDeviceLightInfo> lights = info.getLights();
10711 ASSERT_EQ(1U, lights.size());
10712 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10713 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010714
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010715 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10716 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010717}
10718
10719TEST_F(LightControllerTest, RGBLight) {
10720 RawLightInfo infoRed = {.id = 1,
10721 .name = "red",
10722 .maxBrightness = 255,
10723 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10724 .path = ""};
10725 RawLightInfo infoGreen = {.id = 2,
10726 .name = "green",
10727 .maxBrightness = 255,
10728 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10729 .path = ""};
10730 RawLightInfo infoBlue = {.id = 3,
10731 .name = "blue",
10732 .maxBrightness = 255,
10733 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10734 .path = ""};
10735 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10736 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10737 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10738
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010739 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010740 InputDeviceInfo info;
10741 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010742 std::vector<InputDeviceLightInfo> lights = info.getLights();
10743 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010744 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10745 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10746 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10747
10748 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10749 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10750}
10751
10752TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10753 RawLightInfo infoRed = {.id = 1,
10754 .name = "red_keyboard_backlight",
10755 .maxBrightness = 255,
10756 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10757 InputLightClass::KEYBOARD_BACKLIGHT,
10758 .path = ""};
10759 RawLightInfo infoGreen = {.id = 2,
10760 .name = "green_keyboard_backlight",
10761 .maxBrightness = 255,
10762 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10763 InputLightClass::KEYBOARD_BACKLIGHT,
10764 .path = ""};
10765 RawLightInfo infoBlue = {.id = 3,
10766 .name = "blue_keyboard_backlight",
10767 .maxBrightness = 255,
10768 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10769 InputLightClass::KEYBOARD_BACKLIGHT,
10770 .path = ""};
10771 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10772 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10773 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10774
10775 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10776 InputDeviceInfo info;
10777 controller.populateDeviceInfo(&info);
10778 std::vector<InputDeviceLightInfo> lights = info.getLights();
10779 ASSERT_EQ(1U, lights.size());
10780 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10781 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10782 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10783
10784 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10785 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10786}
10787
10788TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10789 RawLightInfo infoRed = {.id = 1,
10790 .name = "red",
10791 .maxBrightness = 255,
10792 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10793 .path = ""};
10794 RawLightInfo infoGreen = {.id = 2,
10795 .name = "green",
10796 .maxBrightness = 255,
10797 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10798 .path = ""};
10799 RawLightInfo infoBlue = {.id = 3,
10800 .name = "blue",
10801 .maxBrightness = 255,
10802 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10803 .path = ""};
10804 RawLightInfo infoGlobal = {.id = 3,
10805 .name = "global_keyboard_backlight",
10806 .maxBrightness = 255,
10807 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10808 InputLightClass::KEYBOARD_BACKLIGHT,
10809 .path = ""};
10810 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10811 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10812 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10813 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10814
10815 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10816 InputDeviceInfo info;
10817 controller.populateDeviceInfo(&info);
10818 std::vector<InputDeviceLightInfo> lights = info.getLights();
10819 ASSERT_EQ(1U, lights.size());
10820 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10821 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10822 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010823
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010824 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10825 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010826}
10827
10828TEST_F(LightControllerTest, MultiColorRGBLight) {
10829 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010830 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010831 .maxBrightness = 255,
10832 .flags = InputLightClass::BRIGHTNESS |
10833 InputLightClass::MULTI_INTENSITY |
10834 InputLightClass::MULTI_INDEX,
10835 .path = ""};
10836
10837 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10838
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010839 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010840 InputDeviceInfo info;
10841 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010842 std::vector<InputDeviceLightInfo> lights = info.getLights();
10843 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010844 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10845 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10846 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10847
10848 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10849 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10850}
10851
10852TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10853 RawLightInfo infoColor = {.id = 1,
10854 .name = "multi_color_keyboard_backlight",
10855 .maxBrightness = 255,
10856 .flags = InputLightClass::BRIGHTNESS |
10857 InputLightClass::MULTI_INTENSITY |
10858 InputLightClass::MULTI_INDEX |
10859 InputLightClass::KEYBOARD_BACKLIGHT,
10860 .path = ""};
10861
10862 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10863
10864 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10865 InputDeviceInfo info;
10866 controller.populateDeviceInfo(&info);
10867 std::vector<InputDeviceLightInfo> lights = info.getLights();
10868 ASSERT_EQ(1U, lights.size());
10869 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10870 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10871 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010872
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010873 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10874 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010875}
10876
10877TEST_F(LightControllerTest, PlayerIdLight) {
10878 RawLightInfo info1 = {.id = 1,
10879 .name = "player1",
10880 .maxBrightness = 255,
10881 .flags = InputLightClass::BRIGHTNESS,
10882 .path = ""};
10883 RawLightInfo info2 = {.id = 2,
10884 .name = "player2",
10885 .maxBrightness = 255,
10886 .flags = InputLightClass::BRIGHTNESS,
10887 .path = ""};
10888 RawLightInfo info3 = {.id = 3,
10889 .name = "player3",
10890 .maxBrightness = 255,
10891 .flags = InputLightClass::BRIGHTNESS,
10892 .path = ""};
10893 RawLightInfo info4 = {.id = 4,
10894 .name = "player4",
10895 .maxBrightness = 255,
10896 .flags = InputLightClass::BRIGHTNESS,
10897 .path = ""};
10898 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10899 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10900 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10901 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10902
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010903 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010904 InputDeviceInfo info;
10905 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010906 std::vector<InputDeviceLightInfo> lights = info.getLights();
10907 ASSERT_EQ(1U, lights.size());
10908 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010909 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10910 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010911
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010912 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10913 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10914 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010915}
10916
Michael Wrightd02c5b62014-02-10 15:10:22 -080010917} // namespace android