blob: fe36d42db76c4d1722081f39bff079f35eee463b [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;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00001346 std::optional<nsecs_t> mRequestedTimeout;
1347 std::vector<InputDeviceInfo> mExternalStylusDevices;
arthurhungdcef2dc2020-08-11 14:47:50 +08001348
1349 public:
1350 FakeInputReaderContext(InputReader* reader)
1351 : ContextImpl(reader),
1352 mGlobalMetaState(0),
1353 mUpdateGlobalMetaStateWasCalled(false),
1354 mGeneration(1) {}
1355
1356 virtual ~FakeInputReaderContext() {}
1357
1358 void assertUpdateGlobalMetaStateWasCalled() {
1359 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1360 << "Expected updateGlobalMetaState() to have been called.";
1361 mUpdateGlobalMetaStateWasCalled = false;
1362 }
1363
1364 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1365
1366 uint32_t getGeneration() { return mGeneration; }
1367
1368 void updateGlobalMetaState() override {
1369 mUpdateGlobalMetaStateWasCalled = true;
1370 ContextImpl::updateGlobalMetaState();
1371 }
1372
1373 int32_t getGlobalMetaState() override {
1374 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1375 }
1376
1377 int32_t bumpGeneration() override {
1378 mGeneration = ContextImpl::bumpGeneration();
1379 return mGeneration;
1380 }
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00001381
1382 void requestTimeoutAtTime(nsecs_t when) override { mRequestedTimeout = when; }
1383
1384 void assertTimeoutWasRequested(nsecs_t when) {
1385 ASSERT_TRUE(mRequestedTimeout) << "Expected timeout at time " << when
1386 << " but there was no timeout requested.";
1387 ASSERT_EQ(when, *mRequestedTimeout);
1388 mRequestedTimeout.reset();
1389 }
1390
1391 void assertTimeoutWasNotRequested() {
1392 ASSERT_FALSE(mRequestedTimeout) << "Expected no timeout to have been requested,"
1393 " but one was requested at time "
1394 << *mRequestedTimeout;
1395 }
1396
1397 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override {
1398 outDevices = mExternalStylusDevices;
1399 }
1400
1401 void setExternalStylusDevices(std::vector<InputDeviceInfo>&& devices) {
1402 mExternalStylusDevices = devices;
1403 }
arthurhungdcef2dc2020-08-11 14:47:50 +08001404 } mFakeContext;
1405
Michael Wrightd02c5b62014-02-10 15:10:22 -08001406 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001407
1408public:
1409 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001410};
1411
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001412// --- InputReaderPolicyTest ---
1413class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001414protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001415 sp<FakeInputReaderPolicy> mFakePolicy;
1416
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001417 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001418 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001419};
1420
1421/**
1422 * Check that empty set of viewports is an acceptable configuration.
1423 * Also try to get internal viewport two different ways - by type and by uniqueId.
1424 *
1425 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1426 * Such configuration is not currently allowed.
1427 */
1428TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001429 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001430
1431 // We didn't add any viewports yet, so there shouldn't be any.
1432 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001433 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001434 ASSERT_FALSE(internalViewport);
1435
1436 // Add an internal viewport, then clear it
1437 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001438 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001439 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001440
1441 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001442 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001443 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001444 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001445
1446 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001447 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001448 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001449 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001450
1451 mFakePolicy->clearViewports();
1452 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001453 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001454 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001455 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001456 ASSERT_FALSE(internalViewport);
1457}
1458
1459TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1460 const std::string internalUniqueId = "local:0";
1461 const std::string externalUniqueId = "local:1";
1462 const std::string virtualUniqueId1 = "virtual:2";
1463 const std::string virtualUniqueId2 = "virtual:3";
1464 constexpr int32_t virtualDisplayId1 = 2;
1465 constexpr int32_t virtualDisplayId2 = 3;
1466
1467 // Add an internal viewport
1468 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001469 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1470 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001471 // Add an external viewport
1472 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001473 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1474 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001475 // Add an virtual viewport
1476 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001477 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1478 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001479 // Add another virtual viewport
1480 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001481 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1482 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001483
1484 // Check matching by type for internal
1485 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001486 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001487 ASSERT_TRUE(internalViewport);
1488 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1489
1490 // Check matching by type for external
1491 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001492 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001493 ASSERT_TRUE(externalViewport);
1494 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1495
1496 // Check matching by uniqueId for virtual viewport #1
1497 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001498 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001499 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001500 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001501 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1502 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1503
1504 // Check matching by uniqueId for virtual viewport #2
1505 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001506 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001507 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001508 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001509 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1510 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1511}
1512
1513
1514/**
1515 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1516 * that lookup works by checking display id.
1517 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1518 */
1519TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1520 const std::string uniqueId1 = "uniqueId1";
1521 const std::string uniqueId2 = "uniqueId2";
1522 constexpr int32_t displayId1 = 2;
1523 constexpr int32_t displayId2 = 3;
1524
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001525 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1526 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001527 for (const ViewportType& type : types) {
1528 mFakePolicy->clearViewports();
1529 // Add a viewport
1530 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001531 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1532 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001533 // Add another viewport
1534 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001535 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1536 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001537
1538 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001539 std::optional<DisplayViewport> viewport1 =
1540 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001541 ASSERT_TRUE(viewport1);
1542 ASSERT_EQ(displayId1, viewport1->displayId);
1543 ASSERT_EQ(type, viewport1->type);
1544
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001545 std::optional<DisplayViewport> viewport2 =
1546 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001547 ASSERT_TRUE(viewport2);
1548 ASSERT_EQ(displayId2, viewport2->displayId);
1549 ASSERT_EQ(type, viewport2->type);
1550
1551 // When there are multiple viewports of the same kind, and uniqueId is not specified
1552 // in the call to getDisplayViewport, then that situation is not supported.
1553 // The viewports can be stored in any order, so we cannot rely on the order, since that
1554 // is just implementation detail.
1555 // However, we can check that it still returns *a* viewport, we just cannot assert
1556 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001557 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001558 ASSERT_TRUE(someViewport);
1559 }
1560}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001561
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001562/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001563 * When we have multiple internal displays make sure we always return the default display when
1564 * querying by type.
1565 */
1566TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1567 const std::string uniqueId1 = "uniqueId1";
1568 const std::string uniqueId2 = "uniqueId2";
1569 constexpr int32_t nonDefaultDisplayId = 2;
1570 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1571 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1572
1573 // Add the default display first and ensure it gets returned.
1574 mFakePolicy->clearViewports();
1575 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001576 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001577 ViewportType::INTERNAL);
1578 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001579 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001580 ViewportType::INTERNAL);
1581
1582 std::optional<DisplayViewport> viewport =
1583 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1584 ASSERT_TRUE(viewport);
1585 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1586 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1587
1588 // Add the default display second to make sure order doesn't matter.
1589 mFakePolicy->clearViewports();
1590 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001591 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001592 ViewportType::INTERNAL);
1593 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001594 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001595 ViewportType::INTERNAL);
1596
1597 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1598 ASSERT_TRUE(viewport);
1599 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1600 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1601}
1602
1603/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001604 * Check getDisplayViewportByPort
1605 */
1606TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001607 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001608 const std::string uniqueId1 = "uniqueId1";
1609 const std::string uniqueId2 = "uniqueId2";
1610 constexpr int32_t displayId1 = 1;
1611 constexpr int32_t displayId2 = 2;
1612 const uint8_t hdmi1 = 0;
1613 const uint8_t hdmi2 = 1;
1614 const uint8_t hdmi3 = 2;
1615
1616 mFakePolicy->clearViewports();
1617 // Add a viewport that's associated with some display port that's not of interest.
1618 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001619 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1620 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001621 // Add another viewport, connected to HDMI1 port
1622 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001623 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1624 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001625
1626 // Check that correct display viewport was returned by comparing the display ports.
1627 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1628 ASSERT_TRUE(hdmi1Viewport);
1629 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1630 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1631
1632 // Check that we can still get the same viewport using the uniqueId
1633 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1634 ASSERT_TRUE(hdmi1Viewport);
1635 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1636 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1637 ASSERT_EQ(type, hdmi1Viewport->type);
1638
1639 // Check that we cannot find a port with "HDMI2", because we never added one
1640 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1641 ASSERT_FALSE(hdmi2Viewport);
1642}
1643
Michael Wrightd02c5b62014-02-10 15:10:22 -08001644// --- InputReaderTest ---
1645
1646class InputReaderTest : public testing::Test {
1647protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001648 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001649 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001650 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001651 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001652
Chris Yea52ade12020-08-27 16:49:20 -07001653 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001654 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001655 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001656 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001657
Prabir Pradhan28efc192019-11-05 01:10:04 +00001658 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001659 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001660 }
1661
Chris Yea52ade12020-08-27 16:49:20 -07001662 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001663 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001664 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001665 }
1666
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001667 void addDevice(int32_t eventHubId, const std::string& name,
1668 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001669 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001670
1671 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001672 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001673 }
1674 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001675 mReader->loopOnce();
1676 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001677 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1678 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001679 }
1680
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001681 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001682 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001683 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001684 }
1685
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001686 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001687 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001688 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001689 }
1690
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001691 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001692 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001693 ftl::Flags<InputDeviceClass> classes,
1694 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001695 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001696 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1697 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001698 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001699 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001700 return mapper;
1701 }
1702};
1703
Chris Ye98d3f532020-10-01 21:48:59 -07001704TEST_F(InputReaderTest, PolicyGetInputDevices) {
1705 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001706 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001707 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001708
1709 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001710 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001711 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001712 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001713 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001714 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1715 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001716 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001717}
1718
Chris Yee7310032020-09-22 15:36:28 -07001719TEST_F(InputReaderTest, GetMergedInputDevices) {
1720 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1721 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1722 // Add two subdevices to device
1723 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1724 // Must add at least one mapper or the device will be ignored!
1725 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1726 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1727
1728 // Push same device instance for next device to be added, so they'll have same identifier.
1729 mReader->pushNextDevice(device);
1730 mReader->pushNextDevice(device);
1731 ASSERT_NO_FATAL_FAILURE(
1732 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1733 ASSERT_NO_FATAL_FAILURE(
1734 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1735
1736 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001737 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001738}
1739
Chris Yee14523a2020-12-19 13:46:00 -08001740TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1741 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1742 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1743 // Add two subdevices to device
1744 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1745 // Must add at least one mapper or the device will be ignored!
1746 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1747 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1748
1749 // Push same device instance for next device to be added, so they'll have same identifier.
1750 mReader->pushNextDevice(device);
1751 mReader->pushNextDevice(device);
1752 // Sensor device is initially disabled
1753 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1754 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1755 nullptr));
1756 // Device is disabled because the only sub device is a sensor device and disabled initially.
1757 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1758 ASSERT_FALSE(device->isEnabled());
1759 ASSERT_NO_FATAL_FAILURE(
1760 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1761 // The merged device is enabled if any sub device is enabled
1762 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1763 ASSERT_TRUE(device->isEnabled());
1764}
1765
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001766TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001767 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001768 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001769 constexpr int32_t eventHubId = 1;
1770 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001771 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001772 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001773 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001774 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001775
Yi Kong9b14ac62018-07-17 13:48:38 -07001776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001777
1778 NotifyDeviceResetArgs resetArgs;
1779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001780 ASSERT_EQ(deviceId, resetArgs.deviceId);
1781
1782 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001783 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001784 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001785
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001787 ASSERT_EQ(deviceId, resetArgs.deviceId);
1788 ASSERT_EQ(device->isEnabled(), false);
1789
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001790 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001791 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001794 ASSERT_EQ(device->isEnabled(), false);
1795
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001796 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001797 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001799 ASSERT_EQ(deviceId, resetArgs.deviceId);
1800 ASSERT_EQ(device->isEnabled(), true);
1801}
1802
Michael Wrightd02c5b62014-02-10 15:10:22 -08001803TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001804 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001805 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001806 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001807 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001808 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001809 AINPUT_SOURCE_KEYBOARD, nullptr);
1810 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001811
1812 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1813 AINPUT_SOURCE_ANY, AKEYCODE_A))
1814 << "Should return unknown when the device id is >= 0 but unknown.";
1815
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001816 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1817 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1818 << "Should return unknown when the device id is valid but the sources are not "
1819 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001820
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001821 ASSERT_EQ(AKEY_STATE_DOWN,
1822 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1823 AKEYCODE_A))
1824 << "Should return value provided by mapper when device id is valid and the device "
1825 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001826
1827 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1828 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1829 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1830
1831 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1832 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1833 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1834}
1835
Philip Junker4af3b3d2021-12-14 10:36:55 +01001836TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1837 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1838 constexpr int32_t eventHubId = 1;
1839 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1840 InputDeviceClass::KEYBOARD,
1841 AINPUT_SOURCE_KEYBOARD, nullptr);
1842 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1843
1844 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1845 << "Should return unknown when the device with the specified id is not found.";
1846
1847 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1848 << "Should return correct mapping when device id is valid and mapping exists.";
1849
1850 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1851 << "Should return the location key code when device id is valid and there's no "
1852 "mapping.";
1853}
1854
1855TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1856 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1857 constexpr int32_t eventHubId = 1;
1858 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1859 InputDeviceClass::JOYSTICK,
1860 AINPUT_SOURCE_GAMEPAD, nullptr);
1861 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1862
1863 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1864 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1865}
1866
Michael Wrightd02c5b62014-02-10 15:10:22 -08001867TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001868 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001869 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001870 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001871 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001872 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001873 AINPUT_SOURCE_KEYBOARD, nullptr);
1874 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001875
1876 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1877 AINPUT_SOURCE_ANY, KEY_A))
1878 << "Should return unknown when the device id is >= 0 but unknown.";
1879
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001880 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1881 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1882 << "Should return unknown when the device id is valid but the sources are not "
1883 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001884
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001885 ASSERT_EQ(AKEY_STATE_DOWN,
1886 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1887 KEY_A))
1888 << "Should return value provided by mapper when device id is valid and the device "
1889 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001890
1891 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1892 AINPUT_SOURCE_TRACKBALL, KEY_A))
1893 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1894
1895 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1896 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1897 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1898}
1899
1900TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001901 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001902 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001903 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001904 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001905 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001906 AINPUT_SOURCE_KEYBOARD, nullptr);
1907 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001908
1909 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1910 AINPUT_SOURCE_ANY, SW_LID))
1911 << "Should return unknown when the device id is >= 0 but unknown.";
1912
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001913 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1914 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1915 << "Should return unknown when the device id is valid but the sources are not "
1916 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001917
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001918 ASSERT_EQ(AKEY_STATE_DOWN,
1919 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1920 SW_LID))
1921 << "Should return value provided by mapper when device id is valid and the device "
1922 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001923
1924 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1925 AINPUT_SOURCE_TRACKBALL, SW_LID))
1926 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1927
1928 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1929 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1930 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1931}
1932
1933TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001934 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001935 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001936 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001937 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001938 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001939 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001940
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001941 mapper.addSupportedKeyCode(AKEYCODE_A);
1942 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001943
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001944 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001945 uint8_t flags[4] = { 0, 0, 0, 1 };
1946
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001947 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001948 << "Should return false when device id is >= 0 but unknown.";
1949 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1950
1951 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001952 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001953 << "Should return false when device id is valid but the sources are not supported by "
1954 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001955 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1956
1957 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001958 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001959 keyCodes, flags))
1960 << "Should return value provided by mapper when device id is valid and the device "
1961 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001962 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1963
1964 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001965 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1966 << "Should return false when the device id is < 0 but the sources are not supported by "
1967 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001968 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1969
1970 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001971 ASSERT_TRUE(
1972 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1973 << "Should return value provided by mapper when device id is < 0 and one of the "
1974 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001975 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1976}
1977
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001978TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001979 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001980 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001981
1982 NotifyConfigurationChangedArgs args;
1983
1984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1985 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1986}
1987
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001988TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001989 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001990 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001991 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001992 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001993 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001994 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001995 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001996 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001997
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001998 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001999 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002000 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
2001
2002 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002003 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002004 ASSERT_EQ(when, event.when);
2005 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002006 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002007 ASSERT_EQ(EV_KEY, event.type);
2008 ASSERT_EQ(KEY_A, event.code);
2009 ASSERT_EQ(1, event.value);
2010}
2011
Garfield Tan1c7bc862020-01-28 13:24:04 -08002012TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002013 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002014 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002015 constexpr int32_t eventHubId = 1;
2016 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08002017 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002018 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002019 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002020 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08002021
2022 NotifyDeviceResetArgs resetArgs;
2023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002024 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002025
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002026 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002027 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002029 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002030 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002031
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002032 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002033 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002035 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002036 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002037
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002038 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002039 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002041 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002042 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002043}
2044
Garfield Tan1c7bc862020-01-28 13:24:04 -08002045TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
2046 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002047 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002048 constexpr int32_t eventHubId = 1;
2049 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2050 // Must add at least one mapper or the device will be ignored!
2051 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002052 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002053 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
2054
2055 NotifyDeviceResetArgs resetArgs;
2056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2057 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
2058}
2059
Arthur Hungc23540e2018-11-29 20:42:11 +08002060TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002061 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002062 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002063 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08002064 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002065 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2066 FakeInputMapper& mapper =
2067 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002068 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002069
2070 const uint8_t hdmi1 = 1;
2071
2072 // Associated touch screen with second display.
2073 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2074
2075 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002076 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002077 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002078 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002079 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002080 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002081 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002082 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002083 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002084 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002085
2086 // Add the device, and make sure all of the callbacks are triggered.
2087 // The device is added after the input port associations are processed since
2088 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002089 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002092 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002093
Arthur Hung2c9a3342019-07-23 14:18:59 +08002094 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002095 ASSERT_EQ(deviceId, device->getId());
2096 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2097 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002098
2099 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002100 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002101 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002102 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002103}
2104
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002105TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2106 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002107 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002108 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2109 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2110 // Must add at least one mapper or the device will be ignored!
2111 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2112 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2113 mReader->pushNextDevice(device);
2114 mReader->pushNextDevice(device);
2115 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2116 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2117
2118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2119
2120 NotifyDeviceResetArgs resetArgs;
2121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2122 ASSERT_EQ(deviceId, resetArgs.deviceId);
2123 ASSERT_TRUE(device->isEnabled());
2124 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2125 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2126
2127 disableDevice(deviceId);
2128 mReader->loopOnce();
2129
2130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2131 ASSERT_EQ(deviceId, resetArgs.deviceId);
2132 ASSERT_FALSE(device->isEnabled());
2133 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2134 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2135
2136 enableDevice(deviceId);
2137 mReader->loopOnce();
2138
2139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2140 ASSERT_EQ(deviceId, resetArgs.deviceId);
2141 ASSERT_TRUE(device->isEnabled());
2142 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2143 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2144}
2145
2146TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2147 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002148 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002149 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2150 // Add two subdevices to device
2151 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2152 FakeInputMapper& mapperDevice1 =
2153 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2154 FakeInputMapper& mapperDevice2 =
2155 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2156 mReader->pushNextDevice(device);
2157 mReader->pushNextDevice(device);
2158 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2159 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2160
2161 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2162 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2163
2164 ASSERT_EQ(AKEY_STATE_DOWN,
2165 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2166 ASSERT_EQ(AKEY_STATE_DOWN,
2167 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2168 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2169 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2170}
2171
Prabir Pradhan7e186182020-11-10 13:56:45 -08002172TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2173 NotifyPointerCaptureChangedArgs args;
2174
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002175 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002176 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2177 mReader->loopOnce();
2178 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002179 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2180 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002181
2182 mFakePolicy->setPointerCapture(false);
2183 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2184 mReader->loopOnce();
2185 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002186 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002187
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002188 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002189 // does not change.
2190 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2191 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002192 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002193}
2194
Chris Ye87143712020-11-10 05:05:58 +00002195class FakeVibratorInputMapper : public FakeInputMapper {
2196public:
2197 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2198 : FakeInputMapper(deviceContext, sources) {}
2199
2200 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2201};
2202
2203TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2204 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002205 ftl::Flags<InputDeviceClass> deviceClass =
2206 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002207 constexpr int32_t eventHubId = 1;
2208 const char* DEVICE_LOCATION = "BLUETOOTH";
2209 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2210 FakeVibratorInputMapper& mapper =
2211 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2212 mReader->pushNextDevice(device);
2213
2214 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2215 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2216
2217 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2218 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2219}
2220
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002221// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002222
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002223class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002224public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002225 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002226
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002227 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002228
Andy Chenf9f1a022022-08-29 20:07:10 -04002229 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2230
Chris Yee2b1e5c2021-03-10 22:45:12 -08002231 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2232
2233 void dump(std::string& dump) override {}
2234
2235 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2236 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002237 }
2238
Chris Yee2b1e5c2021-03-10 22:45:12 -08002239 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2240 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002241 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002242
2243 bool setLightColor(int32_t lightId, int32_t color) override {
2244 getDeviceContext().setLightBrightness(lightId, color >> 24);
2245 return true;
2246 }
2247
2248 std::optional<int32_t> getLightColor(int32_t lightId) override {
2249 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2250 if (!result.has_value()) {
2251 return std::nullopt;
2252 }
2253 return result.value() << 24;
2254 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002255
2256 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2257
2258 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2259
2260private:
2261 InputDeviceContext& mDeviceContext;
2262 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2263 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04002264 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002265};
2266
Chris Yee2b1e5c2021-03-10 22:45:12 -08002267TEST_F(InputReaderTest, BatteryGetCapacity) {
2268 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002269 ftl::Flags<InputDeviceClass> deviceClass =
2270 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002271 constexpr int32_t eventHubId = 1;
2272 const char* DEVICE_LOCATION = "BLUETOOTH";
2273 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002274 FakePeripheralController& controller =
2275 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002276 mReader->pushNextDevice(device);
2277
2278 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2279
2280 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2281 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2282}
2283
2284TEST_F(InputReaderTest, BatteryGetStatus) {
2285 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002286 ftl::Flags<InputDeviceClass> deviceClass =
2287 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002288 constexpr int32_t eventHubId = 1;
2289 const char* DEVICE_LOCATION = "BLUETOOTH";
2290 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002291 FakePeripheralController& controller =
2292 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002293 mReader->pushNextDevice(device);
2294
2295 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2296
2297 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2298 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2299}
2300
Prabir Pradhane287ecd2022-09-07 21:18:05 +00002301TEST_F(InputReaderTest, BatteryGetDevicePath) {
2302 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2303 ftl::Flags<InputDeviceClass> deviceClass =
2304 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2305 constexpr int32_t eventHubId = 1;
2306 const char* DEVICE_LOCATION = "BLUETOOTH";
2307 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2308 device->addController<FakePeripheralController>(eventHubId);
2309 mReader->pushNextDevice(device);
2310
2311 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2312
2313 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), BATTERY_DEVPATH);
2314}
2315
Chris Ye3fdbfef2021-01-06 18:45:18 -08002316TEST_F(InputReaderTest, LightGetColor) {
2317 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002318 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002319 constexpr int32_t eventHubId = 1;
2320 const char* DEVICE_LOCATION = "BLUETOOTH";
2321 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002322 FakePeripheralController& controller =
2323 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002324 mReader->pushNextDevice(device);
2325 RawLightInfo info = {.id = 1,
2326 .name = "Mono",
2327 .maxBrightness = 255,
2328 .flags = InputLightClass::BRIGHTNESS,
2329 .path = ""};
2330 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2331 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2332
2333 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002334
Chris Yee2b1e5c2021-03-10 22:45:12 -08002335 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2336 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002337 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2338 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2339}
2340
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002341// --- InputReaderIntegrationTest ---
2342
2343// These tests create and interact with the InputReader only through its interface.
2344// The InputReader is started during SetUp(), which starts its processing in its own
2345// thread. The tests use linux uinput to emulate input devices.
2346// NOTE: Interacting with the physical device while these tests are running may cause
2347// the tests to fail.
2348class InputReaderIntegrationTest : public testing::Test {
2349protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002350 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002351 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002352 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002353
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002354 std::shared_ptr<FakePointerController> mFakePointerController;
2355
Chris Yea52ade12020-08-27 16:49:20 -07002356 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002357#if !defined(__ANDROID__)
2358 GTEST_SKIP();
2359#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002360 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002361 mFakePointerController = std::make_shared<FakePointerController>();
2362 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002363 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2364 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002365
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002366 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2367 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002368 ASSERT_EQ(mReader->start(), OK);
2369
2370 // Since this test is run on a real device, all the input devices connected
2371 // to the test device will show up in mReader. We wait for those input devices to
2372 // show up before beginning the tests.
2373 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2374 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2375 }
2376
Chris Yea52ade12020-08-27 16:49:20 -07002377 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002378#if !defined(__ANDROID__)
2379 return;
2380#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002381 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002382 mReader.reset();
2383 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002384 mFakePolicy.clear();
2385 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00002386
2387 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
2388 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
2389 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
2390 [&name](const InputDeviceInfo& info) {
2391 return info.getIdentifier().name == name;
2392 });
2393 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
2394 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002395};
2396
2397TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2398 // An invalid input device that is only used for this test.
2399 class InvalidUinputDevice : public UinputDevice {
2400 public:
Prabir Pradhanb7d434e2022-10-14 22:41:38 +00002401 InvalidUinputDevice() : UinputDevice("Invalid Device", 99 /*productId*/) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002402
2403 private:
2404 void configureDevice(int fd, uinput_user_dev* device) override {}
2405 };
2406
2407 const size_t numDevices = mFakePolicy->getInputDevices().size();
2408
2409 // UinputDevice does not set any event or key bits, so InputReader should not
2410 // consider it as a valid device.
2411 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2412 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2413 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2414 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2415
2416 invalidDevice.reset();
2417 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2418 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2419 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2420}
2421
2422TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2423 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2424
2425 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2426 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2427 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2428 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2429
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002430 const auto device = findDeviceByName(keyboard->getName());
2431 ASSERT_TRUE(device.has_value());
2432 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
2433 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
2434 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002435
2436 keyboard.reset();
2437 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2438 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2439 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2440}
2441
2442TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2443 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2444 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2445
2446 NotifyConfigurationChangedArgs configChangedArgs;
2447 ASSERT_NO_FATAL_FAILURE(
2448 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002449 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002450 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2451
2452 NotifyKeyArgs keyArgs;
2453 keyboard->pressAndReleaseHomeKey();
2454 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2455 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002456 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002457 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002458 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002459 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002460 prevTimestamp = keyArgs.eventTime;
2461
2462 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2463 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002464 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002465 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002466 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002467}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002468
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002469TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
2470 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2471 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2472
2473 const auto device = findDeviceByName(stylus->getName());
2474 ASSERT_TRUE(device.has_value());
2475
Prabir Pradhana3621852022-10-14 18:57:23 +00002476 // An external stylus with buttons should also be recognized as a keyboard.
2477 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002478 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
2479 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
2480
2481 const auto DOWN =
2482 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
2483 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
2484
2485 stylus->pressAndReleaseKey(BTN_STYLUS);
2486 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2487 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2488 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2489 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2490
2491 stylus->pressAndReleaseKey(BTN_STYLUS2);
2492 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2493 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
2494 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2495 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
2496
2497 stylus->pressAndReleaseKey(BTN_STYLUS3);
2498 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2499 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
2500 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2501 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
2502}
2503
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002504/**
2505 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2506 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2507 * are passed to the listener.
2508 */
2509static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2510TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2511 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2512 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2513 NotifyKeyArgs keyArgs;
2514
2515 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2516 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2517 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2518 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2519
2520 controller->pressAndReleaseKey(BTN_GEAR_UP);
2521 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2522 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2523 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2524}
2525
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002526// --- TouchIntegrationTest ---
2527
Arthur Hungaab25622020-01-16 11:22:11 +08002528class TouchIntegrationTest : public InputReaderIntegrationTest {
2529protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002530 const std::string UNIQUE_ID = "local:0";
2531
Chris Yea52ade12020-08-27 16:49:20 -07002532 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002533#if !defined(__ANDROID__)
2534 GTEST_SKIP();
2535#endif
Arthur Hungaab25622020-01-16 11:22:11 +08002536 InputReaderIntegrationTest::SetUp();
2537 // At least add an internal display.
2538 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2539 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002540 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002541
2542 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2543 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2544 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00002545 const auto info = findDeviceByName(mDevice->getName());
2546 ASSERT_TRUE(info);
2547 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08002548 }
2549
2550 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2551 int32_t orientation, const std::string& uniqueId,
2552 std::optional<uint8_t> physicalPort,
2553 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002554 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2555 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002556 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2557 }
2558
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002559 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2560 NotifyMotionArgs args;
2561 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2562 EXPECT_EQ(action, args.action);
2563 ASSERT_EQ(points.size(), args.pointerCount);
2564 for (size_t i = 0; i < args.pointerCount; i++) {
2565 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2566 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2567 }
2568 }
2569
Arthur Hungaab25622020-01-16 11:22:11 +08002570 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00002571 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08002572};
2573
2574TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2575 NotifyMotionArgs args;
2576 const Point centerPoint = mDevice->getCenterPoint();
2577
2578 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002579 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002580 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002581 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002582 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2583 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2584
2585 // ACTION_MOVE
2586 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002587 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002588 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2589 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2590
2591 // ACTION_UP
2592 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002593 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002594 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2595 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2596}
2597
2598TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2599 NotifyMotionArgs args;
2600 const Point centerPoint = mDevice->getCenterPoint();
2601
2602 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002603 mDevice->sendSlot(FIRST_SLOT);
2604 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002605 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002606 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002607 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2608 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2609
2610 // ACTION_POINTER_DOWN (Second slot)
2611 const Point secondPoint = centerPoint + Point(100, 100);
2612 mDevice->sendSlot(SECOND_SLOT);
2613 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002614 mDevice->sendDown(secondPoint);
2615 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002616 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002617 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002618
2619 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002620 mDevice->sendMove(secondPoint + Point(1, 1));
2621 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002622 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2624
2625 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002626 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002627 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002628 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002629 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002630
2631 // ACTION_UP
2632 mDevice->sendSlot(FIRST_SLOT);
2633 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002634 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002635 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2636 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2637}
2638
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002639/**
2640 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2641 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2642 * data?
2643 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2644 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2645 * for Pointer 0 only is generated after.
2646 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2647 * events, we will not miss any information.
2648 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2649 * event generated afterwards that contains the newest movement of pointer 0.
2650 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2651 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2652 * losing information about non-palm pointers.
2653 */
2654TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2655 NotifyMotionArgs args;
2656 const Point centerPoint = mDevice->getCenterPoint();
2657
2658 // ACTION_DOWN
2659 mDevice->sendSlot(FIRST_SLOT);
2660 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2661 mDevice->sendDown(centerPoint);
2662 mDevice->sendSync();
2663 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2664
2665 // ACTION_POINTER_DOWN (Second slot)
2666 const Point secondPoint = centerPoint + Point(100, 100);
2667 mDevice->sendSlot(SECOND_SLOT);
2668 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2669 mDevice->sendDown(secondPoint);
2670 mDevice->sendSync();
2671 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2672
2673 // ACTION_MOVE (First slot)
2674 mDevice->sendSlot(FIRST_SLOT);
2675 mDevice->sendMove(centerPoint + Point(5, 5));
2676 // ACTION_POINTER_UP (Second slot)
2677 mDevice->sendSlot(SECOND_SLOT);
2678 mDevice->sendPointerUp();
2679 // Send a single sync for the above 2 pointer updates
2680 mDevice->sendSync();
2681
2682 // First, we should get POINTER_UP for the second pointer
2683 assertReceivedMotion(ACTION_POINTER_1_UP,
2684 {/*first pointer */ centerPoint + Point(5, 5),
2685 /*second pointer*/ secondPoint});
2686
2687 // Next, the MOVE event for the first pointer
2688 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2689}
2690
2691/**
2692 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2693 * move, and then it will go up, all in the same frame.
2694 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2695 * gets sent to the listener.
2696 */
2697TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2698 NotifyMotionArgs args;
2699 const Point centerPoint = mDevice->getCenterPoint();
2700
2701 // ACTION_DOWN
2702 mDevice->sendSlot(FIRST_SLOT);
2703 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2704 mDevice->sendDown(centerPoint);
2705 mDevice->sendSync();
2706 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2707
2708 // ACTION_POINTER_DOWN (Second slot)
2709 const Point secondPoint = centerPoint + Point(100, 100);
2710 mDevice->sendSlot(SECOND_SLOT);
2711 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2712 mDevice->sendDown(secondPoint);
2713 mDevice->sendSync();
2714 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2715
2716 // ACTION_MOVE (First slot)
2717 mDevice->sendSlot(FIRST_SLOT);
2718 mDevice->sendMove(centerPoint + Point(5, 5));
2719 // ACTION_POINTER_UP (Second slot)
2720 mDevice->sendSlot(SECOND_SLOT);
2721 mDevice->sendMove(secondPoint + Point(6, 6));
2722 mDevice->sendPointerUp();
2723 // Send a single sync for the above 2 pointer updates
2724 mDevice->sendSync();
2725
2726 // First, we should get POINTER_UP for the second pointer
2727 // The movement of the second pointer during the liftoff frame is ignored.
2728 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2729 assertReceivedMotion(ACTION_POINTER_1_UP,
2730 {/*first pointer */ centerPoint + Point(5, 5),
2731 /*second pointer*/ secondPoint});
2732
2733 // Next, the MOVE event for the first pointer
2734 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2735}
2736
Arthur Hungaab25622020-01-16 11:22:11 +08002737TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2738 NotifyMotionArgs args;
2739 const Point centerPoint = mDevice->getCenterPoint();
2740
2741 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002742 mDevice->sendSlot(FIRST_SLOT);
2743 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002744 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002745 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002746 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2747 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2748
arthurhungcc7f9802020-04-30 17:55:40 +08002749 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002750 const Point secondPoint = centerPoint + Point(100, 100);
2751 mDevice->sendSlot(SECOND_SLOT);
2752 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2753 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002754 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002755 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002756 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002757
arthurhungcc7f9802020-04-30 17:55:40 +08002758 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002759 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002760 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002761 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2762 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2763
arthurhungcc7f9802020-04-30 17:55:40 +08002764 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2765 // a palm event.
2766 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002767 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002768 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002769 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002770 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002771 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002772
arthurhungcc7f9802020-04-30 17:55:40 +08002773 // Send up to second slot, expect first slot send moving.
2774 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002775 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002776 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2777 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002778
arthurhungcc7f9802020-04-30 17:55:40 +08002779 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002780 mDevice->sendSlot(FIRST_SLOT);
2781 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002782 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002783
arthurhungcc7f9802020-04-30 17:55:40 +08002784 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2785 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002786}
2787
Prabir Pradhanda20b172022-09-26 17:01:18 +00002788TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
2789 const Point centerPoint = mDevice->getCenterPoint();
2790
2791 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
2792 mDevice->sendSlot(FIRST_SLOT);
2793 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2794 mDevice->sendToolType(MT_TOOL_PEN);
2795 mDevice->sendDown(centerPoint);
2796 mDevice->sendSync();
2797 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2798 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2799 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2800
2801 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2802
2803 // Release the stylus touch.
2804 mDevice->sendUp();
2805 mDevice->sendSync();
2806 ASSERT_NO_FATAL_FAILURE(
2807 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2808
2809 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2810
2811 // Touch down with the finger, without the pen tool selected. The policy is not notified.
2812 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2813 mDevice->sendToolType(MT_TOOL_FINGER);
2814 mDevice->sendDown(centerPoint);
2815 mDevice->sendSync();
2816 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2817 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2818 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2819
2820 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2821
2822 mDevice->sendUp();
2823 mDevice->sendSync();
2824 ASSERT_NO_FATAL_FAILURE(
2825 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2826
2827 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
2828 // The policy should be notified of the stylus presence.
2829 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2830 mDevice->sendToolType(MT_TOOL_PEN);
2831 mDevice->sendMove(centerPoint);
2832 mDevice->sendSync();
2833 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2834 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2835 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2836
2837 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2838}
2839
Prabir Pradhan124ea442022-10-28 20:27:44 +00002840// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002841
Prabir Pradhan124ea442022-10-28 20:27:44 +00002842// Verify the behavior of button presses reported by various kinds of styluses, including buttons
2843// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
2844// stylus.
2845template <typename UinputStylusDevice>
2846class StylusButtonIntegrationTest : public TouchIntegrationTest {
2847protected:
2848 void SetUp() override {
2849#if !defined(__ANDROID__)
2850 GTEST_SKIP();
2851#endif
2852 TouchIntegrationTest::SetUp();
2853 mTouchscreen = mDevice.get();
2854 mTouchscreenInfo = mDeviceInfo;
2855
2856 setUpStylusDevice();
2857 }
2858
2859 UinputStylusDevice* mStylus{nullptr};
2860 InputDeviceInfo mStylusInfo{};
2861
2862 UinputTouchScreen* mTouchscreen{nullptr};
2863 InputDeviceInfo mTouchscreenInfo{};
2864
2865private:
2866 // When we are attempting to test stylus button events that are sent from the touchscreen,
2867 // use the same Uinput device for the touchscreen and the stylus.
2868 template <typename T = UinputStylusDevice>
2869 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2870 mStylus = mDevice.get();
2871 mStylusInfo = mDeviceInfo;
2872 }
2873
2874 // When we are attempting to stylus buttons from an external stylus being merged with touches
2875 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2876 template <typename T = UinputStylusDevice>
2877 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2878 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2879 mStylus = mStylusDeviceLifecycleTracker.get();
2880 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2881 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2882 const auto info = findDeviceByName(mStylus->getName());
2883 ASSERT_TRUE(info);
2884 mStylusInfo = *info;
2885 }
2886
2887 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2888
2889 // Hide the base class's device to expose it with a different name for readability.
2890 using TouchIntegrationTest::mDevice;
2891 using TouchIntegrationTest::mDeviceInfo;
2892};
2893
2894using StylusButtonIntegrationTestTypes =
2895 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2896TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2897
2898TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
2899 const auto stylusId = TestFixture::mStylusInfo.getId();
2900
2901 TestFixture::mStylus->pressKey(BTN_STYLUS);
2902 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2903 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2904 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2905
2906 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2907 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002908 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002909 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002910}
2911
Prabir Pradhan124ea442022-10-28 20:27:44 +00002912TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
2913 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2914 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2915 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002916
2917 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002918 TestFixture::mStylus->pressKey(BTN_STYLUS);
2919 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002920 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002921 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002922
2923 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002924 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2925 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2926 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2927 TestFixture::mTouchscreen->sendDown(centerPoint);
2928 TestFixture::mTouchscreen->sendSync();
2929 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002930 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2931 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002932 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2933 WithDeviceId(touchscreenId))));
2934 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002935 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2936 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002937 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2938 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002939
Prabir Pradhan124ea442022-10-28 20:27:44 +00002940 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2941 TestFixture::mTouchscreen->sendSync();
2942 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002943 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002944 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2945 WithDeviceId(touchscreenId))));
2946 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002947 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002948 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2949 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002950
2951 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002952 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2953 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002954 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002955 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002956}
2957
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002958TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
2959 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2960 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2961 const auto stylusId = TestFixture::mStylusInfo.getId();
2962 auto toolTypeDevice =
2963 AllOf(WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithDeviceId(touchscreenId));
2964
2965 // Press the stylus button.
2966 TestFixture::mStylus->pressKey(BTN_STYLUS);
2967 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2968 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2969 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2970
2971 // Start hovering with the stylus.
2972 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2973 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2974 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2975 TestFixture::mTouchscreen->sendMove(centerPoint);
2976 TestFixture::mTouchscreen->sendSync();
2977 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2978 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2979 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2980 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2981 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2982 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2983 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2984 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2985 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2986
2987 // Touch down with the stylus.
2988 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2989 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2990 TestFixture::mTouchscreen->sendDown(centerPoint);
2991 TestFixture::mTouchscreen->sendSync();
2992 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2993 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2994 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2995
2996 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2997 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2998 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2999
3000 // Stop touching with the stylus, and start hovering.
3001 TestFixture::mTouchscreen->sendUp();
3002 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
3003 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
3004 TestFixture::mTouchscreen->sendMove(centerPoint);
3005 TestFixture::mTouchscreen->sendSync();
3006 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3007 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
3008 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
3009 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3010 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3011 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
3012 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3013 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3014 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
3015
3016 // Stop hovering.
3017 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
3018 TestFixture::mTouchscreen->sendSync();
3019 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3020 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
3021 WithButtonState(0))));
3022 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
3023 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3024 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3025 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
3026
3027 // Release the stylus button.
3028 TestFixture::mStylus->releaseKey(BTN_STYLUS);
3029 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
3030 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
3031 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
3032}
3033
Prabir Pradhan124ea442022-10-28 20:27:44 +00003034TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
3035 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
3036 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
3037 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003038
3039 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00003040 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
3041 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
3042 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
3043 TestFixture::mTouchscreen->sendDown(centerPoint);
3044 TestFixture::mTouchscreen->sendSync();
3045 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003046 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003047 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3048 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003049
3050 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00003051 TestFixture::mStylus->pressKey(BTN_STYLUS);
3052 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003053 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003054 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
3055 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003056 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
3057 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003058 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
3059 WithDeviceId(touchscreenId))));
3060 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003061 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
3062 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003063 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
3064 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003065
Prabir Pradhan124ea442022-10-28 20:27:44 +00003066 TestFixture::mStylus->releaseKey(BTN_STYLUS);
3067 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003068 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003069 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
3070 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003071 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003072 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3073 WithDeviceId(touchscreenId))));
3074 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003075 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003076 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3077 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003078
3079 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00003080 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
3081 TestFixture::mTouchscreen->sendSync();
3082 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003083 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003084 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3085 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003086}
3087
Prabir Pradhan484d55a2022-10-14 23:17:16 +00003088// --- ExternalStylusIntegrationTest ---
3089
3090// Verify the behavior of an external stylus. An external stylus can report pressure or button
3091// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
3092// ongoing stylus gesture that is being emitted by the touchscreen.
3093using ExternalStylusIntegrationTest = TouchIntegrationTest;
3094
3095TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
3096 const Point centerPoint = mDevice->getCenterPoint();
3097
3098 // Create an external stylus capable of reporting pressure data that
3099 // should be fused with a touch pointer.
3100 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
3101 createUinputDevice<UinputExternalStylusWithPressure>();
3102 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
3103 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
3104 const auto stylusInfo = findDeviceByName(stylus->getName());
3105 ASSERT_TRUE(stylusInfo);
3106
3107 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
3108
3109 const auto touchscreenId = mDeviceInfo.getId();
3110
3111 // Set a pressure value on the stylus. It doesn't generate any events.
3112 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
3113 stylus->setPressure(100);
3114 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3115
3116 // Start a finger gesture, and ensure it shows up as stylus gesture
3117 // with the pressure set by the external stylus.
3118 mDevice->sendSlot(FIRST_SLOT);
3119 mDevice->sendTrackingId(FIRST_TRACKING_ID);
3120 mDevice->sendToolType(MT_TOOL_FINGER);
3121 mDevice->sendDown(centerPoint);
3122 mDevice->sendSync();
3123 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3124 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3125 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3126 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
3127
3128 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
3129 // event with the updated pressure.
3130 stylus->setPressure(200);
3131 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3132 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
3133 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3134 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
3135
3136 // The external stylus did not generate any events.
3137 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3138 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
3139}
3140
3141TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
3142 const Point centerPoint = mDevice->getCenterPoint();
3143
3144 // Create an external stylus capable of reporting pressure data that
3145 // should be fused with a touch pointer.
3146 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
3147 createUinputDevice<UinputExternalStylusWithPressure>();
3148 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
3149 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
3150 const auto stylusInfo = findDeviceByName(stylus->getName());
3151 ASSERT_TRUE(stylusInfo);
3152
3153 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
3154
3155 const auto touchscreenId = mDeviceInfo.getId();
3156
3157 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
3158 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
3159 stylus->setPressure(0);
3160
3161 // Start a finger gesture. The touch device will withhold generating any touches for
3162 // up to 72 milliseconds while waiting for pressure data from the external stylus.
3163 mDevice->sendSlot(FIRST_SLOT);
3164 mDevice->sendTrackingId(FIRST_TRACKING_ID);
3165 mDevice->sendToolType(MT_TOOL_FINGER);
3166 mDevice->sendDown(centerPoint);
3167 auto waitUntil = std::chrono::system_clock::now() +
3168 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
3169 mDevice->sendSync();
3170 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
3171
3172 // Since the external stylus did not report a pressure value within the timeout,
3173 // it shows up as a finger pointer.
3174 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3175 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3176 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithDeviceId(touchscreenId),
3177 WithPressure(1.f))));
3178
3179 // Change the pressure on the external stylus. Since the pressure was not present at the start
3180 // of the gesture, it is ignored for now.
3181 stylus->setPressure(200);
3182 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3183
3184 // Finish the finger gesture.
3185 mDevice->sendTrackingId(INVALID_TRACKING_ID);
3186 mDevice->sendSync();
3187 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3188 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3189 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
3190
3191 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
3192 mDevice->sendTrackingId(FIRST_TRACKING_ID);
3193 mDevice->sendToolType(MT_TOOL_FINGER);
3194 mDevice->sendDown(centerPoint);
3195 mDevice->sendSync();
3196 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3197 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3198 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3199 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
3200
3201 // The external stylus did not generate any events.
3202 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3203 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
3204}
3205
Michael Wrightd02c5b62014-02-10 15:10:22 -08003206// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08003207class InputDeviceTest : public testing::Test {
3208protected:
3209 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08003210 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003211 static const int32_t DEVICE_ID;
3212 static const int32_t DEVICE_GENERATION;
3213 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003214 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003215 static const int32_t EVENTHUB_ID;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003216 static const std::string DEVICE_BLUETOOTH_ADDRESS;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003217
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003218 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003219 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003220 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08003221 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00003222 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003223
Chris Yea52ade12020-08-27 16:49:20 -07003224 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003225 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003226 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003227 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08003228 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003229 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003230 InputDeviceIdentifier identifier;
3231 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08003232 identifier.location = DEVICE_LOCATION;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003233 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
arthurhungdcef2dc2020-08-11 14:47:50 +08003234 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003235 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08003236 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003237 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003238 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003239 }
3240
Chris Yea52ade12020-08-27 16:49:20 -07003241 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003242 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003243 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244 }
3245};
3246
3247const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003248const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003249const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003250const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
3251const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003252const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07003253 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003254const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003255const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
Michael Wrightd02c5b62014-02-10 15:10:22 -08003256
3257TEST_F(InputDeviceTest, ImmutableProperties) {
3258 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003259 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003260 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261}
3262
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00003263TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
3264 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
3265
3266 // Configuration
3267 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
3268 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003269 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00003270
3271 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
3272}
3273
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003274TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
3275 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07003276}
3277
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
3279 // Configuration.
3280 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003281 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282
3283 // Reset.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003284 unused += mDevice->reset(ARBITRARY_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285
3286 NotifyDeviceResetArgs resetArgs;
3287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3288 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3289 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3290
3291 // Metadata.
3292 ASSERT_TRUE(mDevice->isIgnored());
3293 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
3294
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00003295 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003296 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003297 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003298 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
3299 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
3300
3301 // State queries.
3302 ASSERT_EQ(0, mDevice->getMetaState());
3303
3304 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
3305 << "Ignored device should return unknown key code state.";
3306 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
3307 << "Ignored device should return unknown scan code state.";
3308 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
3309 << "Ignored device should return unknown switch state.";
3310
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003311 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003313 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314 << "Ignored device should never mark any key codes.";
3315 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
3316 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
3317}
3318
3319TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
3320 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003321 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08003322
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003323 FakeInputMapper& mapper1 =
3324 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003325 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3326 mapper1.setMetaState(AMETA_ALT_ON);
3327 mapper1.addSupportedKeyCode(AKEYCODE_A);
3328 mapper1.addSupportedKeyCode(AKEYCODE_B);
3329 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
3330 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
3331 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
3332 mapper1.setScanCodeState(3, AKEY_STATE_UP);
3333 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003334
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003335 FakeInputMapper& mapper2 =
3336 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003337 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338
3339 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003340 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003341
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003342 std::string propertyValue;
3343 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003344 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003345 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003347 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
3348 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003349
3350 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003351 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003352 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
3353 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003354
3355 NotifyDeviceResetArgs resetArgs;
3356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3357 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3358 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3359
3360 // Metadata.
3361 ASSERT_FALSE(mDevice->isIgnored());
3362 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
3363
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00003364 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003365 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003366 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003367 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
3368 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
3369
3370 // State queries.
3371 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
3372 << "Should query mappers and combine meta states.";
3373
3374 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3375 << "Should return unknown key code state when source not supported.";
3376 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3377 << "Should return unknown scan code state when source not supported.";
3378 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3379 << "Should return unknown switch state when source not supported.";
3380
3381 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
3382 << "Should query mapper when source is supported.";
3383 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
3384 << "Should query mapper when source is supported.";
3385 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
3386 << "Should query mapper when source is supported.";
3387
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003388 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08003389 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003390 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003391 << "Should do nothing when source is unsupported.";
3392 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
3393 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
3394 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
3395 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
3396
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003397 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003398 << "Should query mapper when source is supported.";
3399 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
3400 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
3401 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
3402 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
3403
3404 // Event handling.
3405 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003406 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003407 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003408
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003409 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
3410 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003411}
3412
Arthur Hung2c9a3342019-07-23 14:18:59 +08003413// A single input device is associated with a specific display. Check that:
3414// 1. Device is disabled if the viewport corresponding to the associated display is not found
3415// 2. Device is disabled when setEnabled API is called
3416TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003417 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003418
3419 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003420 std::list<NotifyArgs> unused =
3421 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003422
3423 // Device should be enabled by default.
3424 ASSERT_TRUE(mDevice->isEnabled());
3425
3426 // Prepare associated info.
3427 constexpr uint8_t hdmi = 1;
3428 const std::string UNIQUE_ID = "local:1";
3429
3430 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003431 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3432 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003433 // Device should be disabled because it is associated with a specific display via
3434 // input port <-> display port association, but the corresponding display is not found
3435 ASSERT_FALSE(mDevice->isEnabled());
3436
3437 // Prepare displays.
3438 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003439 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
3440 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003441 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3442 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003443 ASSERT_TRUE(mDevice->isEnabled());
3444
3445 // Device should be disabled after set disable.
3446 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003447 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3448 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003449 ASSERT_FALSE(mDevice->isEnabled());
3450
3451 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003452 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3453 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003454 ASSERT_FALSE(mDevice->isEnabled());
3455}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003456
Christine Franks1ba71cc2021-04-07 14:37:42 -07003457TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
3458 // Device should be enabled by default.
3459 mFakePolicy->clearViewports();
3460 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003461 std::list<NotifyArgs> unused =
3462 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003463 ASSERT_TRUE(mDevice->isEnabled());
3464
3465 // Device should be disabled because it is associated with a specific display, but the
3466 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08003467 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003468 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3469 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003470 ASSERT_FALSE(mDevice->isEnabled());
3471
3472 // Device should be enabled when a display is found.
3473 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3474 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3475 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003476 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3477 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003478 ASSERT_TRUE(mDevice->isEnabled());
3479
3480 // Device should be disabled after set disable.
3481 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003482 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3483 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003484 ASSERT_FALSE(mDevice->isEnabled());
3485
3486 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003487 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3488 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003489 ASSERT_FALSE(mDevice->isEnabled());
3490}
3491
Christine Franks2a2293c2022-01-18 11:51:16 -08003492TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
3493 mFakePolicy->clearViewports();
3494 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003495 std::list<NotifyArgs> unused =
3496 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08003497
Christine Franks2a2293c2022-01-18 11:51:16 -08003498 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
3499 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3500 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3501 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003502 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3503 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08003504 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
3505}
3506
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07003507/**
3508 * This test reproduces a crash caused by a dangling reference that remains after device is added
3509 * and removed. The reference is accessed in InputDevice::dump(..);
3510 */
3511TEST_F(InputDeviceTest, DumpDoesNotCrash) {
3512 constexpr int32_t TEST_EVENTHUB_ID = 10;
3513 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
3514
3515 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
3516 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
3517 device.removeEventHubDevice(TEST_EVENTHUB_ID);
3518 std::string dumpStr, eventHubDevStr;
3519 device.dump(dumpStr, eventHubDevStr);
3520}
3521
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003522TEST_F(InputDeviceTest, GetBluetoothAddress) {
3523 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
3524 ASSERT_TRUE(address);
3525 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
3526}
3527
Michael Wrightd02c5b62014-02-10 15:10:22 -08003528// --- InputMapperTest ---
3529
3530class InputMapperTest : public testing::Test {
3531protected:
3532 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003533 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003534 static const int32_t DEVICE_ID;
3535 static const int32_t DEVICE_GENERATION;
3536 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003537 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003538 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003539
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003540 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003541 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003542 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08003543 std::unique_ptr<InstrumentedInputReader> mReader;
3544 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003545
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003546 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003547 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003548 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003549 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08003550 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003551 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08003552 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003553 // Consume the device reset notification generated when adding a new device.
3554 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003555 }
3556
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003557 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003558 SetUp(DEVICE_CLASSES);
3559 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003560
Chris Yea52ade12020-08-27 16:49:20 -07003561 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003562 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003563 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003564 }
3565
3566 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003567 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003568 }
3569
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003570 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003571 if (!changes ||
3572 (changes &
3573 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3574 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003575 mReader->requestRefreshConfiguration(changes);
3576 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003577 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003578 std::list<NotifyArgs> out =
3579 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003580 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003581 for (const NotifyArgs& args : out) {
3582 mFakeListener->notify(args);
3583 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003584 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003585 return out;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003586 }
3587
arthurhungdcef2dc2020-08-11 14:47:50 +08003588 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3589 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003590 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003591 InputDeviceIdentifier identifier;
3592 identifier.name = name;
3593 identifier.location = location;
3594 std::shared_ptr<InputDevice> device =
3595 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3596 identifier);
3597 mReader->pushNextDevice(device);
3598 mFakeEventHub->addDevice(eventHubId, name, classes);
3599 mReader->loopOnce();
3600 return device;
3601 }
3602
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003603 template <class T, typename... Args>
3604 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003605 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003606 configureDevice(0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003607 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
3608 resetArgList += mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003609 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003610 for (const NotifyArgs& loopArgs : resetArgList) {
3611 mFakeListener->notify(loopArgs);
3612 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003613 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003614 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003615 }
3616
3617 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003618 int32_t orientation, const std::string& uniqueId,
3619 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003620 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3621 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003622 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3623 }
3624
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003625 void clearViewports() {
3626 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003627 }
3628
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003629 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
3630 int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003631 RawEvent event;
3632 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003633 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003634 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003635 event.type = type;
3636 event.code = code;
3637 event.value = value;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003638 std::list<NotifyArgs> processArgList = mapper.process(&event);
3639 for (const NotifyArgs& args : processArgList) {
3640 mFakeListener->notify(args);
3641 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003642 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003643 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003644 return processArgList;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003645 }
3646
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00003647 void resetMapper(InputMapper& mapper, nsecs_t when) {
3648 const auto resetArgs = mapper.reset(when);
3649 for (const auto args : resetArgs) {
3650 mFakeListener->notify(args);
3651 }
3652 // Loop the reader to flush the input listener queue.
3653 mReader->loopOnce();
3654 }
3655
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00003656 std::list<NotifyArgs> handleTimeout(InputMapper& mapper, nsecs_t when) {
3657 std::list<NotifyArgs> generatedArgs = mapper.timeoutExpired(when);
3658 for (const NotifyArgs& args : generatedArgs) {
3659 mFakeListener->notify(args);
3660 }
3661 // Loop the reader to flush the input listener queue.
3662 mReader->loopOnce();
3663 return generatedArgs;
3664 }
3665
Michael Wrightd02c5b62014-02-10 15:10:22 -08003666 static void assertMotionRange(const InputDeviceInfo& info,
3667 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3668 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003669 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003670 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3671 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3672 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3673 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3674 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3675 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3676 }
3677
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003678 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3679 float size, float touchMajor, float touchMinor, float toolMajor,
3680 float toolMinor, float orientation, float distance,
3681 float scaledAxisEpsilon = 1.f) {
3682 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3683 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003684 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3685 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003686 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3687 scaledAxisEpsilon);
3688 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3689 scaledAxisEpsilon);
3690 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3691 scaledAxisEpsilon);
3692 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3693 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003694 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3695 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3696 }
3697
Michael Wright17db18e2020-06-26 20:51:44 +01003698 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003699 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003700 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003701 ASSERT_NEAR(x, actualX, 1);
3702 ASSERT_NEAR(y, actualY, 1);
3703 }
3704};
3705
3706const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003707const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003708const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003709const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3710const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003711const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3712 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003713const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003714
3715// --- SwitchInputMapperTest ---
3716
3717class SwitchInputMapperTest : public InputMapperTest {
3718protected:
3719};
3720
3721TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003722 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003723
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003724 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003725}
3726
3727TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003728 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003729
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003730 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003731 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003732
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003733 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003734 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735}
3736
3737TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003738 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003739 std::list<NotifyArgs> out;
3740 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3741 ASSERT_TRUE(out.empty());
3742 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3743 ASSERT_TRUE(out.empty());
3744 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3745 ASSERT_TRUE(out.empty());
3746 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003747
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003748 ASSERT_EQ(1u, out.size());
3749 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003750 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003751 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3752 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003753 args.switchMask);
3754 ASSERT_EQ(uint32_t(0), args.policyFlags);
3755}
3756
Chris Ye87143712020-11-10 05:05:58 +00003757// --- VibratorInputMapperTest ---
3758class VibratorInputMapperTest : public InputMapperTest {
3759protected:
3760 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3761};
3762
3763TEST_F(VibratorInputMapperTest, GetSources) {
3764 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3765
3766 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3767}
3768
3769TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3770 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3771
3772 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3773}
3774
3775TEST_F(VibratorInputMapperTest, Vibrate) {
3776 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003777 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003778 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3779
3780 VibrationElement pattern(2);
3781 VibrationSequence sequence(2);
3782 pattern.duration = std::chrono::milliseconds(200);
3783 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3784 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3785 sequence.addElement(pattern);
3786 pattern.duration = std::chrono::milliseconds(500);
3787 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3788 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3789 sequence.addElement(pattern);
3790
3791 std::vector<int64_t> timings = {0, 1};
3792 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3793
3794 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003795 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003796 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003797 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003798 // Verify vibrator state listener was notified.
3799 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003800 ASSERT_EQ(1u, out.size());
3801 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3802 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3803 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003804 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003805 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003806 ASSERT_FALSE(mapper.isVibrating());
3807 // Verify vibrator state listener was notified.
3808 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003809 ASSERT_EQ(1u, out.size());
3810 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3811 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3812 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003813}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003814
Chris Yef59a2f42020-10-16 12:55:26 -07003815// --- SensorInputMapperTest ---
3816
3817class SensorInputMapperTest : public InputMapperTest {
3818protected:
3819 static const int32_t ACCEL_RAW_MIN;
3820 static const int32_t ACCEL_RAW_MAX;
3821 static const int32_t ACCEL_RAW_FUZZ;
3822 static const int32_t ACCEL_RAW_FLAT;
3823 static const int32_t ACCEL_RAW_RESOLUTION;
3824
3825 static const int32_t GYRO_RAW_MIN;
3826 static const int32_t GYRO_RAW_MAX;
3827 static const int32_t GYRO_RAW_FUZZ;
3828 static const int32_t GYRO_RAW_FLAT;
3829 static const int32_t GYRO_RAW_RESOLUTION;
3830
3831 static const float GRAVITY_MS2_UNIT;
3832 static const float DEGREE_RADIAN_UNIT;
3833
3834 void prepareAccelAxes();
3835 void prepareGyroAxes();
3836 void setAccelProperties();
3837 void setGyroProperties();
3838 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3839};
3840
3841const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3842const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3843const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3844const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3845const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3846
3847const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3848const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3849const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3850const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3851const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3852
3853const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3854const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3855
3856void SensorInputMapperTest::prepareAccelAxes() {
3857 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3858 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3859 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3860 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3861 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3862 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3863}
3864
3865void SensorInputMapperTest::prepareGyroAxes() {
3866 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3867 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3868 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3869 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3870 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3871 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3872}
3873
3874void SensorInputMapperTest::setAccelProperties() {
3875 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3876 /* sensorDataIndex */ 0);
3877 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3878 /* sensorDataIndex */ 1);
3879 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3880 /* sensorDataIndex */ 2);
3881 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3882 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3883 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3884 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3885 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3886}
3887
3888void SensorInputMapperTest::setGyroProperties() {
3889 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3890 /* sensorDataIndex */ 0);
3891 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3892 /* sensorDataIndex */ 1);
3893 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3894 /* sensorDataIndex */ 2);
3895 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3896 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3897 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3898 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3899 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3900}
3901
3902TEST_F(SensorInputMapperTest, GetSources) {
3903 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3904
3905 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3906}
3907
3908TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3909 setAccelProperties();
3910 prepareAccelAxes();
3911 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3912
3913 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3914 std::chrono::microseconds(10000),
3915 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003916 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003917 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3918 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3919 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3920 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3921 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003922
3923 NotifySensorArgs args;
3924 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3925 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3926 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3927
3928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3929 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3930 ASSERT_EQ(args.deviceId, DEVICE_ID);
3931 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3932 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3933 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3934 ASSERT_EQ(args.values, values);
3935 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3936}
3937
3938TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3939 setGyroProperties();
3940 prepareGyroAxes();
3941 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3942
3943 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3944 std::chrono::microseconds(10000),
3945 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003946 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3951 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003952
3953 NotifySensorArgs args;
3954 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3955 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3956 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3957
3958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3959 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3960 ASSERT_EQ(args.deviceId, DEVICE_ID);
3961 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3962 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3963 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3964 ASSERT_EQ(args.values, values);
3965 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3966}
3967
Michael Wrightd02c5b62014-02-10 15:10:22 -08003968// --- KeyboardInputMapperTest ---
3969
3970class KeyboardInputMapperTest : public InputMapperTest {
3971protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003972 const std::string UNIQUE_ID = "local:0";
3973
3974 void prepareDisplay(int32_t orientation);
3975
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003976 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003977 int32_t originalKeyCode, int32_t rotatedKeyCode,
3978 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003979};
3980
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003981/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3982 * orientation.
3983 */
3984void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003985 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3986 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003987}
3988
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003989void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003990 int32_t originalScanCode, int32_t originalKeyCode,
3991 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003992 NotifyKeyArgs args;
3993
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3996 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3997 ASSERT_EQ(originalScanCode, args.scanCode);
3998 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003999 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004000
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4003 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4004 ASSERT_EQ(originalScanCode, args.scanCode);
4005 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004006 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004007}
4008
Michael Wrightd02c5b62014-02-10 15:10:22 -08004009TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004010 KeyboardInputMapper& mapper =
4011 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4012 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004013
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004014 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004015}
4016
4017TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
4018 const int32_t USAGE_A = 0x070004;
4019 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004020 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4021 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07004022 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
4023 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
4024 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004025
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004026 KeyboardInputMapper& mapper =
4027 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4028 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004029 // Initial metastate is AMETA_NONE.
4030 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004031
4032 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004033 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004034 NotifyKeyArgs args;
4035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4036 ASSERT_EQ(DEVICE_ID, args.deviceId);
4037 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4038 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4039 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4040 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4041 ASSERT_EQ(KEY_HOME, args.scanCode);
4042 ASSERT_EQ(AMETA_NONE, args.metaState);
4043 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4044 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4045 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4046
4047 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004048 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4050 ASSERT_EQ(DEVICE_ID, args.deviceId);
4051 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4052 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4053 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4054 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4055 ASSERT_EQ(KEY_HOME, args.scanCode);
4056 ASSERT_EQ(AMETA_NONE, args.metaState);
4057 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4058 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4059 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4060
4061 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004062 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
4063 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4065 ASSERT_EQ(DEVICE_ID, args.deviceId);
4066 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4067 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4068 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4069 ASSERT_EQ(AKEYCODE_A, args.keyCode);
4070 ASSERT_EQ(0, args.scanCode);
4071 ASSERT_EQ(AMETA_NONE, args.metaState);
4072 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4073 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4074 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4075
4076 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
4078 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4080 ASSERT_EQ(DEVICE_ID, args.deviceId);
4081 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4082 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4083 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4084 ASSERT_EQ(AKEYCODE_A, args.keyCode);
4085 ASSERT_EQ(0, args.scanCode);
4086 ASSERT_EQ(AMETA_NONE, args.metaState);
4087 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4088 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4089 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4090
4091 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
4093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4095 ASSERT_EQ(DEVICE_ID, args.deviceId);
4096 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4097 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4098 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4099 ASSERT_EQ(0, args.keyCode);
4100 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
4101 ASSERT_EQ(AMETA_NONE, args.metaState);
4102 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4103 ASSERT_EQ(0U, args.policyFlags);
4104 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4105
4106 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
4108 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4110 ASSERT_EQ(DEVICE_ID, args.deviceId);
4111 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4112 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4113 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4114 ASSERT_EQ(0, args.keyCode);
4115 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
4116 ASSERT_EQ(AMETA_NONE, args.metaState);
4117 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4118 ASSERT_EQ(0U, args.policyFlags);
4119 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4120}
4121
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004122/**
4123 * Ensure that the readTime is set to the time when the EV_KEY is received.
4124 */
4125TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
4126 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4127
4128 KeyboardInputMapper& mapper =
4129 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4130 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4131 NotifyKeyArgs args;
4132
4133 // Key down
4134 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
4135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4136 ASSERT_EQ(12, args.readTime);
4137
4138 // Key up
4139 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
4140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4141 ASSERT_EQ(15, args.readTime);
4142}
4143
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004145 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
4146 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07004147 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
4148 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
4149 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004150
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004151 KeyboardInputMapper& mapper =
4152 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4153 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004154
Arthur Hung95f68612022-04-07 14:08:22 +08004155 // Initial metastate is AMETA_NONE.
4156 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157
4158 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004159 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160 NotifyKeyArgs args;
4161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4162 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004163 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08004164 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165
4166 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004167 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4169 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004170 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004171
4172 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004173 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4175 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004176 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177
4178 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004179 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4181 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004182 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08004183 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184}
4185
4186TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004187 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4188 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4189 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4190 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004192 KeyboardInputMapper& mapper =
4193 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4194 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004196 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004197 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4198 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
4199 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4200 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
4201 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4202 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
4203 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4204 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
4205}
4206
4207TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004208 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4209 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4210 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4211 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004214 KeyboardInputMapper& mapper =
4215 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4216 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004218 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004219 ASSERT_NO_FATAL_FAILURE(
4220 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4221 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4222 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4223 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4224 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4225 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4226 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004227
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004228 clearViewports();
4229 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004230 ASSERT_NO_FATAL_FAILURE(
4231 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4232 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4233 AKEYCODE_DPAD_UP, DISPLAY_ID));
4234 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4235 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4236 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4237 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004238
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004239 clearViewports();
4240 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004241 ASSERT_NO_FATAL_FAILURE(
4242 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4243 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4244 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4245 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4246 AKEYCODE_DPAD_UP, DISPLAY_ID));
4247 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4248 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004250 clearViewports();
4251 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004252 ASSERT_NO_FATAL_FAILURE(
4253 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4254 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4255 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4256 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4257 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4258 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4259 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260
4261 // Special case: if orientation changes while key is down, we still emit the same keycode
4262 // in the key up as we did in the key down.
4263 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004264 clearViewports();
4265 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004266 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4268 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4269 ASSERT_EQ(KEY_UP, args.scanCode);
4270 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
4271
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004272 clearViewports();
4273 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4276 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4277 ASSERT_EQ(KEY_UP, args.scanCode);
4278 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
4279}
4280
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004281TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
4282 // If the keyboard is not orientation aware,
4283 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004284 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004285
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004286 KeyboardInputMapper& mapper =
4287 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4288 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004289 NotifyKeyArgs args;
4290
4291 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4296 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
4297
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004298 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004299 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4303 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
4304}
4305
4306TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
4307 // If the keyboard is orientation aware,
4308 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004309 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004310
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004311 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004312 KeyboardInputMapper& mapper =
4313 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4314 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004315 NotifyKeyArgs args;
4316
4317 // Display id should be ADISPLAY_ID_NONE without any display configuration.
4318 // ^--- already checked by the previous test
4319
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004320 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004321 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004322 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004324 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4326 ASSERT_EQ(DISPLAY_ID, args.displayId);
4327
4328 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004329 clearViewports();
4330 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004331 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004334 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4336 ASSERT_EQ(newDisplayId, args.displayId);
4337}
4338
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004340 KeyboardInputMapper& mapper =
4341 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4342 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004343
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004344 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004345 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004346
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004347 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004348 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349}
4350
Philip Junker4af3b3d2021-12-14 10:36:55 +01004351TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
4352 KeyboardInputMapper& mapper =
4353 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4354 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4355
4356 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
4357 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
4358 << "If a mapping is available, the result is equal to the mapping";
4359
4360 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
4361 << "If no mapping is available, the result is the key location";
4362}
4363
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004365 KeyboardInputMapper& mapper =
4366 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4367 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004368
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004369 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004370 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004372 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004373 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374}
4375
4376TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004377 KeyboardInputMapper& mapper =
4378 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4379 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004381 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382
Michael Wrightd02c5b62014-02-10 15:10:22 -08004383 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004384 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004385 ASSERT_TRUE(flags[0]);
4386 ASSERT_FALSE(flags[1]);
4387}
4388
4389TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004390 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4391 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4392 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4393 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4394 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4395 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004396
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004397 KeyboardInputMapper& mapper =
4398 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4399 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004400 // Initial metastate is AMETA_NONE.
4401 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004402
4403 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004404 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4405 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4406 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407
4408 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004411 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4412 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4413 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004414 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004415
4416 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004417 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004419 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4420 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4421 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004422 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423
4424 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004427 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4428 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4429 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004430 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431
4432 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004433 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4434 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004435 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4436 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4437 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004438 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439
4440 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004441 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004443 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4444 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4445 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004446 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447
4448 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004451 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4452 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4453 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004454 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004455}
4456
Chris Yea52ade12020-08-27 16:49:20 -07004457TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
4458 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
4459 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
4460 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
4461 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
4462
4463 KeyboardInputMapper& mapper =
4464 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4465 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4466
Chris Yea52ade12020-08-27 16:49:20 -07004467 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004468 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07004469 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4470 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
4471 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
4472 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4473
4474 NotifyKeyArgs args;
4475 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07004477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4478 ASSERT_EQ(AMETA_NONE, args.metaState);
4479 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4480 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4481 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4482
4483 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004484 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07004485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4486 ASSERT_EQ(AMETA_NONE, args.metaState);
4487 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4488 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4489 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4490}
4491
Arthur Hung2c9a3342019-07-23 14:18:59 +08004492TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
4493 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004494 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4495 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4496 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4497 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004498
4499 // keyboard 2.
4500 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08004501 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08004502 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004503 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08004504 std::shared_ptr<InputDevice> device2 =
4505 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004506 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08004507
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004508 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4509 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4510 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4511 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004512
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004513 KeyboardInputMapper& mapper =
4514 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4515 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004516
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004517 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004518 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004519 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004520 std::list<NotifyArgs> unused =
4521 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4522 0 /*changes*/);
4523 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004524
4525 // Prepared displays and associated info.
4526 constexpr uint8_t hdmi1 = 0;
4527 constexpr uint8_t hdmi2 = 1;
4528 const std::string SECONDARY_UNIQUE_ID = "local:1";
4529
4530 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
4531 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
4532
4533 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004534 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4535 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004536 ASSERT_FALSE(device2->isEnabled());
4537
4538 // Prepare second display.
4539 constexpr int32_t newDisplayId = 2;
4540 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004541 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004542 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004543 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004544 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004545 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4546 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004547
4548 // Device should be enabled after the associated display is found.
4549 ASSERT_TRUE(mDevice->isEnabled());
4550 ASSERT_TRUE(device2->isEnabled());
4551
4552 // Test pad key events
4553 ASSERT_NO_FATAL_FAILURE(
4554 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4555 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4556 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4557 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4558 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4559 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4560 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4561
4562 ASSERT_NO_FATAL_FAILURE(
4563 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
4564 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4565 AKEYCODE_DPAD_RIGHT, newDisplayId));
4566 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4567 AKEYCODE_DPAD_DOWN, newDisplayId));
4568 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4569 AKEYCODE_DPAD_LEFT, newDisplayId));
4570}
Michael Wrightd02c5b62014-02-10 15:10:22 -08004571
arthurhungc903df12020-08-11 15:08:42 +08004572TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
4573 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4574 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4575 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4576 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4577 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4578 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4579
4580 KeyboardInputMapper& mapper =
4581 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4582 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004583 // Initial metastate is AMETA_NONE.
4584 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004585
4586 // Initialization should have turned all of the lights off.
4587 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4588 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4589 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4590
4591 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004594 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4595 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4596
4597 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004598 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4599 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004600 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4601 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
4602
4603 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004604 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4605 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004606 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4607 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4608
4609 mFakeEventHub->removeDevice(EVENTHUB_ID);
4610 mReader->loopOnce();
4611
4612 // keyboard 2 should default toggle keys.
4613 const std::string USB2 = "USB2";
4614 const std::string DEVICE_NAME2 = "KEYBOARD2";
4615 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4616 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4617 std::shared_ptr<InputDevice> device2 =
4618 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004619 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004620 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4621 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4622 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4623 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4624 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4625 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4626
arthurhung6fe95782020-10-05 22:41:16 +08004627 KeyboardInputMapper& mapper2 =
4628 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4629 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004630 std::list<NotifyArgs> unused =
4631 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4632 0 /*changes*/);
4633 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08004634
4635 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4636 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4637 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004638 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4639 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004640}
4641
Arthur Hungcb40a002021-08-03 14:31:01 +00004642TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4643 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4644 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4645 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4646
4647 // Suppose we have two mappers. (DPAD + KEYBOARD)
4648 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4649 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4650 KeyboardInputMapper& mapper =
4651 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4652 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004653 // Initial metastate is AMETA_NONE.
4654 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004655
4656 mReader->toggleCapsLockState(DEVICE_ID);
4657 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4658}
4659
Arthur Hungfb3cc112022-04-13 07:39:50 +00004660TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4661 // keyboard 1.
4662 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4663 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4664 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4665 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4666 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4667 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4668
4669 KeyboardInputMapper& mapper1 =
4670 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4671 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4672
4673 // keyboard 2.
4674 const std::string USB2 = "USB2";
4675 const std::string DEVICE_NAME2 = "KEYBOARD2";
4676 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4677 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4678 std::shared_ptr<InputDevice> device2 =
4679 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4680 ftl::Flags<InputDeviceClass>(0));
4681 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4682 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4683 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4684 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4685 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4686 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4687
4688 KeyboardInputMapper& mapper2 =
4689 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4690 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004691 std::list<NotifyArgs> unused =
4692 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4693 0 /*changes*/);
4694 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004695
Arthur Hung95f68612022-04-07 14:08:22 +08004696 // Initial metastate is AMETA_NONE.
4697 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4698 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4699
4700 // Toggle num lock on and off.
4701 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4702 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004703 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4704 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4705 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4706
4707 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4708 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4709 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4710 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4711 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4712
4713 // Toggle caps lock on and off.
4714 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4715 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4716 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4717 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4718 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4719
4720 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4721 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4722 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4723 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4724 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4725
4726 // Toggle scroll lock on and off.
4727 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4728 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4729 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4730 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4731 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4732
4733 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4734 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4735 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4736 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4737 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4738}
4739
Arthur Hung2141d542022-08-23 07:45:21 +00004740TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4741 const int32_t USAGE_A = 0x070004;
4742 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4743 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4744
4745 KeyboardInputMapper& mapper =
4746 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4747 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4748 // Key down by scan code.
4749 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4750 NotifyKeyArgs args;
4751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4752 ASSERT_EQ(DEVICE_ID, args.deviceId);
4753 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4754 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4755 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4756 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4757 ASSERT_EQ(KEY_HOME, args.scanCode);
4758 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4759
4760 // Disable device, it should synthesize cancellation events for down events.
4761 mFakePolicy->addDisabledDevice(DEVICE_ID);
4762 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4763
4764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4765 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4766 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4767 ASSERT_EQ(KEY_HOME, args.scanCode);
4768 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4769}
4770
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004771// --- KeyboardInputMapperTest_ExternalDevice ---
4772
4773class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4774protected:
Chris Yea52ade12020-08-27 16:49:20 -07004775 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004776};
4777
4778TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004779 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4780 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004781
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004782 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4783 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4784 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4785 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004786
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004787 KeyboardInputMapper& mapper =
4788 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4789 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004790
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004792 NotifyKeyArgs args;
4793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4794 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4795
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004796 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4798 ASSERT_EQ(uint32_t(0), args.policyFlags);
4799
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4802 ASSERT_EQ(uint32_t(0), args.policyFlags);
4803
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004804 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4806 ASSERT_EQ(uint32_t(0), args.policyFlags);
4807
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4810 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4811
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004812 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4814 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4815}
4816
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004817TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004818 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004819
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004820 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4821 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4822 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004823
Powei Fengd041c5d2019-05-03 17:11:33 -07004824 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004825 KeyboardInputMapper& mapper =
4826 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4827 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004828
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004830 NotifyKeyArgs args;
4831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4832 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4833
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004834 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4836 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4837
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004838 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4840 ASSERT_EQ(uint32_t(0), args.policyFlags);
4841
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004842 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4844 ASSERT_EQ(uint32_t(0), args.policyFlags);
4845
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004846 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4848 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4849
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004850 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4852 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4853}
4854
Michael Wrightd02c5b62014-02-10 15:10:22 -08004855// --- CursorInputMapperTest ---
4856
4857class CursorInputMapperTest : public InputMapperTest {
4858protected:
4859 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4860
Michael Wright17db18e2020-06-26 20:51:44 +01004861 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004862
Chris Yea52ade12020-08-27 16:49:20 -07004863 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004864 InputMapperTest::SetUp();
4865
Michael Wright17db18e2020-06-26 20:51:44 +01004866 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004867 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004868 }
4869
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004870 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4871 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004872
4873 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004874 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4875 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4876 }
4877
4878 void prepareSecondaryDisplay() {
4879 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4880 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4881 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004882 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004883
4884 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4885 float pressure) {
4886 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4887 0.0f, 0.0f, 0.0f, EPSILON));
4888 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004889};
4890
4891const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4892
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004893void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4894 int32_t originalY, int32_t rotatedX,
4895 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004896 NotifyMotionArgs args;
4897
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004898 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4902 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004903 ASSERT_NO_FATAL_FAILURE(
4904 assertCursorPointerCoords(args.pointerCoords[0],
4905 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4906 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004907}
4908
4909TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004910 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004911 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004913 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004914}
4915
4916TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004917 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004918 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004920 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004921}
4922
4923TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004925 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004926
4927 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004928 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004929
4930 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004931 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4932 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004933 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4934 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4935
4936 // When the bounds are set, then there should be a valid motion range.
4937 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4938
4939 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004940 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004941
4942 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4943 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4944 1, 800 - 1, 0.0f, 0.0f));
4945 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4946 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4947 2, 480 - 1, 0.0f, 0.0f));
4948 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4949 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4950 0.0f, 1.0f, 0.0f, 0.0f));
4951}
4952
4953TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004954 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004955 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004956
4957 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004958 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004959
4960 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4961 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4962 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4963 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4964 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4965 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4966 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4967 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4968 0.0f, 1.0f, 0.0f, 0.0f));
4969}
4970
4971TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004973 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974
arthurhungdcef2dc2020-08-11 14:47:50 +08004975 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976
4977 NotifyMotionArgs args;
4978
4979 // Button press.
4980 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004981 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4982 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4984 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4985 ASSERT_EQ(DEVICE_ID, args.deviceId);
4986 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4987 ASSERT_EQ(uint32_t(0), args.policyFlags);
4988 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4989 ASSERT_EQ(0, args.flags);
4990 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4991 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4992 ASSERT_EQ(0, args.edgeFlags);
4993 ASSERT_EQ(uint32_t(1), args.pointerCount);
4994 ASSERT_EQ(0, args.pointerProperties[0].id);
4995 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004996 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004997 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4998 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4999 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5000
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5002 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5003 ASSERT_EQ(DEVICE_ID, args.deviceId);
5004 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
5005 ASSERT_EQ(uint32_t(0), args.policyFlags);
5006 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
5007 ASSERT_EQ(0, args.flags);
5008 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5009 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
5010 ASSERT_EQ(0, args.edgeFlags);
5011 ASSERT_EQ(uint32_t(1), args.pointerCount);
5012 ASSERT_EQ(0, args.pointerProperties[0].id);
5013 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005014 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005015 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
5016 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
5017 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5018
Michael Wrightd02c5b62014-02-10 15:10:22 -08005019 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005020 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5021 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5023 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
5024 ASSERT_EQ(DEVICE_ID, args.deviceId);
5025 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
5026 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005027 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
5028 ASSERT_EQ(0, args.flags);
5029 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5030 ASSERT_EQ(0, args.buttonState);
5031 ASSERT_EQ(0, args.edgeFlags);
5032 ASSERT_EQ(uint32_t(1), args.pointerCount);
5033 ASSERT_EQ(0, args.pointerProperties[0].id);
5034 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005035 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005036 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
5037 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
5038 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5039
5040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5041 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
5042 ASSERT_EQ(DEVICE_ID, args.deviceId);
5043 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
5044 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005045 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
5046 ASSERT_EQ(0, args.flags);
5047 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5048 ASSERT_EQ(0, args.buttonState);
5049 ASSERT_EQ(0, args.edgeFlags);
5050 ASSERT_EQ(uint32_t(1), args.pointerCount);
5051 ASSERT_EQ(0, args.pointerProperties[0].id);
5052 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005053 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005054 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
5055 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
5056 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5057}
5058
5059TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005061 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005062
5063 NotifyMotionArgs args;
5064
5065 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005066 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5069 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005070 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
5071 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
5072 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005073
5074 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005075 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
5076 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5078 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005079 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
5080 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005081}
5082
5083TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005084 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005085 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005086
5087 NotifyMotionArgs args;
5088
5089 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005090 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5091 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5093 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005094 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005095
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5097 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005098 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005099
Michael Wrightd02c5b62014-02-10 15:10:22 -08005100 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005101 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5102 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005104 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005105 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005106
5107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005108 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005109 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005110}
5111
5112TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005114 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005115
5116 NotifyMotionArgs args;
5117
5118 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
5121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5124 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005125 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
5126 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
5127 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005128
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5130 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005131 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
5132 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
5133 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005134
Michael Wrightd02c5b62014-02-10 15:10:22 -08005135 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
5137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
5138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005141 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
5142 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
5143 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005144
5145 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5147 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005149 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005150 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005151
5152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005153 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005154 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005155}
5156
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005157TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005158 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005159 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005160 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5161 // need to be rotated.
5162 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005163 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005164
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005165 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005166 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
5167 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
5168 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
5169 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
5170 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
5171 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
5172 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
5173 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
5174}
5175
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005176TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005177 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005178 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005179 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5180 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005181 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005182
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005183 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005184 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005185 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
5186 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
5187 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
5188 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
5189 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
5190 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
5191 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
5192 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
5193
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005194 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005195 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005196 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
5197 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
5198 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
5199 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
5200 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
5201 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
5202 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
5203 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005204
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005205 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005206 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005207 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
5208 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
5209 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
5210 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
5211 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
5212 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
5213 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
5214 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
5215
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005216 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005217 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005218 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
5219 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
5220 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
5221 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
5222 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
5223 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
5224 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
5225 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005226}
5227
5228TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005229 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005230 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005231
5232 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5233 mFakePointerController->setPosition(100, 200);
5234 mFakePointerController->setButtonState(0);
5235
5236 NotifyMotionArgs motionArgs;
5237 NotifyKeyArgs keyArgs;
5238
5239 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005240 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
5241 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5243 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5244 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5245 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005246 ASSERT_NO_FATAL_FAILURE(
5247 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005248
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5250 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5251 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5252 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005253 ASSERT_NO_FATAL_FAILURE(
5254 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005255
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005256 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
5257 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005259 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005260 ASSERT_EQ(0, motionArgs.buttonState);
5261 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005262 ASSERT_NO_FATAL_FAILURE(
5263 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005264
5265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005266 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005267 ASSERT_EQ(0, motionArgs.buttonState);
5268 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005269 ASSERT_NO_FATAL_FAILURE(
5270 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005271
5272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005273 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005274 ASSERT_EQ(0, motionArgs.buttonState);
5275 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005276 ASSERT_NO_FATAL_FAILURE(
5277 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005278
5279 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
5281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
5282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5284 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5285 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5286 motionArgs.buttonState);
5287 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5288 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005289 ASSERT_NO_FATAL_FAILURE(
5290 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005291
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5293 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5294 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5295 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5296 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005297 ASSERT_NO_FATAL_FAILURE(
5298 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005299
5300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5301 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5302 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5303 motionArgs.buttonState);
5304 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5305 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005306 ASSERT_NO_FATAL_FAILURE(
5307 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005308
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
5310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005312 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005313 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5314 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005315 ASSERT_NO_FATAL_FAILURE(
5316 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005317
5318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005320 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5321 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005322 ASSERT_NO_FATAL_FAILURE(
5323 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005324
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5326 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005328 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5329 ASSERT_EQ(0, motionArgs.buttonState);
5330 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005331 ASSERT_NO_FATAL_FAILURE(
5332 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5334 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005335
5336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005337 ASSERT_EQ(0, motionArgs.buttonState);
5338 ASSERT_EQ(0, mFakePointerController->getButtonState());
5339 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005340 ASSERT_NO_FATAL_FAILURE(
5341 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005342
Michael Wrightd02c5b62014-02-10 15:10:22 -08005343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5344 ASSERT_EQ(0, motionArgs.buttonState);
5345 ASSERT_EQ(0, mFakePointerController->getButtonState());
5346 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005347 ASSERT_NO_FATAL_FAILURE(
5348 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005349
5350 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
5352 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5354 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5355 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005356
Michael Wrightd02c5b62014-02-10 15:10:22 -08005357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005358 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005359 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5360 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005361 ASSERT_NO_FATAL_FAILURE(
5362 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005363
5364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5365 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5366 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5367 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005368 ASSERT_NO_FATAL_FAILURE(
5369 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005370
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005371 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
5372 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005374 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005375 ASSERT_EQ(0, motionArgs.buttonState);
5376 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005377 ASSERT_NO_FATAL_FAILURE(
5378 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005379
5380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005381 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005382 ASSERT_EQ(0, motionArgs.buttonState);
5383 ASSERT_EQ(0, mFakePointerController->getButtonState());
5384
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005385 ASSERT_NO_FATAL_FAILURE(
5386 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5388 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5389 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5390
5391 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005392 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
5393 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5395 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5396 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005397
Michael Wrightd02c5b62014-02-10 15:10:22 -08005398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005399 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005400 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5401 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005402 ASSERT_NO_FATAL_FAILURE(
5403 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005404
5405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5406 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5407 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5408 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005409 ASSERT_NO_FATAL_FAILURE(
5410 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005411
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005412 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
5413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005415 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416 ASSERT_EQ(0, motionArgs.buttonState);
5417 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005418 ASSERT_NO_FATAL_FAILURE(
5419 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005420
5421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5422 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5423 ASSERT_EQ(0, motionArgs.buttonState);
5424 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005425 ASSERT_NO_FATAL_FAILURE(
5426 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005427
Michael Wrightd02c5b62014-02-10 15:10:22 -08005428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5429 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5430 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5431
5432 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005433 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
5434 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5436 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5437 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005438
Michael Wrightd02c5b62014-02-10 15:10:22 -08005439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005440 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005441 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5442 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005443 ASSERT_NO_FATAL_FAILURE(
5444 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005445
5446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5447 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5448 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5449 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005450 ASSERT_NO_FATAL_FAILURE(
5451 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005452
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005453 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
5454 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005456 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005457 ASSERT_EQ(0, motionArgs.buttonState);
5458 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005459 ASSERT_NO_FATAL_FAILURE(
5460 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005461
5462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5463 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5464 ASSERT_EQ(0, motionArgs.buttonState);
5465 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005466 ASSERT_NO_FATAL_FAILURE(
5467 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005468
Michael Wrightd02c5b62014-02-10 15:10:22 -08005469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5470 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5471 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5472
5473 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
5475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5477 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5478 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005479
Michael Wrightd02c5b62014-02-10 15:10:22 -08005480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005481 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005482 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5483 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005484 ASSERT_NO_FATAL_FAILURE(
5485 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005486
5487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5488 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5489 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5490 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005491 ASSERT_NO_FATAL_FAILURE(
5492 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005493
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005494 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
5495 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005498 ASSERT_EQ(0, motionArgs.buttonState);
5499 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005500 ASSERT_NO_FATAL_FAILURE(
5501 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005502
5503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5504 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5505 ASSERT_EQ(0, motionArgs.buttonState);
5506 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005507 ASSERT_NO_FATAL_FAILURE(
5508 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005509
Michael Wrightd02c5b62014-02-10 15:10:22 -08005510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5511 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5512 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5513}
5514
5515TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005516 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005517 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005518
5519 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5520 mFakePointerController->setPosition(100, 200);
5521 mFakePointerController->setButtonState(0);
5522
5523 NotifyMotionArgs args;
5524
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005525 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5527 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005529 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5530 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5532 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 +01005533 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005534}
5535
5536TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005537 addConfigurationProperty("cursor.mode", "pointer");
5538 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005539 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005540
5541 NotifyDeviceResetArgs resetArgs;
5542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5543 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5544 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5545
5546 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5547 mFakePointerController->setPosition(100, 200);
5548 mFakePointerController->setButtonState(0);
5549
5550 NotifyMotionArgs args;
5551
5552 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005553 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5554 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5555 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5557 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5560 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 +01005561 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005562
5563 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005564 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5565 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5567 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5568 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5570 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5572 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5573 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
5574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5575 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5576
5577 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005578 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5579 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5581 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5582 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
5583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5584 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5586 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5587 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
5588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5589 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5590
5591 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
5593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
5594 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5596 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5597 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5599 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 +01005600 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005601
5602 // Disable pointer capture and check that the device generation got bumped
5603 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08005604 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005605 mFakePolicy->setPointerCapture(false);
5606 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08005607 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005608
5609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005610 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5611
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5616 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005617 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5619 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 +01005620 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005621}
5622
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00005623/**
5624 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
5625 * pointer acceleration or speed processing should not be applied.
5626 */
5627TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5628 addConfigurationProperty("cursor.mode", "pointer");
5629 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5630 100.f /*high threshold*/, 10.f /*acceleration*/);
5631 mFakePolicy->setVelocityControlParams(testParams);
5632 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5633
5634 NotifyDeviceResetArgs resetArgs;
5635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5636 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5637 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5638
5639 NotifyMotionArgs args;
5640
5641 // Move and verify scale is applied.
5642 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5643 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5644 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5646 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5647 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5648 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5649 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5650 ASSERT_GT(relX, 10);
5651 ASSERT_GT(relY, 20);
5652
5653 // Enable Pointer Capture
5654 mFakePolicy->setPointerCapture(true);
5655 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5656 NotifyPointerCaptureChangedArgs captureArgs;
5657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5658 ASSERT_TRUE(captureArgs.request.enable);
5659
5660 // Move and verify scale is not applied.
5661 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5662 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5665 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5666 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5667 ASSERT_EQ(10, args.pointerCoords[0].getX());
5668 ASSERT_EQ(20, args.pointerCoords[0].getY());
5669}
5670
Prabir Pradhan208360b2022-06-24 18:37:04 +00005671TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5672 addConfigurationProperty("cursor.mode", "pointer");
5673 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5674
5675 NotifyDeviceResetArgs resetArgs;
5676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5677 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5678 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5679
5680 // Ensure the display is rotated.
5681 prepareDisplay(DISPLAY_ORIENTATION_90);
5682
5683 NotifyMotionArgs args;
5684
5685 // Verify that the coordinates are rotated.
5686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5688 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5690 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5691 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5692 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5693 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5694
5695 // Enable Pointer Capture.
5696 mFakePolicy->setPointerCapture(true);
5697 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5698 NotifyPointerCaptureChangedArgs captureArgs;
5699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5700 ASSERT_TRUE(captureArgs.request.enable);
5701
5702 // Move and verify rotation is not applied.
5703 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5704 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5705 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5707 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5708 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5709 ASSERT_EQ(10, args.pointerCoords[0].getX());
5710 ASSERT_EQ(20, args.pointerCoords[0].getY());
5711}
5712
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005713TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005714 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005715
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005716 // Set up the default display.
5717 prepareDisplay(DISPLAY_ORIENTATION_90);
5718
5719 // Set up the secondary display as the display on which the pointer should be shown.
5720 // The InputDevice is not associated with any display.
5721 prepareSecondaryDisplay();
5722 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005723 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5724
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005725 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005726 mFakePointerController->setPosition(100, 200);
5727 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005728
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005729 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005730 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5731 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5732 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005734 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5735 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5736 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005737 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005738}
5739
5740TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5741 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5742
5743 // Set up the default display.
5744 prepareDisplay(DISPLAY_ORIENTATION_90);
5745
5746 // Set up the secondary display as the display on which the pointer should be shown,
5747 // and associate the InputDevice with the secondary display.
5748 prepareSecondaryDisplay();
5749 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5750 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5751 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5752
5753 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5754 mFakePointerController->setPosition(100, 200);
5755 mFakePointerController->setButtonState(0);
5756
5757 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5758 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005761 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5762 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5763 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005764 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5765}
5766
5767TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5768 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5769
5770 // Set up the default display as the display on which the pointer should be shown.
5771 prepareDisplay(DISPLAY_ORIENTATION_90);
5772 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5773
5774 // Associate the InputDevice with the secondary display.
5775 prepareSecondaryDisplay();
5776 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5777 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5778
5779 // The mapper should not generate any events because it is associated with a display that is
5780 // different from the pointer display.
5781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5782 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5783 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005785}
5786
Michael Wrightd02c5b62014-02-10 15:10:22 -08005787// --- TouchInputMapperTest ---
5788
5789class TouchInputMapperTest : public InputMapperTest {
5790protected:
5791 static const int32_t RAW_X_MIN;
5792 static const int32_t RAW_X_MAX;
5793 static const int32_t RAW_Y_MIN;
5794 static const int32_t RAW_Y_MAX;
5795 static const int32_t RAW_TOUCH_MIN;
5796 static const int32_t RAW_TOUCH_MAX;
5797 static const int32_t RAW_TOOL_MIN;
5798 static const int32_t RAW_TOOL_MAX;
5799 static const int32_t RAW_PRESSURE_MIN;
5800 static const int32_t RAW_PRESSURE_MAX;
5801 static const int32_t RAW_ORIENTATION_MIN;
5802 static const int32_t RAW_ORIENTATION_MAX;
5803 static const int32_t RAW_DISTANCE_MIN;
5804 static const int32_t RAW_DISTANCE_MAX;
5805 static const int32_t RAW_TILT_MIN;
5806 static const int32_t RAW_TILT_MAX;
5807 static const int32_t RAW_ID_MIN;
5808 static const int32_t RAW_ID_MAX;
5809 static const int32_t RAW_SLOT_MIN;
5810 static const int32_t RAW_SLOT_MAX;
5811 static const float X_PRECISION;
5812 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005813 static const float X_PRECISION_VIRTUAL;
5814 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005815
5816 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005817 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005818
5819 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5820
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005821 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005822 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005823
Michael Wrightd02c5b62014-02-10 15:10:22 -08005824 enum Axes {
5825 POSITION = 1 << 0,
5826 TOUCH = 1 << 1,
5827 TOOL = 1 << 2,
5828 PRESSURE = 1 << 3,
5829 ORIENTATION = 1 << 4,
5830 MINOR = 1 << 5,
5831 ID = 1 << 6,
5832 DISTANCE = 1 << 7,
5833 TILT = 1 << 8,
5834 SLOT = 1 << 9,
5835 TOOL_TYPE = 1 << 10,
5836 };
5837
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005838 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5839 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005840 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005841 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005842 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005843 int32_t toRawX(float displayX);
5844 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005845 int32_t toRotatedRawX(float displayX);
5846 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005847 float toCookedX(float rawX, float rawY);
5848 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005849 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005850 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005851 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005852 float toDisplayY(int32_t rawY, int32_t displayHeight);
5853
Michael Wrightd02c5b62014-02-10 15:10:22 -08005854};
5855
5856const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5857const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5858const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5859const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5860const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5861const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5862const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5863const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005864const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5865const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005866const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5867const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5868const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5869const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5870const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5871const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5872const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5873const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5874const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5875const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5876const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5877const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005878const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5879 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5880const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5881 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005882const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5883 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005884
5885const float TouchInputMapperTest::GEOMETRIC_SCALE =
5886 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5887 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5888
5889const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5890 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5891 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5892};
5893
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005894void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005895 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5896 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005897}
5898
5899void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5900 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5901 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005902}
5903
Santos Cordonfa5cf462017-04-05 10:37:00 -07005904void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005905 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5906 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5907 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005908}
5909
Michael Wrightd02c5b62014-02-10 15:10:22 -08005910void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005911 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5912 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5913 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5914 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005915}
5916
Jason Gerecke489fda82012-09-07 17:19:40 -07005917void TouchInputMapperTest::prepareLocationCalibration() {
5918 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5919}
5920
Michael Wrightd02c5b62014-02-10 15:10:22 -08005921int32_t TouchInputMapperTest::toRawX(float displayX) {
5922 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5923}
5924
5925int32_t TouchInputMapperTest::toRawY(float displayY) {
5926 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5927}
5928
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005929int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5930 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5931}
5932
5933int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5934 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5935}
5936
Jason Gerecke489fda82012-09-07 17:19:40 -07005937float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5938 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5939 return rawX;
5940}
5941
5942float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5943 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5944 return rawY;
5945}
5946
Michael Wrightd02c5b62014-02-10 15:10:22 -08005947float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005948 return toDisplayX(rawX, DISPLAY_WIDTH);
5949}
5950
5951float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5952 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005953}
5954
5955float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005956 return toDisplayY(rawY, DISPLAY_HEIGHT);
5957}
5958
5959float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5960 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005961}
5962
5963
5964// --- SingleTouchInputMapperTest ---
5965
5966class SingleTouchInputMapperTest : public TouchInputMapperTest {
5967protected:
5968 void prepareButtons();
5969 void prepareAxes(int axes);
5970
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005971 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5972 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5973 void processUp(SingleTouchInputMapper& mappery);
5974 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5975 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5976 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5977 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5978 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5979 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005980};
5981
5982void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005983 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005984}
5985
5986void SingleTouchInputMapperTest::prepareAxes(int axes) {
5987 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005988 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5989 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005990 }
5991 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005992 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5993 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005994 }
5995 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005996 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5997 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005998 }
5999 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006000 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
6001 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006002 }
6003 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006004 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
6005 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006006 }
6007}
6008
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006009void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006010 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6011 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
6012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006013}
6014
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006015void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
6017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006018}
6019
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006020void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006022}
6023
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006024void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006025 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006026}
6027
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006028void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
6029 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006030 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006031}
6032
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006033void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006035}
6036
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006037void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
6038 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
6040 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006041}
6042
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006043void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
6044 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006046}
6047
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006048void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006049 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006050}
6051
Michael Wrightd02c5b62014-02-10 15:10:22 -08006052TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006053 prepareButtons();
6054 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006055 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006056
Harry Cutts16a24cc2022-10-26 15:22:19 +00006057 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006058}
6059
Michael Wrightd02c5b62014-02-10 15:10:22 -08006060TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006061 prepareButtons();
6062 prepareAxes(POSITION);
6063 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006064 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006065
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006066 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006067}
6068
6069TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006070 addConfigurationProperty("touch.deviceType", "touchScreen");
6071 prepareDisplay(DISPLAY_ORIENTATION_0);
6072 prepareButtons();
6073 prepareAxes(POSITION);
6074 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006075 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006076
6077 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006078 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006079
6080 // Virtual key is down.
6081 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6082 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6083 processDown(mapper, x, y);
6084 processSync(mapper);
6085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
6086
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006087 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006088
6089 // Virtual key is up.
6090 processUp(mapper);
6091 processSync(mapper);
6092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
6093
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006094 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006095}
6096
6097TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006098 addConfigurationProperty("touch.deviceType", "touchScreen");
6099 prepareDisplay(DISPLAY_ORIENTATION_0);
6100 prepareButtons();
6101 prepareAxes(POSITION);
6102 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006103 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006104
6105 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006106 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006107
6108 // Virtual key is down.
6109 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6110 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6111 processDown(mapper, x, y);
6112 processSync(mapper);
6113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
6114
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006115 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006116
6117 // Virtual key is up.
6118 processUp(mapper);
6119 processSync(mapper);
6120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
6121
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006122 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006123}
6124
6125TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126 addConfigurationProperty("touch.deviceType", "touchScreen");
6127 prepareDisplay(DISPLAY_ORIENTATION_0);
6128 prepareButtons();
6129 prepareAxes(POSITION);
6130 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006131 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006132
Michael Wrightd02c5b62014-02-10 15:10:22 -08006133 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07006134 ASSERT_TRUE(
6135 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006136 ASSERT_TRUE(flags[0]);
6137 ASSERT_FALSE(flags[1]);
6138}
6139
6140TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006141 addConfigurationProperty("touch.deviceType", "touchScreen");
6142 prepareDisplay(DISPLAY_ORIENTATION_0);
6143 prepareButtons();
6144 prepareAxes(POSITION);
6145 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006146 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006147
arthurhungdcef2dc2020-08-11 14:47:50 +08006148 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006149
6150 NotifyKeyArgs args;
6151
6152 // Press virtual key.
6153 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6154 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6155 processDown(mapper, x, y);
6156 processSync(mapper);
6157
6158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
6159 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
6160 ASSERT_EQ(DEVICE_ID, args.deviceId);
6161 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
6162 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
6163 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
6164 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
6165 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
6166 ASSERT_EQ(KEY_HOME, args.scanCode);
6167 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
6168 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
6169
6170 // Release virtual key.
6171 processUp(mapper);
6172 processSync(mapper);
6173
6174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
6175 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
6176 ASSERT_EQ(DEVICE_ID, args.deviceId);
6177 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
6178 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
6179 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
6180 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
6181 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
6182 ASSERT_EQ(KEY_HOME, args.scanCode);
6183 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
6184 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
6185
6186 // Should not have sent any motions.
6187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6188}
6189
6190TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006191 addConfigurationProperty("touch.deviceType", "touchScreen");
6192 prepareDisplay(DISPLAY_ORIENTATION_0);
6193 prepareButtons();
6194 prepareAxes(POSITION);
6195 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006196 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006197
arthurhungdcef2dc2020-08-11 14:47:50 +08006198 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006199
6200 NotifyKeyArgs keyArgs;
6201
6202 // Press virtual key.
6203 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6204 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6205 processDown(mapper, x, y);
6206 processSync(mapper);
6207
6208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6209 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
6210 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
6211 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
6212 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
6213 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6214 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
6215 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
6216 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
6217 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
6218 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
6219
6220 // Move out of bounds. This should generate a cancel and a pointer down since we moved
6221 // into the display area.
6222 y -= 100;
6223 processMove(mapper, x, y);
6224 processSync(mapper);
6225
6226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6227 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
6228 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
6229 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
6230 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
6231 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6232 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
6233 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
6234 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
6235 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
6236 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
6237 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
6238
6239 NotifyMotionArgs motionArgs;
6240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6241 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6242 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6243 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6244 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6245 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6246 ASSERT_EQ(0, motionArgs.flags);
6247 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6248 ASSERT_EQ(0, motionArgs.buttonState);
6249 ASSERT_EQ(0, motionArgs.edgeFlags);
6250 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6251 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6252 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6253 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6254 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6255 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6256 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6257 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6258
6259 // Keep moving out of bounds. Should generate a pointer move.
6260 y -= 50;
6261 processMove(mapper, x, y);
6262 processSync(mapper);
6263
6264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6265 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6266 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6267 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6268 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6270 ASSERT_EQ(0, motionArgs.flags);
6271 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6272 ASSERT_EQ(0, motionArgs.buttonState);
6273 ASSERT_EQ(0, motionArgs.edgeFlags);
6274 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6275 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6276 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6277 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6278 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6279 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6280 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6281 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6282
6283 // Release out of bounds. Should generate a pointer up.
6284 processUp(mapper);
6285 processSync(mapper);
6286
6287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6288 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6289 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6290 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6291 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6292 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6293 ASSERT_EQ(0, motionArgs.flags);
6294 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6295 ASSERT_EQ(0, motionArgs.buttonState);
6296 ASSERT_EQ(0, motionArgs.edgeFlags);
6297 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6298 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6299 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6301 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6302 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6303 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6304 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6305
6306 // Should not have sent any more keys or motions.
6307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6309}
6310
6311TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006312 addConfigurationProperty("touch.deviceType", "touchScreen");
6313 prepareDisplay(DISPLAY_ORIENTATION_0);
6314 prepareButtons();
6315 prepareAxes(POSITION);
6316 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006317 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318
arthurhungdcef2dc2020-08-11 14:47:50 +08006319 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006320
6321 NotifyMotionArgs motionArgs;
6322
6323 // Initially go down out of bounds.
6324 int32_t x = -10;
6325 int32_t y = -10;
6326 processDown(mapper, x, y);
6327 processSync(mapper);
6328
6329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6330
6331 // Move into the display area. Should generate a pointer down.
6332 x = 50;
6333 y = 75;
6334 processMove(mapper, x, y);
6335 processSync(mapper);
6336
6337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6338 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6339 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6340 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6341 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6342 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6343 ASSERT_EQ(0, motionArgs.flags);
6344 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6345 ASSERT_EQ(0, motionArgs.buttonState);
6346 ASSERT_EQ(0, motionArgs.edgeFlags);
6347 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6348 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6349 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6350 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6351 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6352 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6353 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6354 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6355
6356 // Release. Should generate a pointer up.
6357 processUp(mapper);
6358 processSync(mapper);
6359
6360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6361 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6362 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6363 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6364 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6365 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6366 ASSERT_EQ(0, motionArgs.flags);
6367 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6368 ASSERT_EQ(0, motionArgs.buttonState);
6369 ASSERT_EQ(0, motionArgs.edgeFlags);
6370 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6371 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6372 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6374 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6375 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6376 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6377 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6378
6379 // Should not have sent any more keys or motions.
6380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6382}
6383
Santos Cordonfa5cf462017-04-05 10:37:00 -07006384TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07006385 addConfigurationProperty("touch.deviceType", "touchScreen");
6386 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
6387
6388 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
6389 prepareButtons();
6390 prepareAxes(POSITION);
6391 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006392 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07006393
arthurhungdcef2dc2020-08-11 14:47:50 +08006394 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006395
6396 NotifyMotionArgs motionArgs;
6397
6398 // Down.
6399 int32_t x = 100;
6400 int32_t y = 125;
6401 processDown(mapper, x, y);
6402 processSync(mapper);
6403
6404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6405 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6406 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6407 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6408 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6409 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6410 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6411 ASSERT_EQ(0, motionArgs.flags);
6412 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6413 ASSERT_EQ(0, motionArgs.buttonState);
6414 ASSERT_EQ(0, motionArgs.edgeFlags);
6415 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6416 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6417 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6419 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6420 1, 0, 0, 0, 0, 0, 0, 0));
6421 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6422 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6423 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6424
6425 // Move.
6426 x += 50;
6427 y += 75;
6428 processMove(mapper, x, y);
6429 processSync(mapper);
6430
6431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6432 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6433 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6434 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6435 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6436 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6437 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6438 ASSERT_EQ(0, motionArgs.flags);
6439 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6440 ASSERT_EQ(0, motionArgs.buttonState);
6441 ASSERT_EQ(0, motionArgs.edgeFlags);
6442 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6443 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6444 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6446 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6447 1, 0, 0, 0, 0, 0, 0, 0));
6448 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6449 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6450 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6451
6452 // Up.
6453 processUp(mapper);
6454 processSync(mapper);
6455
6456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6457 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6458 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6459 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6460 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6461 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6462 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6463 ASSERT_EQ(0, motionArgs.flags);
6464 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6465 ASSERT_EQ(0, motionArgs.buttonState);
6466 ASSERT_EQ(0, motionArgs.edgeFlags);
6467 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6468 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6469 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6471 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6472 1, 0, 0, 0, 0, 0, 0, 0));
6473 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6474 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6475 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6476
6477 // Should not have sent any more keys or motions.
6478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6480}
6481
Michael Wrightd02c5b62014-02-10 15:10:22 -08006482TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006483 addConfigurationProperty("touch.deviceType", "touchScreen");
6484 prepareDisplay(DISPLAY_ORIENTATION_0);
6485 prepareButtons();
6486 prepareAxes(POSITION);
6487 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006488 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006489
arthurhungdcef2dc2020-08-11 14:47:50 +08006490 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006491
6492 NotifyMotionArgs motionArgs;
6493
6494 // Down.
6495 int32_t x = 100;
6496 int32_t y = 125;
6497 processDown(mapper, x, y);
6498 processSync(mapper);
6499
6500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6501 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6502 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6503 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6504 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6505 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6506 ASSERT_EQ(0, motionArgs.flags);
6507 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6508 ASSERT_EQ(0, motionArgs.buttonState);
6509 ASSERT_EQ(0, motionArgs.edgeFlags);
6510 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6511 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6512 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6514 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6515 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6516 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6517 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6518
6519 // Move.
6520 x += 50;
6521 y += 75;
6522 processMove(mapper, x, y);
6523 processSync(mapper);
6524
6525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6526 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6527 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6528 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6529 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6530 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6531 ASSERT_EQ(0, motionArgs.flags);
6532 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6533 ASSERT_EQ(0, motionArgs.buttonState);
6534 ASSERT_EQ(0, motionArgs.edgeFlags);
6535 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6536 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6539 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6540 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6541 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6542 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6543
6544 // Up.
6545 processUp(mapper);
6546 processSync(mapper);
6547
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6549 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6550 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6551 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6552 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6553 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6554 ASSERT_EQ(0, motionArgs.flags);
6555 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6556 ASSERT_EQ(0, motionArgs.buttonState);
6557 ASSERT_EQ(0, motionArgs.edgeFlags);
6558 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6559 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6560 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6562 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6563 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6564 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6565 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6566
6567 // Should not have sent any more keys or motions.
6568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6570}
6571
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006572TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006573 addConfigurationProperty("touch.deviceType", "touchScreen");
6574 prepareButtons();
6575 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006576 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
6577 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006578 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006579
6580 NotifyMotionArgs args;
6581
6582 // Rotation 90.
6583 prepareDisplay(DISPLAY_ORIENTATION_90);
6584 processDown(mapper, toRawX(50), toRawY(75));
6585 processSync(mapper);
6586
6587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6588 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6589 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6590
6591 processUp(mapper);
6592 processSync(mapper);
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6594}
6595
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006596TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006597 addConfigurationProperty("touch.deviceType", "touchScreen");
6598 prepareButtons();
6599 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006600 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6601 // orientation-aware are affected by display rotation.
6602 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006603 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006604
6605 NotifyMotionArgs args;
6606
6607 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006608 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006609 prepareDisplay(DISPLAY_ORIENTATION_0);
6610 processDown(mapper, toRawX(50), toRawY(75));
6611 processSync(mapper);
6612
6613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6614 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6615 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6616
6617 processUp(mapper);
6618 processSync(mapper);
6619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6620
6621 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006622 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006623 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006624 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006625 processSync(mapper);
6626
6627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6628 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6629 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6630
6631 processUp(mapper);
6632 processSync(mapper);
6633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6634
6635 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006636 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006637 prepareDisplay(DISPLAY_ORIENTATION_180);
6638 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6639 processSync(mapper);
6640
6641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6642 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6643 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6644
6645 processUp(mapper);
6646 processSync(mapper);
6647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6648
6649 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006650 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006651 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006652 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006653 processSync(mapper);
6654
6655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6656 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6657 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6658
6659 processUp(mapper);
6660 processSync(mapper);
6661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6662}
6663
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006664TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6665 addConfigurationProperty("touch.deviceType", "touchScreen");
6666 prepareButtons();
6667 prepareAxes(POSITION);
6668 addConfigurationProperty("touch.orientationAware", "1");
6669 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6670 clearViewports();
6671 prepareDisplay(DISPLAY_ORIENTATION_0);
6672 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6673 NotifyMotionArgs args;
6674
6675 // Orientation 0.
6676 processDown(mapper, toRawX(50), toRawY(75));
6677 processSync(mapper);
6678
6679 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6680 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6681 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6682
6683 processUp(mapper);
6684 processSync(mapper);
6685 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6686}
6687
6688TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6689 addConfigurationProperty("touch.deviceType", "touchScreen");
6690 prepareButtons();
6691 prepareAxes(POSITION);
6692 addConfigurationProperty("touch.orientationAware", "1");
6693 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6694 clearViewports();
6695 prepareDisplay(DISPLAY_ORIENTATION_0);
6696 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6697 NotifyMotionArgs args;
6698
6699 // Orientation 90.
6700 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6701 processSync(mapper);
6702
6703 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6704 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6705 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6706
6707 processUp(mapper);
6708 processSync(mapper);
6709 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6710}
6711
6712TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6713 addConfigurationProperty("touch.deviceType", "touchScreen");
6714 prepareButtons();
6715 prepareAxes(POSITION);
6716 addConfigurationProperty("touch.orientationAware", "1");
6717 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6718 clearViewports();
6719 prepareDisplay(DISPLAY_ORIENTATION_0);
6720 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6721 NotifyMotionArgs args;
6722
6723 // Orientation 180.
6724 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6725 processSync(mapper);
6726
6727 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6728 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6729 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6730
6731 processUp(mapper);
6732 processSync(mapper);
6733 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6734}
6735
6736TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6737 addConfigurationProperty("touch.deviceType", "touchScreen");
6738 prepareButtons();
6739 prepareAxes(POSITION);
6740 addConfigurationProperty("touch.orientationAware", "1");
6741 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6742 clearViewports();
6743 prepareDisplay(DISPLAY_ORIENTATION_0);
6744 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6745 NotifyMotionArgs args;
6746
6747 // Orientation 270.
6748 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6749 processSync(mapper);
6750
6751 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6752 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6753 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6754
6755 processUp(mapper);
6756 processSync(mapper);
6757 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6758}
6759
6760TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6761 addConfigurationProperty("touch.deviceType", "touchScreen");
6762 prepareButtons();
6763 prepareAxes(POSITION);
6764 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6765 // orientation-aware are affected by display rotation.
6766 addConfigurationProperty("touch.orientationAware", "0");
6767 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6768 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6769
6770 NotifyMotionArgs args;
6771
6772 // Orientation 90, Rotation 0.
6773 clearViewports();
6774 prepareDisplay(DISPLAY_ORIENTATION_0);
6775 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6776 processSync(mapper);
6777
6778 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6779 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6780 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6781
6782 processUp(mapper);
6783 processSync(mapper);
6784 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6785
6786 // Orientation 90, Rotation 90.
6787 clearViewports();
6788 prepareDisplay(DISPLAY_ORIENTATION_90);
6789 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6790 processSync(mapper);
6791
6792 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6793 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6794 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6795
6796 processUp(mapper);
6797 processSync(mapper);
6798 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6799
6800 // Orientation 90, Rotation 180.
6801 clearViewports();
6802 prepareDisplay(DISPLAY_ORIENTATION_180);
6803 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6804 processSync(mapper);
6805
6806 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6807 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6808 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6809
6810 processUp(mapper);
6811 processSync(mapper);
6812 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6813
6814 // Orientation 90, Rotation 270.
6815 clearViewports();
6816 prepareDisplay(DISPLAY_ORIENTATION_270);
6817 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6818 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6819 processSync(mapper);
6820
6821 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6822 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6823 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6824
6825 processUp(mapper);
6826 processSync(mapper);
6827 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6828}
6829
Michael Wrightd02c5b62014-02-10 15:10:22 -08006830TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006831 addConfigurationProperty("touch.deviceType", "touchScreen");
6832 prepareDisplay(DISPLAY_ORIENTATION_0);
6833 prepareButtons();
6834 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006835 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006836
6837 // These calculations are based on the input device calibration documentation.
6838 int32_t rawX = 100;
6839 int32_t rawY = 200;
6840 int32_t rawPressure = 10;
6841 int32_t rawToolMajor = 12;
6842 int32_t rawDistance = 2;
6843 int32_t rawTiltX = 30;
6844 int32_t rawTiltY = 110;
6845
6846 float x = toDisplayX(rawX);
6847 float y = toDisplayY(rawY);
6848 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6849 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6850 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6851 float distance = float(rawDistance);
6852
6853 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6854 float tiltScale = M_PI / 180;
6855 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6856 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6857 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6858 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6859
6860 processDown(mapper, rawX, rawY);
6861 processPressure(mapper, rawPressure);
6862 processToolMajor(mapper, rawToolMajor);
6863 processDistance(mapper, rawDistance);
6864 processTilt(mapper, rawTiltX, rawTiltY);
6865 processSync(mapper);
6866
6867 NotifyMotionArgs args;
6868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6870 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6871 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6872}
6873
Jason Gerecke489fda82012-09-07 17:19:40 -07006874TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006875 addConfigurationProperty("touch.deviceType", "touchScreen");
6876 prepareDisplay(DISPLAY_ORIENTATION_0);
6877 prepareLocationCalibration();
6878 prepareButtons();
6879 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006880 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006881
6882 int32_t rawX = 100;
6883 int32_t rawY = 200;
6884
6885 float x = toDisplayX(toCookedX(rawX, rawY));
6886 float y = toDisplayY(toCookedY(rawX, rawY));
6887
6888 processDown(mapper, rawX, rawY);
6889 processSync(mapper);
6890
6891 NotifyMotionArgs args;
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6894 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6895}
6896
Michael Wrightd02c5b62014-02-10 15:10:22 -08006897TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006898 addConfigurationProperty("touch.deviceType", "touchScreen");
6899 prepareDisplay(DISPLAY_ORIENTATION_0);
6900 prepareButtons();
6901 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006902 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006903
6904 NotifyMotionArgs motionArgs;
6905 NotifyKeyArgs keyArgs;
6906
6907 processDown(mapper, 100, 200);
6908 processSync(mapper);
6909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6910 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6911 ASSERT_EQ(0, motionArgs.buttonState);
6912
6913 // press BTN_LEFT, release BTN_LEFT
6914 processKey(mapper, BTN_LEFT, 1);
6915 processSync(mapper);
6916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6917 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6918 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6919
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6921 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6922 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6923
Michael Wrightd02c5b62014-02-10 15:10:22 -08006924 processKey(mapper, BTN_LEFT, 0);
6925 processSync(mapper);
6926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006927 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006928 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006929
6930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006931 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006932 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006933
6934 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6935 processKey(mapper, BTN_RIGHT, 1);
6936 processKey(mapper, BTN_MIDDLE, 1);
6937 processSync(mapper);
6938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6939 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6940 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6941 motionArgs.buttonState);
6942
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6944 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6945 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6946
6947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6948 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6949 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6950 motionArgs.buttonState);
6951
Michael Wrightd02c5b62014-02-10 15:10:22 -08006952 processKey(mapper, BTN_RIGHT, 0);
6953 processSync(mapper);
6954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006955 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006956 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006957
6958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006960 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006961
6962 processKey(mapper, BTN_MIDDLE, 0);
6963 processSync(mapper);
6964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006965 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006966 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006967
6968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006969 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006970 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006971
6972 // press BTN_BACK, release BTN_BACK
6973 processKey(mapper, BTN_BACK, 1);
6974 processSync(mapper);
6975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6976 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6977 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006978
Michael Wrightd02c5b62014-02-10 15:10:22 -08006979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006980 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006981 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6982
6983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6984 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6985 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006986
6987 processKey(mapper, BTN_BACK, 0);
6988 processSync(mapper);
6989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006990 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006991 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006992
6993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006994 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006995 ASSERT_EQ(0, motionArgs.buttonState);
6996
Michael Wrightd02c5b62014-02-10 15:10:22 -08006997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6998 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6999 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7000
7001 // press BTN_SIDE, release BTN_SIDE
7002 processKey(mapper, BTN_SIDE, 1);
7003 processSync(mapper);
7004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7005 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7006 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007007
Michael Wrightd02c5b62014-02-10 15:10:22 -08007008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007009 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007010 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7011
7012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7013 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7014 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007015
7016 processKey(mapper, BTN_SIDE, 0);
7017 processSync(mapper);
7018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007019 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007020 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007021
7022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007023 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007024 ASSERT_EQ(0, motionArgs.buttonState);
7025
Michael Wrightd02c5b62014-02-10 15:10:22 -08007026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7027 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7028 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7029
7030 // press BTN_FORWARD, release BTN_FORWARD
7031 processKey(mapper, BTN_FORWARD, 1);
7032 processSync(mapper);
7033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7034 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7035 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007036
Michael Wrightd02c5b62014-02-10 15:10:22 -08007037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007039 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7040
7041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7042 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7043 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007044
7045 processKey(mapper, BTN_FORWARD, 0);
7046 processSync(mapper);
7047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007048 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007049 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007050
7051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007052 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007053 ASSERT_EQ(0, motionArgs.buttonState);
7054
Michael Wrightd02c5b62014-02-10 15:10:22 -08007055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7056 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7057 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7058
7059 // press BTN_EXTRA, release BTN_EXTRA
7060 processKey(mapper, BTN_EXTRA, 1);
7061 processSync(mapper);
7062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7063 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7064 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007065
Michael Wrightd02c5b62014-02-10 15:10:22 -08007066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007068 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7069
7070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7071 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7072 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007073
7074 processKey(mapper, BTN_EXTRA, 0);
7075 processSync(mapper);
7076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007077 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007078 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007079
7080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007081 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007082 ASSERT_EQ(0, motionArgs.buttonState);
7083
Michael Wrightd02c5b62014-02-10 15:10:22 -08007084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7085 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7086 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7087
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7089
Michael Wrightd02c5b62014-02-10 15:10:22 -08007090 // press BTN_STYLUS, release BTN_STYLUS
7091 processKey(mapper, BTN_STYLUS, 1);
7092 processSync(mapper);
7093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007095 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7096
7097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7098 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7099 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007100
7101 processKey(mapper, BTN_STYLUS, 0);
7102 processSync(mapper);
7103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007104 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007105 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007106
7107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007109 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007110
7111 // press BTN_STYLUS2, release BTN_STYLUS2
7112 processKey(mapper, BTN_STYLUS2, 1);
7113 processSync(mapper);
7114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007116 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7117
7118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7119 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7120 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007121
7122 processKey(mapper, BTN_STYLUS2, 0);
7123 processSync(mapper);
7124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007125 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007126 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007127
7128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007130 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007131
7132 // release touch
7133 processUp(mapper);
7134 processSync(mapper);
7135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7136 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7137 ASSERT_EQ(0, motionArgs.buttonState);
7138}
7139
7140TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007141 addConfigurationProperty("touch.deviceType", "touchScreen");
7142 prepareDisplay(DISPLAY_ORIENTATION_0);
7143 prepareButtons();
7144 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007145 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007146
7147 NotifyMotionArgs motionArgs;
7148
7149 // default tool type is finger
7150 processDown(mapper, 100, 200);
7151 processSync(mapper);
7152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7153 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7154 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7155
7156 // eraser
7157 processKey(mapper, BTN_TOOL_RUBBER, 1);
7158 processSync(mapper);
7159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7160 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7161 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7162
7163 // stylus
7164 processKey(mapper, BTN_TOOL_RUBBER, 0);
7165 processKey(mapper, BTN_TOOL_PEN, 1);
7166 processSync(mapper);
7167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7169 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7170
7171 // brush
7172 processKey(mapper, BTN_TOOL_PEN, 0);
7173 processKey(mapper, BTN_TOOL_BRUSH, 1);
7174 processSync(mapper);
7175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7176 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7177 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7178
7179 // pencil
7180 processKey(mapper, BTN_TOOL_BRUSH, 0);
7181 processKey(mapper, BTN_TOOL_PENCIL, 1);
7182 processSync(mapper);
7183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7184 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7185 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7186
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007187 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007188 processKey(mapper, BTN_TOOL_PENCIL, 0);
7189 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7190 processSync(mapper);
7191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7193 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7194
7195 // mouse
7196 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7197 processKey(mapper, BTN_TOOL_MOUSE, 1);
7198 processSync(mapper);
7199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7201 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7202
7203 // lens
7204 processKey(mapper, BTN_TOOL_MOUSE, 0);
7205 processKey(mapper, BTN_TOOL_LENS, 1);
7206 processSync(mapper);
7207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7209 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7210
7211 // double-tap
7212 processKey(mapper, BTN_TOOL_LENS, 0);
7213 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7214 processSync(mapper);
7215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7217 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7218
7219 // triple-tap
7220 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7221 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7222 processSync(mapper);
7223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7224 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7225 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7226
7227 // quad-tap
7228 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7229 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7230 processSync(mapper);
7231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7233 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7234
7235 // finger
7236 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7237 processKey(mapper, BTN_TOOL_FINGER, 1);
7238 processSync(mapper);
7239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7241 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7242
7243 // stylus trumps finger
7244 processKey(mapper, BTN_TOOL_PEN, 1);
7245 processSync(mapper);
7246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7247 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7248 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7249
7250 // eraser trumps stylus
7251 processKey(mapper, BTN_TOOL_RUBBER, 1);
7252 processSync(mapper);
7253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7254 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7255 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7256
7257 // mouse trumps eraser
7258 processKey(mapper, BTN_TOOL_MOUSE, 1);
7259 processSync(mapper);
7260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7262 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7263
7264 // back to default tool type
7265 processKey(mapper, BTN_TOOL_MOUSE, 0);
7266 processKey(mapper, BTN_TOOL_RUBBER, 0);
7267 processKey(mapper, BTN_TOOL_PEN, 0);
7268 processKey(mapper, BTN_TOOL_FINGER, 0);
7269 processSync(mapper);
7270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7272 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7273}
7274
7275TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007276 addConfigurationProperty("touch.deviceType", "touchScreen");
7277 prepareDisplay(DISPLAY_ORIENTATION_0);
7278 prepareButtons();
7279 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007280 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007281 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007282
7283 NotifyMotionArgs motionArgs;
7284
7285 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7286 processKey(mapper, BTN_TOOL_FINGER, 1);
7287 processMove(mapper, 100, 200);
7288 processSync(mapper);
7289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7290 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7292 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7293
7294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7295 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7297 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7298
7299 // move a little
7300 processMove(mapper, 150, 250);
7301 processSync(mapper);
7302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7303 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7305 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7306
7307 // down when BTN_TOUCH is pressed, pressure defaults to 1
7308 processKey(mapper, BTN_TOUCH, 1);
7309 processSync(mapper);
7310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7311 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7313 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7314
7315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7316 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7318 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7319
7320 // up when BTN_TOUCH is released, hover restored
7321 processKey(mapper, BTN_TOUCH, 0);
7322 processSync(mapper);
7323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7324 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7326 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7327
7328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7329 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7331 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7332
7333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7334 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7336 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7337
7338 // exit hover when pointer goes away
7339 processKey(mapper, BTN_TOOL_FINGER, 0);
7340 processSync(mapper);
7341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7342 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7343 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7344 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7345}
7346
7347TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007348 addConfigurationProperty("touch.deviceType", "touchScreen");
7349 prepareDisplay(DISPLAY_ORIENTATION_0);
7350 prepareButtons();
7351 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007352 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007353
7354 NotifyMotionArgs motionArgs;
7355
7356 // initially hovering because pressure is 0
7357 processDown(mapper, 100, 200);
7358 processPressure(mapper, 0);
7359 processSync(mapper);
7360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7361 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7363 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7364
7365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7366 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7367 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7368 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7369
7370 // move a little
7371 processMove(mapper, 150, 250);
7372 processSync(mapper);
7373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7374 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7376 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7377
7378 // down when pressure is non-zero
7379 processPressure(mapper, RAW_PRESSURE_MAX);
7380 processSync(mapper);
7381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7382 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7384 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7385
7386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7387 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7389 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7390
7391 // up when pressure becomes 0, hover restored
7392 processPressure(mapper, 0);
7393 processSync(mapper);
7394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7395 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7396 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7397 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7398
7399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7400 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7402 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7403
7404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7405 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7407 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7408
7409 // exit hover when pointer goes away
7410 processUp(mapper);
7411 processSync(mapper);
7412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7413 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7415 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7416}
7417
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007418TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
7419 addConfigurationProperty("touch.deviceType", "touchScreen");
7420 prepareDisplay(DISPLAY_ORIENTATION_0);
7421 prepareButtons();
7422 prepareAxes(POSITION | PRESSURE);
7423 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7424
7425 // Touch down.
7426 processDown(mapper, 100, 200);
7427 processPressure(mapper, 1);
7428 processSync(mapper);
7429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7430 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
7431
7432 // Reset the mapper. This should cancel the ongoing gesture.
7433 resetMapper(mapper, ARBITRARY_TIME);
7434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7435 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
7436
7437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7438}
7439
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007440TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
7441 addConfigurationProperty("touch.deviceType", "touchScreen");
7442 prepareDisplay(DISPLAY_ORIENTATION_0);
7443 prepareButtons();
7444 prepareAxes(POSITION | PRESSURE);
7445 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7446
7447 // Set the initial state for the touch pointer.
7448 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
7449 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
7450 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
7451 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7452
7453 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007454 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
7455 // does not generate any events.
7456 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007457
7458 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
7459 // the recreated touch state to generate a down event.
7460 processSync(mapper);
7461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7462 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
7463
7464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7465}
7466
lilinnan687e58f2022-07-19 16:00:50 +08007467TEST_F(SingleTouchInputMapperTest,
7468 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
7469 addConfigurationProperty("touch.deviceType", "touchScreen");
7470 prepareDisplay(DISPLAY_ORIENTATION_0);
7471 prepareButtons();
7472 prepareAxes(POSITION);
7473 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7474 NotifyMotionArgs motionArgs;
7475
7476 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00007477 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08007478 processSync(mapper);
7479
7480 // We should receive a down event
7481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7482 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7483
7484 // Change display id
7485 clearViewports();
7486 prepareSecondaryDisplay(ViewportType::INTERNAL);
7487
7488 // We should receive a cancel event
7489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7490 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7491 // Then receive reset called
7492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7493}
7494
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007495TEST_F(SingleTouchInputMapperTest,
7496 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
7497 addConfigurationProperty("touch.deviceType", "touchScreen");
7498 prepareDisplay(DISPLAY_ORIENTATION_0);
7499 prepareButtons();
7500 prepareAxes(POSITION);
7501 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7503 NotifyMotionArgs motionArgs;
7504
7505 // Start a new gesture.
7506 processDown(mapper, 100, 200);
7507 processSync(mapper);
7508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7509 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7510
7511 // Make the viewport inactive. This will put the device in disabled mode.
7512 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7513 viewport->isActive = false;
7514 mFakePolicy->updateViewport(*viewport);
7515 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7516
7517 // We should receive a cancel event for the ongoing gesture.
7518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7519 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7520 // Then we should be notified that the device was reset.
7521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7522
7523 // No events are generated while the viewport is inactive.
7524 processMove(mapper, 101, 201);
7525 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007526 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007527 processSync(mapper);
7528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7529
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007530 // Start a new gesture while the viewport is still inactive.
7531 processDown(mapper, 300, 400);
7532 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
7533 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
7534 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7535 processSync(mapper);
7536
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007537 // Make the viewport active again. The device should resume processing events.
7538 viewport->isActive = true;
7539 mFakePolicy->updateViewport(*viewport);
7540 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7541
7542 // The device is reset because it changes back to direct mode, without generating any events.
7543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7545
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007546 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007547 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7549 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007550
7551 // No more events.
7552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
7554}
7555
Prabir Pradhan211ba622022-10-31 21:09:21 +00007556TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
7557 addConfigurationProperty("touch.deviceType", "touchScreen");
7558 prepareDisplay(DISPLAY_ORIENTATION_0);
7559 prepareButtons();
7560 prepareAxes(POSITION);
7561 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7563
7564 // Press a stylus button.
7565 processKey(mapper, BTN_STYLUS, 1);
7566 processSync(mapper);
7567
7568 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
7569 processDown(mapper, 100, 200);
7570 processSync(mapper);
7571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7572 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7573 WithCoords(toDisplayX(100), toDisplayY(200)),
7574 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7576 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7577 WithCoords(toDisplayX(100), toDisplayY(200)),
7578 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7579
7580 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
7581 // the button has not actually been released, since there will be no pointers through which the
7582 // button state can be reported. The event is generated at the location of the pointer before
7583 // it went up.
7584 processUp(mapper);
7585 processSync(mapper);
7586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7587 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7588 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7590 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7591 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7592}
7593
Prabir Pradhan5632d622021-09-06 07:57:20 -07007594// --- TouchDisplayProjectionTest ---
7595
7596class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7597public:
7598 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7599 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7600 // rotated equivalent of the given un-rotated physical display bounds.
7601 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
7602 uint32_t inverseRotationFlags;
7603 auto width = DISPLAY_WIDTH;
7604 auto height = DISPLAY_HEIGHT;
7605 switch (orientation) {
7606 case DISPLAY_ORIENTATION_90:
7607 inverseRotationFlags = ui::Transform::ROT_270;
7608 std::swap(width, height);
7609 break;
7610 case DISPLAY_ORIENTATION_180:
7611 inverseRotationFlags = ui::Transform::ROT_180;
7612 break;
7613 case DISPLAY_ORIENTATION_270:
7614 inverseRotationFlags = ui::Transform::ROT_90;
7615 std::swap(width, height);
7616 break;
7617 case DISPLAY_ORIENTATION_0:
7618 inverseRotationFlags = ui::Transform::ROT_0;
7619 break;
7620 default:
7621 FAIL() << "Invalid orientation: " << orientation;
7622 }
7623
7624 const ui::Transform rotation(inverseRotationFlags, width, height);
7625 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7626
7627 std::optional<DisplayViewport> internalViewport =
7628 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7629 DisplayViewport& v = *internalViewport;
7630 v.displayId = DISPLAY_ID;
7631 v.orientation = orientation;
7632
7633 v.logicalLeft = 0;
7634 v.logicalTop = 0;
7635 v.logicalRight = 100;
7636 v.logicalBottom = 100;
7637
7638 v.physicalLeft = rotatedPhysicalDisplay.left;
7639 v.physicalTop = rotatedPhysicalDisplay.top;
7640 v.physicalRight = rotatedPhysicalDisplay.right;
7641 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7642
7643 v.deviceWidth = width;
7644 v.deviceHeight = height;
7645
7646 v.isActive = true;
7647 v.uniqueId = UNIQUE_ID;
7648 v.type = ViewportType::INTERNAL;
7649 mFakePolicy->updateViewport(v);
7650 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7651 }
7652
7653 void assertReceivedMove(const Point& point) {
7654 NotifyMotionArgs motionArgs;
7655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7656 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7657 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7659 1, 0, 0, 0, 0, 0, 0, 0));
7660 }
7661};
7662
7663TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7664 addConfigurationProperty("touch.deviceType", "touchScreen");
7665 prepareDisplay(DISPLAY_ORIENTATION_0);
7666
7667 prepareButtons();
7668 prepareAxes(POSITION);
7669 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7670
7671 NotifyMotionArgs motionArgs;
7672
7673 // Configure the DisplayViewport such that the logical display maps to a subsection of
7674 // the display panel called the physical display. Here, the physical display is bounded by the
7675 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7676 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7677 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7678 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7679
7680 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7681 DISPLAY_ORIENTATION_270}) {
7682 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7683
7684 // Touches outside the physical display should be ignored, and should not generate any
7685 // events. Ensure touches at the following points that lie outside of the physical display
7686 // area do not generate any events.
7687 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7688 processDown(mapper, toRawX(point.x), toRawY(point.y));
7689 processSync(mapper);
7690 processUp(mapper);
7691 processSync(mapper);
7692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7693 << "Unexpected event generated for touch outside physical display at point: "
7694 << point.x << ", " << point.y;
7695 }
7696 }
7697}
7698
7699TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7700 addConfigurationProperty("touch.deviceType", "touchScreen");
7701 prepareDisplay(DISPLAY_ORIENTATION_0);
7702
7703 prepareButtons();
7704 prepareAxes(POSITION);
7705 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7706
7707 NotifyMotionArgs motionArgs;
7708
7709 // Configure the DisplayViewport such that the logical display maps to a subsection of
7710 // the display panel called the physical display. Here, the physical display is bounded by the
7711 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7712 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7713
7714 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7715 DISPLAY_ORIENTATION_270}) {
7716 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7717
7718 // Touches that start outside the physical display should be ignored until it enters the
7719 // physical display bounds, at which point it should generate a down event. Start a touch at
7720 // the point (5, 100), which is outside the physical display bounds.
7721 static const Point kOutsidePoint{5, 100};
7722 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7723 processSync(mapper);
7724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7725
7726 // Move the touch into the physical display area. This should generate a pointer down.
7727 processMove(mapper, toRawX(11), toRawY(21));
7728 processSync(mapper);
7729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7730 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7731 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7732 ASSERT_NO_FATAL_FAILURE(
7733 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7734
7735 // Move the touch inside the physical display area. This should generate a pointer move.
7736 processMove(mapper, toRawX(69), toRawY(159));
7737 processSync(mapper);
7738 assertReceivedMove({69, 159});
7739
7740 // Move outside the physical display area. Since the pointer is already down, this should
7741 // now continue generating events.
7742 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7743 processSync(mapper);
7744 assertReceivedMove(kOutsidePoint);
7745
7746 // Release. This should generate a pointer up.
7747 processUp(mapper);
7748 processSync(mapper);
7749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7750 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7752 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7753
7754 // Ensure no more events were generated.
7755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7757 }
7758}
7759
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007760// --- ExternalStylusFusionTest ---
7761
7762class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7763public:
7764 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7765 addConfigurationProperty("touch.deviceType", "touchScreen");
7766 prepareDisplay(DISPLAY_ORIENTATION_0);
7767 prepareButtons();
7768 prepareAxes(POSITION);
7769 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7770
7771 mStylusState.when = ARBITRARY_TIME;
7772 mStylusState.pressure = 0.f;
7773 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7774 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
7775 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
7776 processExternalStylusState(mapper);
7777 return mapper;
7778 }
7779
7780 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7781 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7782 for (const NotifyArgs& args : generatedArgs) {
7783 mFakeListener->notify(args);
7784 }
7785 // Loop the reader to flush the input listener queue.
7786 mReader->loopOnce();
7787 return generatedArgs;
7788 }
7789
7790protected:
7791 StylusState mStylusState{};
7792 static constexpr uint32_t EXPECTED_SOURCE =
7793 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7794
7795 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7796 auto toolTypeSource =
7797 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7798
7799 // The first pointer is withheld.
7800 processDown(mapper, 100, 200);
7801 processSync(mapper);
7802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7803 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7804 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7805
7806 // The external stylus reports pressure. The withheld finger pointer is released as a
7807 // stylus.
7808 mStylusState.pressure = 1.f;
7809 processExternalStylusState(mapper);
7810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7811 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7812 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7813
7814 // Subsequent pointer events are not withheld.
7815 processMove(mapper, 101, 201);
7816 processSync(mapper);
7817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7818 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7819
7820 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7822 }
7823
7824 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7825 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7826
7827 // Releasing the touch pointer ends the gesture.
7828 processUp(mapper);
7829 processSync(mapper);
7830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7831 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7832 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7833
7834 mStylusState.pressure = 0.f;
7835 processExternalStylusState(mapper);
7836 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7838 }
7839
7840 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7841 auto toolTypeSource =
7842 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER));
7843
7844 // The first pointer is withheld when an external stylus is connected,
7845 // and a timeout is requested.
7846 processDown(mapper, 100, 200);
7847 processSync(mapper);
7848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7849 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7850 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7851
7852 // If the timeout expires early, it is requested again.
7853 handleTimeout(mapper, ARBITRARY_TIME + 1);
7854 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7855 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7856
7857 // When the timeout expires, the withheld touch is released as a finger pointer.
7858 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7860 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7861
7862 // Subsequent pointer events are not withheld.
7863 processMove(mapper, 101, 201);
7864 processSync(mapper);
7865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7866 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7867 processUp(mapper);
7868 processSync(mapper);
7869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7870 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7871
7872 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7874 }
7875
7876private:
7877 InputDeviceInfo mExternalStylusDeviceInfo{};
7878};
7879
7880TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7881 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7882 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7883}
7884
7885TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7886 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7887 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7888}
7889
7890TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7891 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7892 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7893}
7894
7895// Test a successful stylus fusion gesture where the pressure is reported by the external
7896// before the touch is reported by the touchscreen.
7897TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7898 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7899 auto toolTypeSource =
7900 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7901
7902 // The external stylus reports pressure first. It is ignored for now.
7903 mStylusState.pressure = 1.f;
7904 processExternalStylusState(mapper);
7905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7906 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7907
7908 // When the touch goes down afterwards, it is reported as a stylus pointer.
7909 processDown(mapper, 100, 200);
7910 processSync(mapper);
7911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7912 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7913 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7914
7915 processMove(mapper, 101, 201);
7916 processSync(mapper);
7917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7918 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7919 processUp(mapper);
7920 processSync(mapper);
7921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7922 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7923
7924 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7926}
7927
7928TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7929 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7930
7931 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7932 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7933
7934 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7935 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7936 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7937 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7938}
7939
7940TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7941 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7942 auto toolTypeSource =
7943 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7944
7945 mStylusState.pressure = 0.8f;
7946 processExternalStylusState(mapper);
7947 processDown(mapper, 100, 200);
7948 processSync(mapper);
7949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7950 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7951 WithPressure(0.8f))));
7952 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7953
7954 // The external stylus reports a pressure change. We wait for some time for a touch event.
7955 mStylusState.pressure = 0.6f;
7956 processExternalStylusState(mapper);
7957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7958 ASSERT_NO_FATAL_FAILURE(
7959 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7960
7961 // If a touch is reported within the timeout, it reports the updated pressure.
7962 processMove(mapper, 101, 201);
7963 processSync(mapper);
7964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7965 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7966 WithPressure(0.6f))));
7967 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7968
7969 // There is another pressure change.
7970 mStylusState.pressure = 0.5f;
7971 processExternalStylusState(mapper);
7972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7973 ASSERT_NO_FATAL_FAILURE(
7974 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7975
7976 // If a touch is not reported within the timeout, a move event is generated to report
7977 // the new pressure.
7978 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7980 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7981 WithPressure(0.5f))));
7982
7983 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7984 // repeated indefinitely.
7985 mStylusState.pressure = 0.0f;
7986 processExternalStylusState(mapper);
7987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7988 ASSERT_NO_FATAL_FAILURE(
7989 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7990 processMove(mapper, 102, 202);
7991 processSync(mapper);
7992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7993 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7994 WithPressure(0.5f))));
7995 processMove(mapper, 103, 203);
7996 processSync(mapper);
7997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7998 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7999 WithPressure(0.5f))));
8000
8001 processUp(mapper);
8002 processSync(mapper);
8003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8004 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
8005 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
8006
8007 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8009}
8010
8011TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
8012 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8013 auto source = WithSource(EXPECTED_SOURCE);
8014
8015 mStylusState.pressure = 1.f;
8016 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_ERASER;
8017 processExternalStylusState(mapper);
8018 processDown(mapper, 100, 200);
8019 processSync(mapper);
8020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8021 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
8022 WithToolType(AMOTION_EVENT_TOOL_TYPE_ERASER))));
8023 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8024
8025 // The external stylus reports a tool change. We wait for some time for a touch event.
8026 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
8027 processExternalStylusState(mapper);
8028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8029 ASSERT_NO_FATAL_FAILURE(
8030 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8031
8032 // If a touch is reported within the timeout, it reports the updated pressure.
8033 processMove(mapper, 101, 201);
8034 processSync(mapper);
8035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8036 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8037 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
8038 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8039
8040 // There is another tool type change.
8041 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
8042 processExternalStylusState(mapper);
8043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8044 ASSERT_NO_FATAL_FAILURE(
8045 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8046
8047 // If a touch is not reported within the timeout, a move event is generated to report
8048 // the new tool type.
8049 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
8050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8051 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8052 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
8053
8054 processUp(mapper);
8055 processSync(mapper);
8056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8057 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
8058 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
8059
8060 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8062}
8063
8064TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
8065 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8066 auto toolTypeSource =
8067 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
8068
8069 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
8070
8071 // The external stylus reports a button change. We wait for some time for a touch event.
8072 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
8073 processExternalStylusState(mapper);
8074 ASSERT_NO_FATAL_FAILURE(
8075 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8076
8077 // If a touch is reported within the timeout, it reports the updated button state.
8078 processMove(mapper, 101, 201);
8079 processSync(mapper);
8080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8081 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8082 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8084 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8085 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8086 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8087
8088 // The button is now released.
8089 mStylusState.buttons = 0;
8090 processExternalStylusState(mapper);
8091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8092 ASSERT_NO_FATAL_FAILURE(
8093 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8094
8095 // If a touch is not reported within the timeout, a move event is generated to report
8096 // the new button state.
8097 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8099 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
8100 WithButtonState(0))));
8101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00008102 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8103 WithButtonState(0))));
8104
8105 processUp(mapper);
8106 processSync(mapper);
8107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008108 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8109
8110 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8112}
8113
Michael Wrightd02c5b62014-02-10 15:10:22 -08008114// --- MultiTouchInputMapperTest ---
8115
8116class MultiTouchInputMapperTest : public TouchInputMapperTest {
8117protected:
8118 void prepareAxes(int axes);
8119
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008120 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
8121 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
8122 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
8123 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
8124 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
8125 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
8126 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
8127 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
8128 void processId(MultiTouchInputMapper& mapper, int32_t id);
8129 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
8130 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
8131 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008132 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008133 void processMTSync(MultiTouchInputMapper& mapper);
8134 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008135};
8136
8137void MultiTouchInputMapperTest::prepareAxes(int axes) {
8138 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008139 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
8140 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008141 }
8142 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008143 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
8144 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008145 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008146 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
8147 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008148 }
8149 }
8150 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008151 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8152 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008153 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008154 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008155 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008156 }
8157 }
8158 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008159 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
8160 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008161 }
8162 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008163 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
8164 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008165 }
8166 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008167 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
8168 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008169 }
8170 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008171 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
8172 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008173 }
8174 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008175 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
8176 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008177 }
8178 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008179 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008180 }
8181}
8182
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008183void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
8184 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
8186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008187}
8188
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008189void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
8190 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008192}
8193
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008194void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
8195 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008196 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008197}
8198
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008199void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008200 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008201}
8202
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008203void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008205}
8206
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008207void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
8208 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008209 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008210}
8211
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008212void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008213 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008214}
8215
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008216void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008218}
8219
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008220void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008221 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008222}
8223
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008224void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008225 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008226}
8227
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008228void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008229 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008230}
8231
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008232void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
8233 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008235}
8236
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008237void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
8238 int32_t value) {
8239 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
8240 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
8241}
8242
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008243void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008244 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008245}
8246
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008247void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008248 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008249}
8250
Michael Wrightd02c5b62014-02-10 15:10:22 -08008251TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008252 addConfigurationProperty("touch.deviceType", "touchScreen");
8253 prepareDisplay(DISPLAY_ORIENTATION_0);
8254 prepareAxes(POSITION);
8255 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008256 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008257
arthurhungdcef2dc2020-08-11 14:47:50 +08008258 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008259
8260 NotifyMotionArgs motionArgs;
8261
8262 // Two fingers down at once.
8263 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8264 processPosition(mapper, x1, y1);
8265 processMTSync(mapper);
8266 processPosition(mapper, x2, y2);
8267 processMTSync(mapper);
8268 processSync(mapper);
8269
8270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8271 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8272 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8273 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8274 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8275 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8276 ASSERT_EQ(0, motionArgs.flags);
8277 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8278 ASSERT_EQ(0, motionArgs.buttonState);
8279 ASSERT_EQ(0, motionArgs.edgeFlags);
8280 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8281 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8282 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8283 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8284 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8285 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8286 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8287 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8288
8289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8290 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8291 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8292 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8293 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008294 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008295 ASSERT_EQ(0, motionArgs.flags);
8296 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8297 ASSERT_EQ(0, motionArgs.buttonState);
8298 ASSERT_EQ(0, motionArgs.edgeFlags);
8299 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8300 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8301 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8302 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8303 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8305 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8307 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8308 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8309 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8310 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8311
8312 // Move.
8313 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8314 processPosition(mapper, x1, y1);
8315 processMTSync(mapper);
8316 processPosition(mapper, x2, y2);
8317 processMTSync(mapper);
8318 processSync(mapper);
8319
8320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8321 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8322 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8323 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8324 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8326 ASSERT_EQ(0, motionArgs.flags);
8327 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8328 ASSERT_EQ(0, motionArgs.buttonState);
8329 ASSERT_EQ(0, motionArgs.edgeFlags);
8330 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8331 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8333 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8334 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8336 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8338 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8339 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8340 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8341 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8342
8343 // First finger up.
8344 x2 += 15; y2 -= 20;
8345 processPosition(mapper, x2, y2);
8346 processMTSync(mapper);
8347 processSync(mapper);
8348
8349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8350 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8351 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8352 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8353 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008354 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008355 ASSERT_EQ(0, motionArgs.flags);
8356 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8357 ASSERT_EQ(0, motionArgs.buttonState);
8358 ASSERT_EQ(0, motionArgs.edgeFlags);
8359 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8360 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8362 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8365 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8367 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8368 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8369 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8370 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8371
8372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8373 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8374 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8375 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8376 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8378 ASSERT_EQ(0, motionArgs.flags);
8379 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8380 ASSERT_EQ(0, motionArgs.buttonState);
8381 ASSERT_EQ(0, motionArgs.edgeFlags);
8382 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8383 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8384 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8386 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8387 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8388 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8389 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8390
8391 // Move.
8392 x2 += 20; y2 -= 25;
8393 processPosition(mapper, x2, y2);
8394 processMTSync(mapper);
8395 processSync(mapper);
8396
8397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8398 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8399 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8400 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8401 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8403 ASSERT_EQ(0, motionArgs.flags);
8404 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8405 ASSERT_EQ(0, motionArgs.buttonState);
8406 ASSERT_EQ(0, motionArgs.edgeFlags);
8407 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8408 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8409 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8411 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8412 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8413 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8414 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8415
8416 // New finger down.
8417 int32_t x3 = 700, y3 = 300;
8418 processPosition(mapper, x2, y2);
8419 processMTSync(mapper);
8420 processPosition(mapper, x3, y3);
8421 processMTSync(mapper);
8422 processSync(mapper);
8423
8424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8425 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8426 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8427 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8428 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008429 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008430 ASSERT_EQ(0, motionArgs.flags);
8431 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8432 ASSERT_EQ(0, motionArgs.buttonState);
8433 ASSERT_EQ(0, motionArgs.edgeFlags);
8434 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8435 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8436 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8437 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8438 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8440 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8442 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8443 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8444 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8445 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8446
8447 // Second finger up.
8448 x3 += 30; y3 -= 20;
8449 processPosition(mapper, x3, y3);
8450 processMTSync(mapper);
8451 processSync(mapper);
8452
8453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8454 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8455 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8456 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8457 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008458 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008459 ASSERT_EQ(0, motionArgs.flags);
8460 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8461 ASSERT_EQ(0, motionArgs.buttonState);
8462 ASSERT_EQ(0, motionArgs.edgeFlags);
8463 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8464 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8465 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8466 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8467 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8469 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8471 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8472 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8473 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8474 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8475
8476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8477 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8478 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8479 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8480 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8482 ASSERT_EQ(0, motionArgs.flags);
8483 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8484 ASSERT_EQ(0, motionArgs.buttonState);
8485 ASSERT_EQ(0, motionArgs.edgeFlags);
8486 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8487 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8488 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8490 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8491 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8492 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8493 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8494
8495 // Last finger up.
8496 processMTSync(mapper);
8497 processSync(mapper);
8498
8499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8500 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8501 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8502 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8503 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8504 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8505 ASSERT_EQ(0, motionArgs.flags);
8506 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8507 ASSERT_EQ(0, motionArgs.buttonState);
8508 ASSERT_EQ(0, motionArgs.edgeFlags);
8509 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8510 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8511 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8513 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8514 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8515 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8516 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8517
8518 // Should not have sent any more keys or motions.
8519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8521}
8522
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008523TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8524 addConfigurationProperty("touch.deviceType", "touchScreen");
8525 prepareDisplay(DISPLAY_ORIENTATION_0);
8526
8527 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8528 /*fuzz*/ 0, /*resolution*/ 10);
8529 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8530 /*fuzz*/ 0, /*resolution*/ 11);
8531 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8532 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8533 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8534 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8535 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8536 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8537 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8538 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8539
8540 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8541
8542 // X and Y axes
8543 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8544 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8545 // Touch major and minor
8546 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8547 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8548 // Tool major and minor
8549 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8550 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8551}
8552
8553TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8554 addConfigurationProperty("touch.deviceType", "touchScreen");
8555 prepareDisplay(DISPLAY_ORIENTATION_0);
8556
8557 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8558 /*fuzz*/ 0, /*resolution*/ 10);
8559 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8560 /*fuzz*/ 0, /*resolution*/ 11);
8561
8562 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8563
8564 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8565
8566 // Touch major and minor
8567 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8568 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8569 // Tool major and minor
8570 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8571 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8572}
8573
Michael Wrightd02c5b62014-02-10 15:10:22 -08008574TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008575 addConfigurationProperty("touch.deviceType", "touchScreen");
8576 prepareDisplay(DISPLAY_ORIENTATION_0);
8577 prepareAxes(POSITION | ID);
8578 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008579 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008580
arthurhungdcef2dc2020-08-11 14:47:50 +08008581 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008582
8583 NotifyMotionArgs motionArgs;
8584
8585 // Two fingers down at once.
8586 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8587 processPosition(mapper, x1, y1);
8588 processId(mapper, 1);
8589 processMTSync(mapper);
8590 processPosition(mapper, x2, y2);
8591 processId(mapper, 2);
8592 processMTSync(mapper);
8593 processSync(mapper);
8594
8595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8596 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8597 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8598 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8599 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8601 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8602
8603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008604 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008605 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8606 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8607 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8608 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8609 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8611 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8613 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8614
8615 // Move.
8616 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8617 processPosition(mapper, x1, y1);
8618 processId(mapper, 1);
8619 processMTSync(mapper);
8620 processPosition(mapper, x2, y2);
8621 processId(mapper, 2);
8622 processMTSync(mapper);
8623 processSync(mapper);
8624
8625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8627 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8628 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8630 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8631 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8633 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8634 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8635 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8636
8637 // First finger up.
8638 x2 += 15; y2 -= 20;
8639 processPosition(mapper, x2, y2);
8640 processId(mapper, 2);
8641 processMTSync(mapper);
8642 processSync(mapper);
8643
8644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008645 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008646 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8647 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8648 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8649 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8650 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8651 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8652 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8654 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8655
8656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8658 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8659 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8660 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8662 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8663
8664 // Move.
8665 x2 += 20; y2 -= 25;
8666 processPosition(mapper, x2, y2);
8667 processId(mapper, 2);
8668 processMTSync(mapper);
8669 processSync(mapper);
8670
8671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8672 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8673 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8674 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8677 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8678
8679 // New finger down.
8680 int32_t x3 = 700, y3 = 300;
8681 processPosition(mapper, x2, y2);
8682 processId(mapper, 2);
8683 processMTSync(mapper);
8684 processPosition(mapper, x3, y3);
8685 processId(mapper, 3);
8686 processMTSync(mapper);
8687 processSync(mapper);
8688
8689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008690 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008691 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8692 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8693 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8694 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8695 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8697 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8699 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8700
8701 // Second finger up.
8702 x3 += 30; y3 -= 20;
8703 processPosition(mapper, x3, y3);
8704 processId(mapper, 3);
8705 processMTSync(mapper);
8706 processSync(mapper);
8707
8708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008709 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008710 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8711 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8712 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8713 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8714 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8715 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8716 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8718 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8719
8720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8721 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8722 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8723 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8724 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8726 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8727
8728 // Last finger up.
8729 processMTSync(mapper);
8730 processSync(mapper);
8731
8732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8733 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8734 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8735 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8736 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8738 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8739
8740 // Should not have sent any more keys or motions.
8741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8743}
8744
8745TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008746 addConfigurationProperty("touch.deviceType", "touchScreen");
8747 prepareDisplay(DISPLAY_ORIENTATION_0);
8748 prepareAxes(POSITION | ID | SLOT);
8749 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008750 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008751
arthurhungdcef2dc2020-08-11 14:47:50 +08008752 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008753
8754 NotifyMotionArgs motionArgs;
8755
8756 // Two fingers down at once.
8757 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8758 processPosition(mapper, x1, y1);
8759 processId(mapper, 1);
8760 processSlot(mapper, 1);
8761 processPosition(mapper, x2, y2);
8762 processId(mapper, 2);
8763 processSync(mapper);
8764
8765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8766 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8767 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8768 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8769 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8770 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8771 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8772
8773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008774 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008775 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8776 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8777 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8778 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8779 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8780 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8781 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8783 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8784
8785 // Move.
8786 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8787 processSlot(mapper, 0);
8788 processPosition(mapper, x1, y1);
8789 processSlot(mapper, 1);
8790 processPosition(mapper, x2, y2);
8791 processSync(mapper);
8792
8793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8794 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8795 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8796 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8797 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8798 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8799 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8800 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8801 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8803 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8804
8805 // First finger up.
8806 x2 += 15; y2 -= 20;
8807 processSlot(mapper, 0);
8808 processId(mapper, -1);
8809 processSlot(mapper, 1);
8810 processPosition(mapper, x2, y2);
8811 processSync(mapper);
8812
8813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008814 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008815 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8816 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8817 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8818 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8819 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8821 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8823 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8824
8825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8826 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8827 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8828 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8829 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8831 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8832
8833 // Move.
8834 x2 += 20; y2 -= 25;
8835 processPosition(mapper, x2, y2);
8836 processSync(mapper);
8837
8838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8839 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8840 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8841 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8842 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8843 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8844 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8845
8846 // New finger down.
8847 int32_t x3 = 700, y3 = 300;
8848 processPosition(mapper, x2, y2);
8849 processSlot(mapper, 0);
8850 processId(mapper, 3);
8851 processPosition(mapper, x3, y3);
8852 processSync(mapper);
8853
8854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008855 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008856 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8857 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8858 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8859 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8860 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8862 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8863 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8864 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8865
8866 // Second finger up.
8867 x3 += 30; y3 -= 20;
8868 processSlot(mapper, 1);
8869 processId(mapper, -1);
8870 processSlot(mapper, 0);
8871 processPosition(mapper, x3, y3);
8872 processSync(mapper);
8873
8874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008875 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008876 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8877 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8878 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8879 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8880 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8881 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8882 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8883 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8884 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8885
8886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8887 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8888 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8889 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8890 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8891 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8892 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8893
8894 // Last finger up.
8895 processId(mapper, -1);
8896 processSync(mapper);
8897
8898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8899 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8900 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8901 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8902 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8903 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8904 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8905
8906 // Should not have sent any more keys or motions.
8907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8909}
8910
8911TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008912 addConfigurationProperty("touch.deviceType", "touchScreen");
8913 prepareDisplay(DISPLAY_ORIENTATION_0);
8914 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008915 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008916
8917 // These calculations are based on the input device calibration documentation.
8918 int32_t rawX = 100;
8919 int32_t rawY = 200;
8920 int32_t rawTouchMajor = 7;
8921 int32_t rawTouchMinor = 6;
8922 int32_t rawToolMajor = 9;
8923 int32_t rawToolMinor = 8;
8924 int32_t rawPressure = 11;
8925 int32_t rawDistance = 0;
8926 int32_t rawOrientation = 3;
8927 int32_t id = 5;
8928
8929 float x = toDisplayX(rawX);
8930 float y = toDisplayY(rawY);
8931 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8932 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8933 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8934 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8935 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8936 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8937 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8938 float distance = float(rawDistance);
8939
8940 processPosition(mapper, rawX, rawY);
8941 processTouchMajor(mapper, rawTouchMajor);
8942 processTouchMinor(mapper, rawTouchMinor);
8943 processToolMajor(mapper, rawToolMajor);
8944 processToolMinor(mapper, rawToolMinor);
8945 processPressure(mapper, rawPressure);
8946 processOrientation(mapper, rawOrientation);
8947 processDistance(mapper, rawDistance);
8948 processId(mapper, id);
8949 processMTSync(mapper);
8950 processSync(mapper);
8951
8952 NotifyMotionArgs args;
8953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8954 ASSERT_EQ(0, args.pointerProperties[0].id);
8955 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8956 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8957 orientation, distance));
8958}
8959
8960TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008961 addConfigurationProperty("touch.deviceType", "touchScreen");
8962 prepareDisplay(DISPLAY_ORIENTATION_0);
8963 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8964 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008965 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008966
8967 // These calculations are based on the input device calibration documentation.
8968 int32_t rawX = 100;
8969 int32_t rawY = 200;
8970 int32_t rawTouchMajor = 140;
8971 int32_t rawTouchMinor = 120;
8972 int32_t rawToolMajor = 180;
8973 int32_t rawToolMinor = 160;
8974
8975 float x = toDisplayX(rawX);
8976 float y = toDisplayY(rawY);
8977 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8978 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8979 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8980 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8981 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8982
8983 processPosition(mapper, rawX, rawY);
8984 processTouchMajor(mapper, rawTouchMajor);
8985 processTouchMinor(mapper, rawTouchMinor);
8986 processToolMajor(mapper, rawToolMajor);
8987 processToolMinor(mapper, rawToolMinor);
8988 processMTSync(mapper);
8989 processSync(mapper);
8990
8991 NotifyMotionArgs args;
8992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8994 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8995}
8996
8997TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008998 addConfigurationProperty("touch.deviceType", "touchScreen");
8999 prepareDisplay(DISPLAY_ORIENTATION_0);
9000 prepareAxes(POSITION | TOUCH | TOOL);
9001 addConfigurationProperty("touch.size.calibration", "diameter");
9002 addConfigurationProperty("touch.size.scale", "10");
9003 addConfigurationProperty("touch.size.bias", "160");
9004 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009005 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009006
9007 // These calculations are based on the input device calibration documentation.
9008 // Note: We only provide a single common touch/tool value because the device is assumed
9009 // not to emit separate values for each pointer (isSummed = 1).
9010 int32_t rawX = 100;
9011 int32_t rawY = 200;
9012 int32_t rawX2 = 150;
9013 int32_t rawY2 = 250;
9014 int32_t rawTouchMajor = 5;
9015 int32_t rawToolMajor = 8;
9016
9017 float x = toDisplayX(rawX);
9018 float y = toDisplayY(rawY);
9019 float x2 = toDisplayX(rawX2);
9020 float y2 = toDisplayY(rawY2);
9021 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
9022 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
9023 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
9024
9025 processPosition(mapper, rawX, rawY);
9026 processTouchMajor(mapper, rawTouchMajor);
9027 processToolMajor(mapper, rawToolMajor);
9028 processMTSync(mapper);
9029 processPosition(mapper, rawX2, rawY2);
9030 processTouchMajor(mapper, rawTouchMajor);
9031 processToolMajor(mapper, rawToolMajor);
9032 processMTSync(mapper);
9033 processSync(mapper);
9034
9035 NotifyMotionArgs args;
9036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9037 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9038
9039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009040 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009041 ASSERT_EQ(size_t(2), args.pointerCount);
9042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9043 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
9044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
9045 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
9046}
9047
9048TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009049 addConfigurationProperty("touch.deviceType", "touchScreen");
9050 prepareDisplay(DISPLAY_ORIENTATION_0);
9051 prepareAxes(POSITION | TOUCH | TOOL);
9052 addConfigurationProperty("touch.size.calibration", "area");
9053 addConfigurationProperty("touch.size.scale", "43");
9054 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009055 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009056
9057 // These calculations are based on the input device calibration documentation.
9058 int32_t rawX = 100;
9059 int32_t rawY = 200;
9060 int32_t rawTouchMajor = 5;
9061 int32_t rawToolMajor = 8;
9062
9063 float x = toDisplayX(rawX);
9064 float y = toDisplayY(rawY);
9065 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
9066 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
9067 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
9068
9069 processPosition(mapper, rawX, rawY);
9070 processTouchMajor(mapper, rawTouchMajor);
9071 processToolMajor(mapper, rawToolMajor);
9072 processMTSync(mapper);
9073 processSync(mapper);
9074
9075 NotifyMotionArgs args;
9076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9078 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
9079}
9080
9081TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009082 addConfigurationProperty("touch.deviceType", "touchScreen");
9083 prepareDisplay(DISPLAY_ORIENTATION_0);
9084 prepareAxes(POSITION | PRESSURE);
9085 addConfigurationProperty("touch.pressure.calibration", "amplitude");
9086 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009087 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009088
Michael Wrightaa449c92017-12-13 21:21:43 +00009089 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009090 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00009091 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
9092 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
9093 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
9094
Michael Wrightd02c5b62014-02-10 15:10:22 -08009095 // These calculations are based on the input device calibration documentation.
9096 int32_t rawX = 100;
9097 int32_t rawY = 200;
9098 int32_t rawPressure = 60;
9099
9100 float x = toDisplayX(rawX);
9101 float y = toDisplayY(rawY);
9102 float pressure = float(rawPressure) * 0.01f;
9103
9104 processPosition(mapper, rawX, rawY);
9105 processPressure(mapper, rawPressure);
9106 processMTSync(mapper);
9107 processSync(mapper);
9108
9109 NotifyMotionArgs args;
9110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9111 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9112 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
9113}
9114
9115TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009116 addConfigurationProperty("touch.deviceType", "touchScreen");
9117 prepareDisplay(DISPLAY_ORIENTATION_0);
9118 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009119 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009120
9121 NotifyMotionArgs motionArgs;
9122 NotifyKeyArgs keyArgs;
9123
9124 processId(mapper, 1);
9125 processPosition(mapper, 100, 200);
9126 processSync(mapper);
9127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9128 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9129 ASSERT_EQ(0, motionArgs.buttonState);
9130
9131 // press BTN_LEFT, release BTN_LEFT
9132 processKey(mapper, BTN_LEFT, 1);
9133 processSync(mapper);
9134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9135 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9136 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
9137
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9139 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9140 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
9141
Michael Wrightd02c5b62014-02-10 15:10:22 -08009142 processKey(mapper, BTN_LEFT, 0);
9143 processSync(mapper);
9144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009145 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009146 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009147
9148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009150 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009151
9152 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
9153 processKey(mapper, BTN_RIGHT, 1);
9154 processKey(mapper, BTN_MIDDLE, 1);
9155 processSync(mapper);
9156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9157 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9158 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
9159 motionArgs.buttonState);
9160
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9162 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9163 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
9164
9165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9166 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9167 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
9168 motionArgs.buttonState);
9169
Michael Wrightd02c5b62014-02-10 15:10:22 -08009170 processKey(mapper, BTN_RIGHT, 0);
9171 processSync(mapper);
9172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009173 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009174 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009175
9176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009177 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009178 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009179
9180 processKey(mapper, BTN_MIDDLE, 0);
9181 processSync(mapper);
9182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009184 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009185
9186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009187 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009188 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009189
9190 // press BTN_BACK, release BTN_BACK
9191 processKey(mapper, BTN_BACK, 1);
9192 processSync(mapper);
9193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9194 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9195 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009196
Michael Wrightd02c5b62014-02-10 15:10:22 -08009197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009199 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
9200
9201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9202 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9203 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009204
9205 processKey(mapper, BTN_BACK, 0);
9206 processSync(mapper);
9207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009208 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009209 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009210
9211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009213 ASSERT_EQ(0, motionArgs.buttonState);
9214
Michael Wrightd02c5b62014-02-10 15:10:22 -08009215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9216 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9217 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
9218
9219 // press BTN_SIDE, release BTN_SIDE
9220 processKey(mapper, BTN_SIDE, 1);
9221 processSync(mapper);
9222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9223 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9224 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009225
Michael Wrightd02c5b62014-02-10 15:10:22 -08009226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009228 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
9229
9230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9231 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9232 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009233
9234 processKey(mapper, BTN_SIDE, 0);
9235 processSync(mapper);
9236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009237 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009238 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009239
9240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009241 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009242 ASSERT_EQ(0, motionArgs.buttonState);
9243
Michael Wrightd02c5b62014-02-10 15:10:22 -08009244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9245 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9246 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
9247
9248 // press BTN_FORWARD, release BTN_FORWARD
9249 processKey(mapper, BTN_FORWARD, 1);
9250 processSync(mapper);
9251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9252 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9253 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009254
Michael Wrightd02c5b62014-02-10 15:10:22 -08009255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009256 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009257 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9258
9259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9260 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9261 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009262
9263 processKey(mapper, BTN_FORWARD, 0);
9264 processSync(mapper);
9265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009266 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009267 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009268
9269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009270 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009271 ASSERT_EQ(0, motionArgs.buttonState);
9272
Michael Wrightd02c5b62014-02-10 15:10:22 -08009273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9274 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9275 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9276
9277 // press BTN_EXTRA, release BTN_EXTRA
9278 processKey(mapper, BTN_EXTRA, 1);
9279 processSync(mapper);
9280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9281 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9282 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009283
Michael Wrightd02c5b62014-02-10 15:10:22 -08009284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009286 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9287
9288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9289 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9290 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009291
9292 processKey(mapper, BTN_EXTRA, 0);
9293 processSync(mapper);
9294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009295 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009296 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009297
9298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009299 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009300 ASSERT_EQ(0, motionArgs.buttonState);
9301
Michael Wrightd02c5b62014-02-10 15:10:22 -08009302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9303 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9304 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9305
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9307
Michael Wrightd02c5b62014-02-10 15:10:22 -08009308 // press BTN_STYLUS, release BTN_STYLUS
9309 processKey(mapper, BTN_STYLUS, 1);
9310 processSync(mapper);
9311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9312 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009313 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
9314
9315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9316 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9317 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009318
9319 processKey(mapper, BTN_STYLUS, 0);
9320 processSync(mapper);
9321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009322 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009323 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009324
9325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009327 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009328
9329 // press BTN_STYLUS2, release BTN_STYLUS2
9330 processKey(mapper, BTN_STYLUS2, 1);
9331 processSync(mapper);
9332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009334 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
9335
9336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9337 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9338 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009339
9340 processKey(mapper, BTN_STYLUS2, 0);
9341 processSync(mapper);
9342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009343 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009344 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009345
9346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009347 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009348 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009349
9350 // release touch
9351 processId(mapper, -1);
9352 processSync(mapper);
9353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9354 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9355 ASSERT_EQ(0, motionArgs.buttonState);
9356}
9357
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009358TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
9359 addConfigurationProperty("touch.deviceType", "touchScreen");
9360 prepareDisplay(DISPLAY_ORIENTATION_0);
9361 prepareAxes(POSITION | ID | SLOT);
9362 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9363
9364 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
9365 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
9366
9367 // Touch down.
9368 processId(mapper, 1);
9369 processPosition(mapper, 100, 200);
9370 processSync(mapper);
9371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9372 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
9373
9374 // Press and release button mapped to the primary stylus button.
9375 processKey(mapper, BTN_A, 1);
9376 processSync(mapper);
9377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9378 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9379 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9381 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9382 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9383
9384 processKey(mapper, BTN_A, 0);
9385 processSync(mapper);
9386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9387 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9389 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9390
9391 // Press and release the HID usage mapped to the secondary stylus button.
9392 processHidUsage(mapper, 0xabcd, 1);
9393 processSync(mapper);
9394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9395 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9396 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9398 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9399 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9400
9401 processHidUsage(mapper, 0xabcd, 0);
9402 processSync(mapper);
9403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9404 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9406 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9407
9408 // Release touch.
9409 processId(mapper, -1);
9410 processSync(mapper);
9411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9412 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
9413}
9414
Michael Wrightd02c5b62014-02-10 15:10:22 -08009415TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009416 addConfigurationProperty("touch.deviceType", "touchScreen");
9417 prepareDisplay(DISPLAY_ORIENTATION_0);
9418 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009419 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009420
9421 NotifyMotionArgs motionArgs;
9422
9423 // default tool type is finger
9424 processId(mapper, 1);
9425 processPosition(mapper, 100, 200);
9426 processSync(mapper);
9427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9428 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9429 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9430
9431 // eraser
9432 processKey(mapper, BTN_TOOL_RUBBER, 1);
9433 processSync(mapper);
9434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9435 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9436 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
9437
9438 // stylus
9439 processKey(mapper, BTN_TOOL_RUBBER, 0);
9440 processKey(mapper, BTN_TOOL_PEN, 1);
9441 processSync(mapper);
9442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9443 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9444 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9445
9446 // brush
9447 processKey(mapper, BTN_TOOL_PEN, 0);
9448 processKey(mapper, BTN_TOOL_BRUSH, 1);
9449 processSync(mapper);
9450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9452 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9453
9454 // pencil
9455 processKey(mapper, BTN_TOOL_BRUSH, 0);
9456 processKey(mapper, BTN_TOOL_PENCIL, 1);
9457 processSync(mapper);
9458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9460 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9461
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009462 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009463 processKey(mapper, BTN_TOOL_PENCIL, 0);
9464 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
9465 processSync(mapper);
9466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9468 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9469
9470 // mouse
9471 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9472 processKey(mapper, BTN_TOOL_MOUSE, 1);
9473 processSync(mapper);
9474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9475 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9476 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
9477
9478 // lens
9479 processKey(mapper, BTN_TOOL_MOUSE, 0);
9480 processKey(mapper, BTN_TOOL_LENS, 1);
9481 processSync(mapper);
9482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9483 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9484 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
9485
9486 // double-tap
9487 processKey(mapper, BTN_TOOL_LENS, 0);
9488 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
9489 processSync(mapper);
9490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9491 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9492 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9493
9494 // triple-tap
9495 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9496 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
9497 processSync(mapper);
9498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9500 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9501
9502 // quad-tap
9503 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9504 processKey(mapper, BTN_TOOL_QUADTAP, 1);
9505 processSync(mapper);
9506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9507 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9508 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9509
9510 // finger
9511 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9512 processKey(mapper, BTN_TOOL_FINGER, 1);
9513 processSync(mapper);
9514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9515 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9516 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9517
9518 // stylus trumps finger
9519 processKey(mapper, BTN_TOOL_PEN, 1);
9520 processSync(mapper);
9521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9522 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9523 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9524
9525 // eraser trumps stylus
9526 processKey(mapper, BTN_TOOL_RUBBER, 1);
9527 processSync(mapper);
9528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9529 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9530 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
9531
9532 // mouse trumps eraser
9533 processKey(mapper, BTN_TOOL_MOUSE, 1);
9534 processSync(mapper);
9535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9536 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
9538
9539 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9540 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9541 processSync(mapper);
9542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9543 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9544 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9545
9546 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9547 processToolType(mapper, MT_TOOL_PEN);
9548 processSync(mapper);
9549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9550 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9551 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9552
9553 // back to default tool type
9554 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9555 processKey(mapper, BTN_TOOL_MOUSE, 0);
9556 processKey(mapper, BTN_TOOL_RUBBER, 0);
9557 processKey(mapper, BTN_TOOL_PEN, 0);
9558 processKey(mapper, BTN_TOOL_FINGER, 0);
9559 processSync(mapper);
9560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9561 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9562 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9563}
9564
9565TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009566 addConfigurationProperty("touch.deviceType", "touchScreen");
9567 prepareDisplay(DISPLAY_ORIENTATION_0);
9568 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009569 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009570 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009571
9572 NotifyMotionArgs motionArgs;
9573
9574 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9575 processId(mapper, 1);
9576 processPosition(mapper, 100, 200);
9577 processSync(mapper);
9578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9579 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9581 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9582
9583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9584 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9586 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9587
9588 // move a little
9589 processPosition(mapper, 150, 250);
9590 processSync(mapper);
9591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9592 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9594 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9595
9596 // down when BTN_TOUCH is pressed, pressure defaults to 1
9597 processKey(mapper, BTN_TOUCH, 1);
9598 processSync(mapper);
9599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9600 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9602 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9603
9604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9605 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9607 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9608
9609 // up when BTN_TOUCH is released, hover restored
9610 processKey(mapper, BTN_TOUCH, 0);
9611 processSync(mapper);
9612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9613 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9615 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9616
9617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9618 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9620 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9621
9622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9623 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9625 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9626
9627 // exit hover when pointer goes away
9628 processId(mapper, -1);
9629 processSync(mapper);
9630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9631 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9633 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9634}
9635
9636TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009637 addConfigurationProperty("touch.deviceType", "touchScreen");
9638 prepareDisplay(DISPLAY_ORIENTATION_0);
9639 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009640 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009641
9642 NotifyMotionArgs motionArgs;
9643
9644 // initially hovering because pressure is 0
9645 processId(mapper, 1);
9646 processPosition(mapper, 100, 200);
9647 processPressure(mapper, 0);
9648 processSync(mapper);
9649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9650 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9651 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9652 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9653
9654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9655 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9657 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9658
9659 // move a little
9660 processPosition(mapper, 150, 250);
9661 processSync(mapper);
9662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9663 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9665 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9666
9667 // down when pressure becomes non-zero
9668 processPressure(mapper, RAW_PRESSURE_MAX);
9669 processSync(mapper);
9670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9671 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9673 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9674
9675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9676 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9678 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9679
9680 // up when pressure becomes 0, hover restored
9681 processPressure(mapper, 0);
9682 processSync(mapper);
9683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9684 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9686 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9687
9688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9689 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9690 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9691 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9692
9693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9694 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9696 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9697
9698 // exit hover when pointer goes away
9699 processId(mapper, -1);
9700 processSync(mapper);
9701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9702 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9704 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9705}
9706
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009707/**
9708 * Set the input device port <--> display port associations, and check that the
9709 * events are routed to the display that matches the display port.
9710 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9711 */
9712TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009713 const std::string usb2 = "USB2";
9714 const uint8_t hdmi1 = 0;
9715 const uint8_t hdmi2 = 1;
9716 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009717 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009718
9719 addConfigurationProperty("touch.deviceType", "touchScreen");
9720 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009721 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009722
9723 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9724 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9725
9726 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9727 // for this input device is specified, and the matching viewport is not present,
9728 // the input device should be disabled (at the mapper level).
9729
9730 // Add viewport for display 2 on hdmi2
9731 prepareSecondaryDisplay(type, hdmi2);
9732 // Send a touch event
9733 processPosition(mapper, 100, 100);
9734 processSync(mapper);
9735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9736
9737 // Add viewport for display 1 on hdmi1
9738 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
9739 // Send a touch event again
9740 processPosition(mapper, 100, 100);
9741 processSync(mapper);
9742
9743 NotifyMotionArgs args;
9744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9745 ASSERT_EQ(DISPLAY_ID, args.displayId);
9746}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009747
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009748TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9749 addConfigurationProperty("touch.deviceType", "touchScreen");
9750 prepareAxes(POSITION);
9751 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9752
9753 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9754
9755 prepareDisplay(DISPLAY_ORIENTATION_0);
9756 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9757
9758 // Send a touch event
9759 processPosition(mapper, 100, 100);
9760 processSync(mapper);
9761
9762 NotifyMotionArgs args;
9763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9764 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9765}
9766
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009767TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009768 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009769 std::shared_ptr<FakePointerController> fakePointerController =
9770 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009771 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009772 fakePointerController->setPosition(100, 200);
9773 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009774 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009775
Garfield Tan888a6a42020-01-09 11:39:16 -08009776 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009777 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009778
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009779 prepareDisplay(DISPLAY_ORIENTATION_0);
9780 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009781 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009782
Harry Cutts16a24cc2022-10-26 15:22:19 +00009783 // Check source is a touchpad that would obtain the PointerController.
9784 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009785
9786 NotifyMotionArgs motionArgs;
9787 processPosition(mapper, 100, 100);
9788 processSync(mapper);
9789
9790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9791 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9792 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9793}
9794
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009795/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009796 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9797 */
9798TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9799 addConfigurationProperty("touch.deviceType", "touchScreen");
9800 prepareAxes(POSITION);
9801 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9802
9803 prepareDisplay(DISPLAY_ORIENTATION_0);
9804 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
9805 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
9806 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
9807 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9808
9809 NotifyMotionArgs args;
9810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9811 ASSERT_EQ(26, args.readTime);
9812
9813 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
9814 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
9815 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9816
9817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9818 ASSERT_EQ(33, args.readTime);
9819}
9820
9821/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009822 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9823 * events should not be delivered to the listener.
9824 */
9825TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9826 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009827 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009828 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9829 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
9830 ViewportType::INTERNAL);
9831 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9832 prepareAxes(POSITION);
9833 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9834
9835 NotifyMotionArgs motionArgs;
9836 processPosition(mapper, 100, 100);
9837 processSync(mapper);
9838
9839 mFakeListener->assertNotifyMotionWasNotCalled();
9840}
9841
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009842/**
9843 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9844 * the touch mapper can process the events and the events can be delivered to the listener.
9845 */
9846TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9847 addConfigurationProperty("touch.deviceType", "touchScreen");
9848 addConfigurationProperty("touch.enableForInactiveViewport", "1");
9849 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9850 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
9851 ViewportType::INTERNAL);
9852 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9853 prepareAxes(POSITION);
9854 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9855
9856 NotifyMotionArgs motionArgs;
9857 processPosition(mapper, 100, 100);
9858 processSync(mapper);
9859
9860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9861 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9862}
9863
Garfield Tanc734e4f2021-01-15 20:01:39 -08009864TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9865 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009866 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Garfield Tanc734e4f2021-01-15 20:01:39 -08009867 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9868 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
9869 ViewportType::INTERNAL);
9870 std::optional<DisplayViewport> optionalDisplayViewport =
9871 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9872 ASSERT_TRUE(optionalDisplayViewport.has_value());
9873 DisplayViewport displayViewport = *optionalDisplayViewport;
9874
9875 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9876 prepareAxes(POSITION);
9877 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9878
9879 // Finger down
9880 int32_t x = 100, y = 100;
9881 processPosition(mapper, x, y);
9882 processSync(mapper);
9883
9884 NotifyMotionArgs motionArgs;
9885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9886 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9887
9888 // Deactivate display viewport
9889 displayViewport.isActive = false;
9890 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9891 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9892
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009893 // The ongoing touch should be canceled immediately
9894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9895 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9896
9897 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009898 x += 10, y += 10;
9899 processPosition(mapper, x, y);
9900 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009902
9903 // Reactivate display viewport
9904 displayViewport.isActive = true;
9905 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9906 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9907
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009908 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009909 x += 10, y += 10;
9910 processPosition(mapper, x, y);
9911 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9913 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009914}
9915
Arthur Hung7c645402019-01-25 17:45:42 +08009916TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9917 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009918 prepareAxes(POSITION | ID | SLOT);
9919 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009920 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009921
9922 // Create the second touch screen device, and enable multi fingers.
9923 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009924 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009925 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009926 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009927 std::shared_ptr<InputDevice> device2 =
9928 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009929 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009930
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009931 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9932 0 /*flat*/, 0 /*fuzz*/);
9933 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9934 0 /*flat*/, 0 /*fuzz*/);
9935 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
9936 0 /*flat*/, 0 /*fuzz*/);
9937 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
9938 0 /*flat*/, 0 /*fuzz*/);
9939 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
9940 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9941 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009942
9943 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009944 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009945 std::list<NotifyArgs> unused =
9946 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9947 0 /*changes*/);
9948 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009949
9950 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009951 std::shared_ptr<FakePointerController> fakePointerController =
9952 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009953 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009954
9955 // Setup policy for associated displays and show touches.
9956 const uint8_t hdmi1 = 0;
9957 const uint8_t hdmi2 = 1;
9958 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9959 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9960 mFakePolicy->setShowTouches(true);
9961
9962 // Create displays.
9963 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009964 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009965
9966 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009967 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9968 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9969 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009970
9971 // Two fingers down at default display.
9972 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9973 processPosition(mapper, x1, y1);
9974 processId(mapper, 1);
9975 processSlot(mapper, 1);
9976 processPosition(mapper, x2, y2);
9977 processId(mapper, 2);
9978 processSync(mapper);
9979
9980 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9981 fakePointerController->getSpots().find(DISPLAY_ID);
9982 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9983 ASSERT_EQ(size_t(2), iter->second.size());
9984
9985 // Two fingers down at second display.
9986 processPosition(mapper2, x1, y1);
9987 processId(mapper2, 1);
9988 processSlot(mapper2, 1);
9989 processPosition(mapper2, x2, y2);
9990 processId(mapper2, 2);
9991 processSync(mapper2);
9992
9993 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9994 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9995 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009996
9997 // Disable the show touches configuration and ensure the spots are cleared.
9998 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009999 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10000 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +000010001
10002 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +080010003}
10004
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010005TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010006 prepareAxes(POSITION);
10007 addConfigurationProperty("touch.deviceType", "touchScreen");
10008 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010009 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010010
10011 NotifyMotionArgs motionArgs;
10012 // Unrotated video frame
10013 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10014 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010015 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010016 processPosition(mapper, 100, 200);
10017 processSync(mapper);
10018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10019 ASSERT_EQ(frames, motionArgs.videoFrames);
10020
10021 // Subsequent touch events should not have any videoframes
10022 // This is implemented separately in FakeEventHub,
10023 // but that should match the behaviour of TouchVideoDevice.
10024 processPosition(mapper, 200, 200);
10025 processSync(mapper);
10026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10027 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
10028}
10029
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010030TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010031 prepareAxes(POSITION);
10032 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010033 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010034 // Unrotated video frame
10035 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10036 NotifyMotionArgs motionArgs;
10037
10038 // Test all 4 orientations
10039 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010040 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
10041 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
10042 clearViewports();
10043 prepareDisplay(orientation);
10044 std::vector<TouchVideoFrame> frames{frame};
10045 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
10046 processPosition(mapper, 100, 200);
10047 processSync(mapper);
10048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10049 ASSERT_EQ(frames, motionArgs.videoFrames);
10050 }
10051}
10052
10053TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
10054 prepareAxes(POSITION);
10055 addConfigurationProperty("touch.deviceType", "touchScreen");
10056 // Since InputReader works in the un-rotated coordinate space, only devices that are not
10057 // orientation-aware are affected by display rotation.
10058 addConfigurationProperty("touch.orientationAware", "0");
10059 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10060 // Unrotated video frame
10061 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10062 NotifyMotionArgs motionArgs;
10063
10064 // Test all 4 orientations
10065 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010066 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
10067 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
10068 clearViewports();
10069 prepareDisplay(orientation);
10070 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010071 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010072 processPosition(mapper, 100, 200);
10073 processSync(mapper);
10074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010075 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
10076 // compared to the display. This is so that when the window transform (which contains the
10077 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
10078 // window's coordinate space.
10079 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010080 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +080010081
10082 // Release finger.
10083 processSync(mapper);
10084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010085 }
10086}
10087
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010088TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010089 prepareAxes(POSITION);
10090 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010091 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010092 // Unrotated video frames. There's no rule that they must all have the same dimensions,
10093 // so mix these.
10094 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10095 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
10096 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
10097 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
10098 NotifyMotionArgs motionArgs;
10099
10100 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010101 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010102 processPosition(mapper, 100, 200);
10103 processSync(mapper);
10104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010105 ASSERT_EQ(frames, motionArgs.videoFrames);
10106}
10107
10108TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
10109 prepareAxes(POSITION);
10110 addConfigurationProperty("touch.deviceType", "touchScreen");
10111 // Since InputReader works in the un-rotated coordinate space, only devices that are not
10112 // orientation-aware are affected by display rotation.
10113 addConfigurationProperty("touch.orientationAware", "0");
10114 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10115 // Unrotated video frames. There's no rule that they must all have the same dimensions,
10116 // so mix these.
10117 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10118 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
10119 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
10120 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
10121 NotifyMotionArgs motionArgs;
10122
10123 prepareDisplay(DISPLAY_ORIENTATION_90);
10124 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
10125 processPosition(mapper, 100, 200);
10126 processSync(mapper);
10127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10128 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
10129 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
10130 // compared to the display. This is so that when the window transform (which contains the
10131 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
10132 // window's coordinate space.
10133 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
10134 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010135 ASSERT_EQ(frames, motionArgs.videoFrames);
10136}
10137
Arthur Hung9da14732019-09-02 16:16:58 +080010138/**
10139 * If we had defined port associations, but the viewport is not ready, the touch device would be
10140 * expected to be disabled, and it should be enabled after the viewport has found.
10141 */
10142TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +080010143 constexpr uint8_t hdmi2 = 1;
10144 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010145 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +080010146
10147 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
10148
10149 addConfigurationProperty("touch.deviceType", "touchScreen");
10150 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010151 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +080010152
10153 ASSERT_EQ(mDevice->isEnabled(), false);
10154
10155 // Add display on hdmi2, the device should be enabled and can receive touch event.
10156 prepareSecondaryDisplay(type, hdmi2);
10157 ASSERT_EQ(mDevice->isEnabled(), true);
10158
10159 // Send a touch event.
10160 processPosition(mapper, 100, 100);
10161 processSync(mapper);
10162
10163 NotifyMotionArgs args;
10164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10165 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
10166}
10167
Arthur Hung421eb1c2020-01-16 00:09:42 +080010168TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010169 addConfigurationProperty("touch.deviceType", "touchScreen");
10170 prepareDisplay(DISPLAY_ORIENTATION_0);
10171 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010172 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010173
10174 NotifyMotionArgs motionArgs;
10175
10176 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10177 // finger down
10178 processId(mapper, 1);
10179 processPosition(mapper, x1, y1);
10180 processSync(mapper);
10181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10182 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10183 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10184
10185 // finger move
10186 processId(mapper, 1);
10187 processPosition(mapper, x2, y2);
10188 processSync(mapper);
10189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10192
10193 // finger up.
10194 processId(mapper, -1);
10195 processSync(mapper);
10196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10197 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10198 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10199
10200 // new finger down
10201 processId(mapper, 1);
10202 processPosition(mapper, x3, y3);
10203 processSync(mapper);
10204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10205 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10206 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10207}
10208
10209/**
arthurhungcc7f9802020-04-30 17:55:40 +080010210 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
10211 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +080010212 */
arthurhungcc7f9802020-04-30 17:55:40 +080010213TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010214 addConfigurationProperty("touch.deviceType", "touchScreen");
10215 prepareDisplay(DISPLAY_ORIENTATION_0);
10216 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010217 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010218
10219 NotifyMotionArgs motionArgs;
10220
10221 // default tool type is finger
10222 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +080010223 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010224 processPosition(mapper, x1, y1);
10225 processSync(mapper);
10226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10227 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10228 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10229
10230 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
10231 processToolType(mapper, MT_TOOL_PALM);
10232 processSync(mapper);
10233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10234 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10235
10236 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +080010237 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010238 processPosition(mapper, x2, y2);
10239 processSync(mapper);
10240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10241
10242 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +080010243 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010244 processSync(mapper);
10245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10246
10247 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +080010248 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010249 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010250 processPosition(mapper, x3, y3);
10251 processSync(mapper);
10252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10253 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10255}
10256
arthurhungbf89a482020-04-17 17:37:55 +080010257/**
arthurhungcc7f9802020-04-30 17:55:40 +080010258 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10259 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +080010260 */
arthurhungcc7f9802020-04-30 17:55:40 +080010261TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +080010262 addConfigurationProperty("touch.deviceType", "touchScreen");
10263 prepareDisplay(DISPLAY_ORIENTATION_0);
10264 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
10265 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10266
10267 NotifyMotionArgs motionArgs;
10268
10269 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +080010270 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10271 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010272 processPosition(mapper, x1, y1);
10273 processSync(mapper);
10274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10275 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10276 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10277
10278 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +080010279 processSlot(mapper, SECOND_SLOT);
10280 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010281 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +080010282 processSync(mapper);
10283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010284 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010285 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
10286
10287 // If the tool type of the first finger changes to MT_TOOL_PALM,
10288 // we expect to receive ACTION_POINTER_UP with cancel flag.
10289 processSlot(mapper, FIRST_SLOT);
10290 processId(mapper, FIRST_TRACKING_ID);
10291 processToolType(mapper, MT_TOOL_PALM);
10292 processSync(mapper);
10293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010294 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010295 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10296
10297 // The following MOVE events of second finger should be processed.
10298 processSlot(mapper, SECOND_SLOT);
10299 processId(mapper, SECOND_TRACKING_ID);
10300 processPosition(mapper, x2 + 1, y2 + 1);
10301 processSync(mapper);
10302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10304 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10305
10306 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
10307 // it. Second finger receive move.
10308 processSlot(mapper, FIRST_SLOT);
10309 processId(mapper, INVALID_TRACKING_ID);
10310 processSync(mapper);
10311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10312 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10313 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10314
10315 // Second finger keeps moving.
10316 processSlot(mapper, SECOND_SLOT);
10317 processId(mapper, SECOND_TRACKING_ID);
10318 processPosition(mapper, x2 + 2, y2 + 2);
10319 processSync(mapper);
10320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10321 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10322 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10323
10324 // Second finger up.
10325 processId(mapper, INVALID_TRACKING_ID);
10326 processSync(mapper);
10327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10328 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10329 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10330}
10331
10332/**
10333 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
10334 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
10335 */
10336TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
10337 addConfigurationProperty("touch.deviceType", "touchScreen");
10338 prepareDisplay(DISPLAY_ORIENTATION_0);
10339 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
10340 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10341
10342 NotifyMotionArgs motionArgs;
10343
10344 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10345 // First finger down.
10346 processId(mapper, FIRST_TRACKING_ID);
10347 processPosition(mapper, x1, y1);
10348 processSync(mapper);
10349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10350 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10351 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10352
10353 // Second finger down.
10354 processSlot(mapper, SECOND_SLOT);
10355 processId(mapper, SECOND_TRACKING_ID);
10356 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +080010357 processSync(mapper);
10358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010359 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +080010360 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10361
arthurhungcc7f9802020-04-30 17:55:40 +080010362 // If the tool type of the first finger changes to MT_TOOL_PALM,
10363 // we expect to receive ACTION_POINTER_UP with cancel flag.
10364 processSlot(mapper, FIRST_SLOT);
10365 processId(mapper, FIRST_TRACKING_ID);
10366 processToolType(mapper, MT_TOOL_PALM);
10367 processSync(mapper);
10368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010369 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010370 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10371
10372 // Second finger keeps moving.
10373 processSlot(mapper, SECOND_SLOT);
10374 processId(mapper, SECOND_TRACKING_ID);
10375 processPosition(mapper, x2 + 1, y2 + 1);
10376 processSync(mapper);
10377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10378 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10379
10380 // second finger becomes palm, receive cancel due to only 1 finger is active.
10381 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010382 processToolType(mapper, MT_TOOL_PALM);
10383 processSync(mapper);
10384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10385 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10386
arthurhungcc7f9802020-04-30 17:55:40 +080010387 // third finger down.
10388 processSlot(mapper, THIRD_SLOT);
10389 processId(mapper, THIRD_TRACKING_ID);
10390 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +080010391 processPosition(mapper, x3, y3);
10392 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +080010393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10394 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10395 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010396 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10397
10398 // third finger move
10399 processId(mapper, THIRD_TRACKING_ID);
10400 processPosition(mapper, x3 + 1, y3 + 1);
10401 processSync(mapper);
10402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10403 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10404
10405 // first finger up, third finger receive move.
10406 processSlot(mapper, FIRST_SLOT);
10407 processId(mapper, INVALID_TRACKING_ID);
10408 processSync(mapper);
10409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10411 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10412
10413 // second finger up, third finger receive move.
10414 processSlot(mapper, SECOND_SLOT);
10415 processId(mapper, INVALID_TRACKING_ID);
10416 processSync(mapper);
10417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10419 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10420
10421 // third finger up.
10422 processSlot(mapper, THIRD_SLOT);
10423 processId(mapper, INVALID_TRACKING_ID);
10424 processSync(mapper);
10425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10426 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10427 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10428}
10429
10430/**
10431 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10432 * and the active finger could still be allowed to receive the events
10433 */
10434TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
10435 addConfigurationProperty("touch.deviceType", "touchScreen");
10436 prepareDisplay(DISPLAY_ORIENTATION_0);
10437 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
10438 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10439
10440 NotifyMotionArgs motionArgs;
10441
10442 // default tool type is finger
10443 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10444 processId(mapper, FIRST_TRACKING_ID);
10445 processPosition(mapper, x1, y1);
10446 processSync(mapper);
10447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10448 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10449 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10450
10451 // Second finger down.
10452 processSlot(mapper, SECOND_SLOT);
10453 processId(mapper, SECOND_TRACKING_ID);
10454 processPosition(mapper, x2, y2);
10455 processSync(mapper);
10456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010457 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010458 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10459
10460 // If the tool type of the second finger changes to MT_TOOL_PALM,
10461 // we expect to receive ACTION_POINTER_UP with cancel flag.
10462 processId(mapper, SECOND_TRACKING_ID);
10463 processToolType(mapper, MT_TOOL_PALM);
10464 processSync(mapper);
10465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010466 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010467 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10468
10469 // The following MOVE event should be processed.
10470 processSlot(mapper, FIRST_SLOT);
10471 processId(mapper, FIRST_TRACKING_ID);
10472 processPosition(mapper, x1 + 1, y1 + 1);
10473 processSync(mapper);
10474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10475 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10476 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10477
10478 // second finger up.
10479 processSlot(mapper, SECOND_SLOT);
10480 processId(mapper, INVALID_TRACKING_ID);
10481 processSync(mapper);
10482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10483 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10484
10485 // first finger keep moving
10486 processSlot(mapper, FIRST_SLOT);
10487 processId(mapper, FIRST_TRACKING_ID);
10488 processPosition(mapper, x1 + 2, y1 + 2);
10489 processSync(mapper);
10490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10491 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10492
10493 // first finger up.
10494 processId(mapper, INVALID_TRACKING_ID);
10495 processSync(mapper);
10496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10497 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10498 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010499}
10500
Arthur Hung9ad18942021-06-19 02:04:46 +000010501/**
10502 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10503 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10504 * cause slot be valid again.
10505 */
10506TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10507 addConfigurationProperty("touch.deviceType", "touchScreen");
10508 prepareDisplay(DISPLAY_ORIENTATION_0);
10509 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10510 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10511
10512 NotifyMotionArgs motionArgs;
10513
10514 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10515 // First finger down.
10516 processId(mapper, FIRST_TRACKING_ID);
10517 processPosition(mapper, x1, y1);
10518 processPressure(mapper, RAW_PRESSURE_MAX);
10519 processSync(mapper);
10520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10521 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10522 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10523
10524 // First finger move.
10525 processId(mapper, FIRST_TRACKING_ID);
10526 processPosition(mapper, x1 + 1, y1 + 1);
10527 processPressure(mapper, RAW_PRESSURE_MAX);
10528 processSync(mapper);
10529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10530 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10531 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10532
10533 // Second finger down.
10534 processSlot(mapper, SECOND_SLOT);
10535 processId(mapper, SECOND_TRACKING_ID);
10536 processPosition(mapper, x2, y2);
10537 processPressure(mapper, RAW_PRESSURE_MAX);
10538 processSync(mapper);
10539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010540 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010541 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10542
10543 // second finger up with some unexpected data.
10544 processSlot(mapper, SECOND_SLOT);
10545 processId(mapper, INVALID_TRACKING_ID);
10546 processPosition(mapper, x2, y2);
10547 processSync(mapper);
10548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010549 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010550 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10551
10552 // first finger up with some unexpected data.
10553 processSlot(mapper, FIRST_SLOT);
10554 processId(mapper, INVALID_TRACKING_ID);
10555 processPosition(mapper, x2, y2);
10556 processPressure(mapper, RAW_PRESSURE_MAX);
10557 processSync(mapper);
10558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10559 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10560 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10561}
10562
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010563TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10564 addConfigurationProperty("touch.deviceType", "touchScreen");
10565 prepareDisplay(DISPLAY_ORIENTATION_0);
10566 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10567 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10568
10569 // First finger down.
10570 processId(mapper, FIRST_TRACKING_ID);
10571 processPosition(mapper, 100, 200);
10572 processPressure(mapper, RAW_PRESSURE_MAX);
10573 processSync(mapper);
10574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10575 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10576
10577 // Second finger down.
10578 processSlot(mapper, SECOND_SLOT);
10579 processId(mapper, SECOND_TRACKING_ID);
10580 processPosition(mapper, 300, 400);
10581 processPressure(mapper, RAW_PRESSURE_MAX);
10582 processSync(mapper);
10583 ASSERT_NO_FATAL_FAILURE(
10584 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10585
10586 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010587 // preserved. Resetting should cancel the ongoing gesture.
10588 resetMapper(mapper, ARBITRARY_TIME);
10589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10590 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010591
10592 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10593 // the existing touch state to generate a down event.
10594 processPosition(mapper, 301, 302);
10595 processSync(mapper);
10596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10597 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10599 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10600
10601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10602}
10603
10604TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10605 addConfigurationProperty("touch.deviceType", "touchScreen");
10606 prepareDisplay(DISPLAY_ORIENTATION_0);
10607 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10608 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10609
10610 // First finger touches down and releases.
10611 processId(mapper, FIRST_TRACKING_ID);
10612 processPosition(mapper, 100, 200);
10613 processPressure(mapper, RAW_PRESSURE_MAX);
10614 processSync(mapper);
10615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10616 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10617 processId(mapper, INVALID_TRACKING_ID);
10618 processSync(mapper);
10619 ASSERT_NO_FATAL_FAILURE(
10620 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10621
10622 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10623 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010624 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10626
10627 // Send an empty sync frame. Since there are no pointers, no events are generated.
10628 processSync(mapper);
10629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10630}
10631
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010632// --- MultiTouchInputMapperTest_ExternalDevice ---
10633
10634class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10635protected:
Chris Yea52ade12020-08-27 16:49:20 -070010636 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010637};
10638
10639/**
10640 * Expect fallback to internal viewport if device is external and external viewport is not present.
10641 */
10642TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10643 prepareAxes(POSITION);
10644 addConfigurationProperty("touch.deviceType", "touchScreen");
10645 prepareDisplay(DISPLAY_ORIENTATION_0);
10646 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10647
10648 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10649
10650 NotifyMotionArgs motionArgs;
10651
10652 // Expect the event to be sent to the internal viewport,
10653 // because an external viewport is not present.
10654 processPosition(mapper, 100, 100);
10655 processSync(mapper);
10656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10657 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10658
10659 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010660 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010661 processPosition(mapper, 100, 100);
10662 processSync(mapper);
10663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10664 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10665}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010666
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010667TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10668 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10669 std::shared_ptr<FakePointerController> fakePointerController =
10670 std::make_shared<FakePointerController>();
10671 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10672 fakePointerController->setPosition(0, 0);
10673 fakePointerController->setButtonState(0);
10674
10675 // prepare device and capture
10676 prepareDisplay(DISPLAY_ORIENTATION_0);
10677 prepareAxes(POSITION | ID | SLOT);
10678 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10679 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10680 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010681 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010682 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10683
10684 // captured touchpad should be a touchpad source
10685 NotifyDeviceResetArgs resetArgs;
10686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10687 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10688
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010689 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010690
10691 const InputDeviceInfo::MotionRange* relRangeX =
10692 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10693 ASSERT_NE(relRangeX, nullptr);
10694 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10695 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10696 const InputDeviceInfo::MotionRange* relRangeY =
10697 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10698 ASSERT_NE(relRangeY, nullptr);
10699 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10700 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10701
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010702 // run captured pointer tests - note that this is unscaled, so input listener events should be
10703 // identical to what the hardware sends (accounting for any
10704 // calibration).
10705 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010706 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010707 processId(mapper, 1);
10708 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10709 processKey(mapper, BTN_TOUCH, 1);
10710 processSync(mapper);
10711
10712 // expect coord[0] to contain initial location of touch 0
10713 NotifyMotionArgs args;
10714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10715 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10716 ASSERT_EQ(1U, args.pointerCount);
10717 ASSERT_EQ(0, args.pointerProperties[0].id);
10718 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10719 ASSERT_NO_FATAL_FAILURE(
10720 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10721
10722 // FINGER 1 DOWN
10723 processSlot(mapper, 1);
10724 processId(mapper, 2);
10725 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10726 processSync(mapper);
10727
10728 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010730 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010731 ASSERT_EQ(2U, args.pointerCount);
10732 ASSERT_EQ(0, args.pointerProperties[0].id);
10733 ASSERT_EQ(1, args.pointerProperties[1].id);
10734 ASSERT_NO_FATAL_FAILURE(
10735 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10736 ASSERT_NO_FATAL_FAILURE(
10737 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10738
10739 // FINGER 1 MOVE
10740 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10741 processSync(mapper);
10742
10743 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10744 // from move
10745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10747 ASSERT_NO_FATAL_FAILURE(
10748 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10749 ASSERT_NO_FATAL_FAILURE(
10750 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10751
10752 // FINGER 0 MOVE
10753 processSlot(mapper, 0);
10754 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10755 processSync(mapper);
10756
10757 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10760 ASSERT_NO_FATAL_FAILURE(
10761 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10762 ASSERT_NO_FATAL_FAILURE(
10763 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10764
10765 // BUTTON DOWN
10766 processKey(mapper, BTN_LEFT, 1);
10767 processSync(mapper);
10768
10769 // touchinputmapper design sends a move before button press
10770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10771 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10773 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10774
10775 // BUTTON UP
10776 processKey(mapper, BTN_LEFT, 0);
10777 processSync(mapper);
10778
10779 // touchinputmapper design sends a move after button release
10780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10781 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10783 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10784
10785 // FINGER 0 UP
10786 processId(mapper, -1);
10787 processSync(mapper);
10788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10789 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10790
10791 // FINGER 1 MOVE
10792 processSlot(mapper, 1);
10793 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10794 processSync(mapper);
10795
10796 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10798 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10799 ASSERT_EQ(1U, args.pointerCount);
10800 ASSERT_EQ(1, args.pointerProperties[0].id);
10801 ASSERT_NO_FATAL_FAILURE(
10802 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10803
10804 // FINGER 1 UP
10805 processId(mapper, -1);
10806 processKey(mapper, BTN_TOUCH, 0);
10807 processSync(mapper);
10808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10809 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10810
Harry Cutts16a24cc2022-10-26 15:22:19 +000010811 // A non captured touchpad should have a mouse and touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010812 mFakePolicy->setPointerCapture(false);
10813 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Harry Cutts16a24cc2022-10-26 15:22:19 +000010815 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010816}
10817
10818TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10819 std::shared_ptr<FakePointerController> fakePointerController =
10820 std::make_shared<FakePointerController>();
10821 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10822 fakePointerController->setPosition(0, 0);
10823 fakePointerController->setButtonState(0);
10824
10825 // prepare device and capture
10826 prepareDisplay(DISPLAY_ORIENTATION_0);
10827 prepareAxes(POSITION | ID | SLOT);
10828 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10829 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010830 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010831 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10832 // run uncaptured pointer tests - pushes out generic events
10833 // FINGER 0 DOWN
10834 processId(mapper, 3);
10835 processPosition(mapper, 100, 100);
10836 processKey(mapper, BTN_TOUCH, 1);
10837 processSync(mapper);
10838
10839 // start at (100,100), cursor should be at (0,0) * scale
10840 NotifyMotionArgs args;
10841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10842 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10843 ASSERT_NO_FATAL_FAILURE(
10844 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10845
10846 // FINGER 0 MOVE
10847 processPosition(mapper, 200, 200);
10848 processSync(mapper);
10849
10850 // compute scaling to help with touch position checking
10851 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10852 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10853 float scale =
10854 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10855
10856 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10858 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10860 0, 0, 0, 0, 0, 0, 0));
10861}
10862
10863TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10864 std::shared_ptr<FakePointerController> fakePointerController =
10865 std::make_shared<FakePointerController>();
10866
10867 prepareDisplay(DISPLAY_ORIENTATION_0);
10868 prepareAxes(POSITION | ID | SLOT);
10869 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010870 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010871 mFakePolicy->setPointerCapture(false);
10872 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10873
Harry Cutts16a24cc2022-10-26 15:22:19 +000010874 // An uncaptured touchpad should be a pointer device, with additional touchpad source.
10875 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010876
Harry Cutts16a24cc2022-10-26 15:22:19 +000010877 // A captured touchpad should just have a touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010878 mFakePolicy->setPointerCapture(true);
10879 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10880 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10881}
10882
HQ Liue6983c72022-04-19 22:14:56 +000010883class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10884protected:
10885 float mPointerMovementScale;
10886 float mPointerXZoomScale;
10887 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10888 addConfigurationProperty("touch.deviceType", "pointer");
10889 std::shared_ptr<FakePointerController> fakePointerController =
10890 std::make_shared<FakePointerController>();
10891 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10892 fakePointerController->setPosition(0, 0);
10893 fakePointerController->setButtonState(0);
10894 prepareDisplay(DISPLAY_ORIENTATION_0);
10895
10896 prepareAxes(POSITION);
10897 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10898 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10899 // needs to be disabled, and the pointer gesture needs to be enabled.
10900 mFakePolicy->setPointerCapture(false);
10901 mFakePolicy->setPointerGestureEnabled(true);
10902 mFakePolicy->setPointerController(fakePointerController);
10903
10904 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10905 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10906 mPointerMovementScale =
10907 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10908 mPointerXZoomScale =
10909 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10910 }
10911
10912 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10913 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10914 /*flat*/ 0,
10915 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10916 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10917 /*flat*/ 0,
10918 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10919 }
10920};
10921
10922/**
10923 * Two fingers down on a pointer mode touch pad. The width
10924 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10925 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10926 * be greater than the both value to be freeform gesture, so that after two
10927 * fingers start to move downwards, the gesture should be swipe.
10928 */
10929TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10930 // The min freeform gesture width is 25units/mm x 30mm = 750
10931 // which is greater than fraction of the diagnal length of the touchpad (349).
10932 // Thus, MaxSwipWidth is 750.
10933 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10934 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10935 NotifyMotionArgs motionArgs;
10936
10937 // Two fingers down at once.
10938 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10939 // Pointer's initial position is used the [0,0] coordinate.
10940 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10941
10942 processId(mapper, FIRST_TRACKING_ID);
10943 processPosition(mapper, x1, y1);
10944 processMTSync(mapper);
10945 processId(mapper, SECOND_TRACKING_ID);
10946 processPosition(mapper, x2, y2);
10947 processMTSync(mapper);
10948 processSync(mapper);
10949
10950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10951 ASSERT_EQ(1U, motionArgs.pointerCount);
10952 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10953 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010954 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010955 ASSERT_NO_FATAL_FAILURE(
10956 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10957
10958 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10959 // that there should be 1 pointer.
10960 int32_t movingDistance = 200;
10961 y1 += movingDistance;
10962 y2 += movingDistance;
10963
10964 processId(mapper, FIRST_TRACKING_ID);
10965 processPosition(mapper, x1, y1);
10966 processMTSync(mapper);
10967 processId(mapper, SECOND_TRACKING_ID);
10968 processPosition(mapper, x2, y2);
10969 processMTSync(mapper);
10970 processSync(mapper);
10971
10972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10973 ASSERT_EQ(1U, motionArgs.pointerCount);
10974 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10975 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010976 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10978 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10979 0, 0, 0, 0));
10980}
10981
10982/**
10983 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10984 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10985 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10986 * value to be freeform gesture, so that after two fingers start to move downwards,
10987 * the gesture should be swipe.
10988 */
10989TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10990 // The min freeform gesture width is 5units/mm x 30mm = 150
10991 // which is greater than fraction of the diagnal length of the touchpad (349).
10992 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
10993 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
10994 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10995 NotifyMotionArgs motionArgs;
10996
10997 // Two fingers down at once.
10998 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10999 // Pointer's initial position is used the [0,0] coordinate.
11000 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
11001
11002 processId(mapper, FIRST_TRACKING_ID);
11003 processPosition(mapper, x1, y1);
11004 processMTSync(mapper);
11005 processId(mapper, SECOND_TRACKING_ID);
11006 processPosition(mapper, x2, y2);
11007 processMTSync(mapper);
11008 processSync(mapper);
11009
11010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11011 ASSERT_EQ(1U, motionArgs.pointerCount);
11012 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11013 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011014 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011015 ASSERT_NO_FATAL_FAILURE(
11016 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11017
11018 // It should be recognized as a SWIPE gesture when two fingers start to move down,
11019 // and there should be 1 pointer.
11020 int32_t movingDistance = 200;
11021 y1 += movingDistance;
11022 y2 += movingDistance;
11023
11024 processId(mapper, FIRST_TRACKING_ID);
11025 processPosition(mapper, x1, y1);
11026 processMTSync(mapper);
11027 processId(mapper, SECOND_TRACKING_ID);
11028 processPosition(mapper, x2, y2);
11029 processMTSync(mapper);
11030 processSync(mapper);
11031
11032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11033 ASSERT_EQ(1U, motionArgs.pointerCount);
11034 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11035 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011036 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011037 // New coordinate is the scaled relative coordinate from the initial coordinate.
11038 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
11039 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11040 0, 0, 0, 0));
11041}
11042
11043/**
11044 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
11045 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
11046 * freeform gestures after two fingers start to move downwards.
11047 */
11048TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
11049 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
11050 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11051
11052 NotifyMotionArgs motionArgs;
11053
11054 // Two fingers down at once. Wider than the max swipe width.
11055 // The gesture is expected to be PRESS, then transformed to FREEFORM
11056 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
11057
11058 processId(mapper, FIRST_TRACKING_ID);
11059 processPosition(mapper, x1, y1);
11060 processMTSync(mapper);
11061 processId(mapper, SECOND_TRACKING_ID);
11062 processPosition(mapper, x2, y2);
11063 processMTSync(mapper);
11064 processSync(mapper);
11065
11066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11067 ASSERT_EQ(1U, motionArgs.pointerCount);
11068 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11069 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011070 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011071 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
11072 ASSERT_NO_FATAL_FAILURE(
11073 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11074
11075 int32_t movingDistance = 200;
11076
11077 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
11078 // then two down events for two pointers.
11079 y1 += movingDistance;
11080 y2 += movingDistance;
11081
11082 processId(mapper, FIRST_TRACKING_ID);
11083 processPosition(mapper, x1, y1);
11084 processMTSync(mapper);
11085 processId(mapper, SECOND_TRACKING_ID);
11086 processPosition(mapper, x2, y2);
11087 processMTSync(mapper);
11088 processSync(mapper);
11089
11090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11091 // The previous PRESS gesture is cancelled, because it is transformed to freeform
11092 ASSERT_EQ(1U, motionArgs.pointerCount);
11093 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
11094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11095 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
11096 ASSERT_EQ(1U, motionArgs.pointerCount);
11097 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11099 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011100 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011101 ASSERT_EQ(2U, motionArgs.pointerCount);
11102 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
11103 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011104 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011105 // Two pointers' scaled relative coordinates from their initial centroid.
11106 // Initial y coordinates are 0 as y1 and y2 have the same value.
11107 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
11108 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
11109 // When pointers move, the new coordinates equal to the initial coordinates plus
11110 // scaled moving distance.
11111 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11112 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11113 0, 0, 0, 0));
11114 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11115 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11116 0, 0, 0, 0));
11117
11118 // Move two fingers down again, expect one MOVE motion event.
11119 y1 += movingDistance;
11120 y2 += movingDistance;
11121
11122 processId(mapper, FIRST_TRACKING_ID);
11123 processPosition(mapper, x1, y1);
11124 processMTSync(mapper);
11125 processId(mapper, SECOND_TRACKING_ID);
11126 processPosition(mapper, x2, y2);
11127 processMTSync(mapper);
11128 processSync(mapper);
11129
11130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11131 ASSERT_EQ(2U, motionArgs.pointerCount);
11132 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11133 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011134 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011135 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11136 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11137 0, 0, 0, 0, 0));
11138 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11139 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11140 0, 0, 0, 0, 0));
11141}
11142
Harry Cutts39b7ca22022-10-05 15:55:48 +000011143TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
11144 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
11145 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11146 NotifyMotionArgs motionArgs;
11147
11148 // Place two fingers down.
11149 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
11150
11151 processId(mapper, FIRST_TRACKING_ID);
11152 processPosition(mapper, x1, y1);
11153 processMTSync(mapper);
11154 processId(mapper, SECOND_TRACKING_ID);
11155 processPosition(mapper, x2, y2);
11156 processMTSync(mapper);
11157 processSync(mapper);
11158
11159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11160 ASSERT_EQ(1U, motionArgs.pointerCount);
11161 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11162 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
11163 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
11164 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
11165
11166 // Move the two fingers down and to the left.
11167 int32_t movingDistance = 200;
11168 x1 -= movingDistance;
11169 y1 += movingDistance;
11170 x2 -= movingDistance;
11171 y2 += movingDistance;
11172
11173 processId(mapper, FIRST_TRACKING_ID);
11174 processPosition(mapper, x1, y1);
11175 processMTSync(mapper);
11176 processId(mapper, SECOND_TRACKING_ID);
11177 processPosition(mapper, x2, y2);
11178 processMTSync(mapper);
11179 processSync(mapper);
11180
11181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11182 ASSERT_EQ(1U, motionArgs.pointerCount);
11183 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11184 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
11185 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
11186 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
11187}
11188
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011189TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
11190 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
11191 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
11192 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
11194
11195 // Start a stylus gesture.
11196 processKey(mapper, BTN_TOOL_PEN, 1);
11197 processId(mapper, FIRST_TRACKING_ID);
11198 processPosition(mapper, 100, 200);
11199 processSync(mapper);
11200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11201 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
11202 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11203 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11204 // TODO(b/257078296): Pointer mode generates extra event.
11205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11206 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11207 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11208 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11210
11211 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
11212 // gesture should be disabled.
11213 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
11214 viewport->isActive = false;
11215 mFakePolicy->updateViewport(*viewport);
11216 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
11217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11218 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11219 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11220 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11221 // TODO(b/257078296): Pointer mode generates extra event.
11222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11223 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11224 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11225 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11227}
11228
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011229// --- JoystickInputMapperTest ---
11230
11231class JoystickInputMapperTest : public InputMapperTest {
11232protected:
11233 static const int32_t RAW_X_MIN;
11234 static const int32_t RAW_X_MAX;
11235 static const int32_t RAW_Y_MIN;
11236 static const int32_t RAW_Y_MAX;
11237
11238 void SetUp() override {
11239 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
11240 }
11241 void prepareAxes() {
11242 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
11243 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
11244 }
11245
11246 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
11247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
11248 }
11249
11250 void processSync(JoystickInputMapper& mapper) {
11251 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
11252 }
11253
11254 void prepareVirtualDisplay(int32_t orientation) {
11255 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
11256 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
11257 NO_PORT, ViewportType::VIRTUAL);
11258 }
11259};
11260
11261const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
11262const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
11263const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
11264const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
11265
11266TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
11267 prepareAxes();
11268 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
11269
11270 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
11271
11272 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
11273
11274 // Send an axis event
11275 processAxis(mapper, ABS_X, 100);
11276 processSync(mapper);
11277
11278 NotifyMotionArgs args;
11279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11280 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11281
11282 // Send another axis event
11283 processAxis(mapper, ABS_Y, 100);
11284 processSync(mapper);
11285
11286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11287 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11288}
11289
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011290// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011291
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011292class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011293protected:
11294 static const char* DEVICE_NAME;
11295 static const char* DEVICE_LOCATION;
11296 static const int32_t DEVICE_ID;
11297 static const int32_t DEVICE_GENERATION;
11298 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011299 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011300 static const int32_t EVENTHUB_ID;
11301
11302 std::shared_ptr<FakeEventHub> mFakeEventHub;
11303 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011304 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011305 std::unique_ptr<InstrumentedInputReader> mReader;
11306 std::shared_ptr<InputDevice> mDevice;
11307
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011308 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011309 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011310 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011311 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011312 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011313 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011314 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11315 }
11316
11317 void SetUp() override { SetUp(DEVICE_CLASSES); }
11318
11319 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011320 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011321 mFakePolicy.clear();
11322 }
11323
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070011324 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011325 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
11326 mReader->requestRefreshConfiguration(changes);
11327 mReader->loopOnce();
11328 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070011329 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011330 }
11331
11332 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11333 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011334 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011335 InputDeviceIdentifier identifier;
11336 identifier.name = name;
11337 identifier.location = location;
11338 std::shared_ptr<InputDevice> device =
11339 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11340 identifier);
11341 mReader->pushNextDevice(device);
11342 mFakeEventHub->addDevice(eventHubId, name, classes);
11343 mReader->loopOnce();
11344 return device;
11345 }
11346
11347 template <class T, typename... Args>
11348 T& addControllerAndConfigure(Args... args) {
11349 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11350
11351 return controller;
11352 }
11353};
11354
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011355const char* PeripheralControllerTest::DEVICE_NAME = "device";
11356const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11357const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11358const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11359const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011360const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11361 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011362const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011363
11364// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011365class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011366protected:
11367 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011368 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011369 }
11370};
11371
11372TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011373 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011374
11375 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
11376 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
11377}
11378
11379TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011380 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011381
11382 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
11383 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
11384}
11385
11386// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011387class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011388protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011389 void SetUp() override {
11390 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11391 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011392};
11393
Chris Ye85758332021-05-16 23:05:17 -070011394TEST_F(LightControllerTest, MonoLight) {
11395 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011396 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011397 .maxBrightness = 255,
11398 .flags = InputLightClass::BRIGHTNESS,
11399 .path = ""};
11400 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011401
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011402 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011403 InputDeviceInfo info;
11404 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011405 std::vector<InputDeviceLightInfo> lights = info.getLights();
11406 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011407 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11408 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11409
11410 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11411 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11412}
11413
11414TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11415 RawLightInfo infoMono = {.id = 1,
11416 .name = "mono_keyboard_backlight",
11417 .maxBrightness = 255,
11418 .flags = InputLightClass::BRIGHTNESS |
11419 InputLightClass::KEYBOARD_BACKLIGHT,
11420 .path = ""};
11421 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11422
11423 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11424 InputDeviceInfo info;
11425 controller.populateDeviceInfo(&info);
11426 std::vector<InputDeviceLightInfo> lights = info.getLights();
11427 ASSERT_EQ(1U, lights.size());
11428 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11429 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011430
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011431 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11432 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011433}
11434
11435TEST_F(LightControllerTest, RGBLight) {
11436 RawLightInfo infoRed = {.id = 1,
11437 .name = "red",
11438 .maxBrightness = 255,
11439 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11440 .path = ""};
11441 RawLightInfo infoGreen = {.id = 2,
11442 .name = "green",
11443 .maxBrightness = 255,
11444 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11445 .path = ""};
11446 RawLightInfo infoBlue = {.id = 3,
11447 .name = "blue",
11448 .maxBrightness = 255,
11449 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11450 .path = ""};
11451 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11452 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11453 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11454
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011455 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011456 InputDeviceInfo info;
11457 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011458 std::vector<InputDeviceLightInfo> lights = info.getLights();
11459 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011460 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11461 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11462 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11463
11464 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11465 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11466}
11467
11468TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11469 RawLightInfo infoRed = {.id = 1,
11470 .name = "red_keyboard_backlight",
11471 .maxBrightness = 255,
11472 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11473 InputLightClass::KEYBOARD_BACKLIGHT,
11474 .path = ""};
11475 RawLightInfo infoGreen = {.id = 2,
11476 .name = "green_keyboard_backlight",
11477 .maxBrightness = 255,
11478 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11479 InputLightClass::KEYBOARD_BACKLIGHT,
11480 .path = ""};
11481 RawLightInfo infoBlue = {.id = 3,
11482 .name = "blue_keyboard_backlight",
11483 .maxBrightness = 255,
11484 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11485 InputLightClass::KEYBOARD_BACKLIGHT,
11486 .path = ""};
11487 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11488 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11489 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11490
11491 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11492 InputDeviceInfo info;
11493 controller.populateDeviceInfo(&info);
11494 std::vector<InputDeviceLightInfo> lights = info.getLights();
11495 ASSERT_EQ(1U, lights.size());
11496 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11497 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11498 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11499
11500 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11501 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11502}
11503
11504TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11505 RawLightInfo infoRed = {.id = 1,
11506 .name = "red",
11507 .maxBrightness = 255,
11508 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11509 .path = ""};
11510 RawLightInfo infoGreen = {.id = 2,
11511 .name = "green",
11512 .maxBrightness = 255,
11513 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11514 .path = ""};
11515 RawLightInfo infoBlue = {.id = 3,
11516 .name = "blue",
11517 .maxBrightness = 255,
11518 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11519 .path = ""};
11520 RawLightInfo infoGlobal = {.id = 3,
11521 .name = "global_keyboard_backlight",
11522 .maxBrightness = 255,
11523 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11524 InputLightClass::KEYBOARD_BACKLIGHT,
11525 .path = ""};
11526 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11527 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11528 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11529 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11530
11531 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11532 InputDeviceInfo info;
11533 controller.populateDeviceInfo(&info);
11534 std::vector<InputDeviceLightInfo> lights = info.getLights();
11535 ASSERT_EQ(1U, lights.size());
11536 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11537 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11538 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011539
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011540 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11541 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011542}
11543
11544TEST_F(LightControllerTest, MultiColorRGBLight) {
11545 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011546 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011547 .maxBrightness = 255,
11548 .flags = InputLightClass::BRIGHTNESS |
11549 InputLightClass::MULTI_INTENSITY |
11550 InputLightClass::MULTI_INDEX,
11551 .path = ""};
11552
11553 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11554
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011555 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011556 InputDeviceInfo info;
11557 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011558 std::vector<InputDeviceLightInfo> lights = info.getLights();
11559 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011560 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11561 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11562 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11563
11564 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11565 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11566}
11567
11568TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11569 RawLightInfo infoColor = {.id = 1,
11570 .name = "multi_color_keyboard_backlight",
11571 .maxBrightness = 255,
11572 .flags = InputLightClass::BRIGHTNESS |
11573 InputLightClass::MULTI_INTENSITY |
11574 InputLightClass::MULTI_INDEX |
11575 InputLightClass::KEYBOARD_BACKLIGHT,
11576 .path = ""};
11577
11578 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11579
11580 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11581 InputDeviceInfo info;
11582 controller.populateDeviceInfo(&info);
11583 std::vector<InputDeviceLightInfo> lights = info.getLights();
11584 ASSERT_EQ(1U, lights.size());
11585 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11586 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11587 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011588
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011589 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11590 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011591}
11592
11593TEST_F(LightControllerTest, PlayerIdLight) {
11594 RawLightInfo info1 = {.id = 1,
11595 .name = "player1",
11596 .maxBrightness = 255,
11597 .flags = InputLightClass::BRIGHTNESS,
11598 .path = ""};
11599 RawLightInfo info2 = {.id = 2,
11600 .name = "player2",
11601 .maxBrightness = 255,
11602 .flags = InputLightClass::BRIGHTNESS,
11603 .path = ""};
11604 RawLightInfo info3 = {.id = 3,
11605 .name = "player3",
11606 .maxBrightness = 255,
11607 .flags = InputLightClass::BRIGHTNESS,
11608 .path = ""};
11609 RawLightInfo info4 = {.id = 4,
11610 .name = "player4",
11611 .maxBrightness = 255,
11612 .flags = InputLightClass::BRIGHTNESS,
11613 .path = ""};
11614 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11615 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11616 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11617 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11618
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011619 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011620 InputDeviceInfo info;
11621 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011622 std::vector<InputDeviceLightInfo> lights = info.getLights();
11623 ASSERT_EQ(1U, lights.size());
11624 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011625 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11626 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011627
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011628 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11629 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11630 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011631}
11632
Michael Wrightd02c5b62014-02-10 15:10:22 -080011633} // namespace android