blob: 63bf633ff8dc4850be1af85ca508e6dea00b963d [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
Arthur Hungaab25622020-01-16 11:22:11 +08002526// --- TouchProcessTest ---
2527class TouchIntegrationTest : public InputReaderIntegrationTest {
2528protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002529 const std::string UNIQUE_ID = "local:0";
2530
Chris Yea52ade12020-08-27 16:49:20 -07002531 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002532#if !defined(__ANDROID__)
2533 GTEST_SKIP();
2534#endif
Arthur Hungaab25622020-01-16 11:22:11 +08002535 InputReaderIntegrationTest::SetUp();
2536 // At least add an internal display.
2537 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2538 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002539 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002540
2541 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2542 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2543 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00002544 const auto info = findDeviceByName(mDevice->getName());
2545 ASSERT_TRUE(info);
2546 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08002547 }
2548
2549 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2550 int32_t orientation, const std::string& uniqueId,
2551 std::optional<uint8_t> physicalPort,
2552 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002553 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2554 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002555 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2556 }
2557
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002558 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2559 NotifyMotionArgs args;
2560 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2561 EXPECT_EQ(action, args.action);
2562 ASSERT_EQ(points.size(), args.pointerCount);
2563 for (size_t i = 0; i < args.pointerCount; i++) {
2564 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2565 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2566 }
2567 }
2568
Arthur Hungaab25622020-01-16 11:22:11 +08002569 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00002570 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08002571};
2572
2573TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2574 NotifyMotionArgs args;
2575 const Point centerPoint = mDevice->getCenterPoint();
2576
2577 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002578 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002579 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002580 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002581 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2582 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2583
2584 // ACTION_MOVE
2585 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002586 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002587 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2588 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2589
2590 // ACTION_UP
2591 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002592 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002593 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2594 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2595}
2596
2597TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2598 NotifyMotionArgs args;
2599 const Point centerPoint = mDevice->getCenterPoint();
2600
2601 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002602 mDevice->sendSlot(FIRST_SLOT);
2603 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002604 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002605 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002606 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2607 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2608
2609 // ACTION_POINTER_DOWN (Second slot)
2610 const Point secondPoint = centerPoint + Point(100, 100);
2611 mDevice->sendSlot(SECOND_SLOT);
2612 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002613 mDevice->sendDown(secondPoint);
2614 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002615 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002616 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002617
2618 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002619 mDevice->sendMove(secondPoint + Point(1, 1));
2620 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002621 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2622 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2623
2624 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002625 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002626 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002627 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002628 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002629
2630 // ACTION_UP
2631 mDevice->sendSlot(FIRST_SLOT);
2632 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002633 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002634 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2635 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2636}
2637
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002638/**
2639 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2640 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2641 * data?
2642 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2643 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2644 * for Pointer 0 only is generated after.
2645 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2646 * events, we will not miss any information.
2647 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2648 * event generated afterwards that contains the newest movement of pointer 0.
2649 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2650 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2651 * losing information about non-palm pointers.
2652 */
2653TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2654 NotifyMotionArgs args;
2655 const Point centerPoint = mDevice->getCenterPoint();
2656
2657 // ACTION_DOWN
2658 mDevice->sendSlot(FIRST_SLOT);
2659 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2660 mDevice->sendDown(centerPoint);
2661 mDevice->sendSync();
2662 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2663
2664 // ACTION_POINTER_DOWN (Second slot)
2665 const Point secondPoint = centerPoint + Point(100, 100);
2666 mDevice->sendSlot(SECOND_SLOT);
2667 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2668 mDevice->sendDown(secondPoint);
2669 mDevice->sendSync();
2670 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2671
2672 // ACTION_MOVE (First slot)
2673 mDevice->sendSlot(FIRST_SLOT);
2674 mDevice->sendMove(centerPoint + Point(5, 5));
2675 // ACTION_POINTER_UP (Second slot)
2676 mDevice->sendSlot(SECOND_SLOT);
2677 mDevice->sendPointerUp();
2678 // Send a single sync for the above 2 pointer updates
2679 mDevice->sendSync();
2680
2681 // First, we should get POINTER_UP for the second pointer
2682 assertReceivedMotion(ACTION_POINTER_1_UP,
2683 {/*first pointer */ centerPoint + Point(5, 5),
2684 /*second pointer*/ secondPoint});
2685
2686 // Next, the MOVE event for the first pointer
2687 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2688}
2689
2690/**
2691 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2692 * move, and then it will go up, all in the same frame.
2693 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2694 * gets sent to the listener.
2695 */
2696TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2697 NotifyMotionArgs args;
2698 const Point centerPoint = mDevice->getCenterPoint();
2699
2700 // ACTION_DOWN
2701 mDevice->sendSlot(FIRST_SLOT);
2702 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2703 mDevice->sendDown(centerPoint);
2704 mDevice->sendSync();
2705 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2706
2707 // ACTION_POINTER_DOWN (Second slot)
2708 const Point secondPoint = centerPoint + Point(100, 100);
2709 mDevice->sendSlot(SECOND_SLOT);
2710 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2711 mDevice->sendDown(secondPoint);
2712 mDevice->sendSync();
2713 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2714
2715 // ACTION_MOVE (First slot)
2716 mDevice->sendSlot(FIRST_SLOT);
2717 mDevice->sendMove(centerPoint + Point(5, 5));
2718 // ACTION_POINTER_UP (Second slot)
2719 mDevice->sendSlot(SECOND_SLOT);
2720 mDevice->sendMove(secondPoint + Point(6, 6));
2721 mDevice->sendPointerUp();
2722 // Send a single sync for the above 2 pointer updates
2723 mDevice->sendSync();
2724
2725 // First, we should get POINTER_UP for the second pointer
2726 // The movement of the second pointer during the liftoff frame is ignored.
2727 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2728 assertReceivedMotion(ACTION_POINTER_1_UP,
2729 {/*first pointer */ centerPoint + Point(5, 5),
2730 /*second pointer*/ secondPoint});
2731
2732 // Next, the MOVE event for the first pointer
2733 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2734}
2735
Arthur Hungaab25622020-01-16 11:22:11 +08002736TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2737 NotifyMotionArgs args;
2738 const Point centerPoint = mDevice->getCenterPoint();
2739
2740 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002741 mDevice->sendSlot(FIRST_SLOT);
2742 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002743 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002744 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002745 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2746 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2747
arthurhungcc7f9802020-04-30 17:55:40 +08002748 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002749 const Point secondPoint = centerPoint + Point(100, 100);
2750 mDevice->sendSlot(SECOND_SLOT);
2751 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2752 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002753 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002754 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002755 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002756
arthurhungcc7f9802020-04-30 17:55:40 +08002757 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002758 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002759 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002760 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2761 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2762
arthurhungcc7f9802020-04-30 17:55:40 +08002763 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2764 // a palm event.
2765 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002766 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002767 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002768 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002769 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002770 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002771
arthurhungcc7f9802020-04-30 17:55:40 +08002772 // Send up to second slot, expect first slot send moving.
2773 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002774 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002775 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2776 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002777
arthurhungcc7f9802020-04-30 17:55:40 +08002778 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002779 mDevice->sendSlot(FIRST_SLOT);
2780 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002781 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002782
arthurhungcc7f9802020-04-30 17:55:40 +08002783 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2784 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002785}
2786
Prabir Pradhanda20b172022-09-26 17:01:18 +00002787TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
2788 const Point centerPoint = mDevice->getCenterPoint();
2789
2790 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
2791 mDevice->sendSlot(FIRST_SLOT);
2792 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2793 mDevice->sendToolType(MT_TOOL_PEN);
2794 mDevice->sendDown(centerPoint);
2795 mDevice->sendSync();
2796 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2797 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2798 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2799
2800 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2801
2802 // Release the stylus touch.
2803 mDevice->sendUp();
2804 mDevice->sendSync();
2805 ASSERT_NO_FATAL_FAILURE(
2806 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2807
2808 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2809
2810 // Touch down with the finger, without the pen tool selected. The policy is not notified.
2811 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2812 mDevice->sendToolType(MT_TOOL_FINGER);
2813 mDevice->sendDown(centerPoint);
2814 mDevice->sendSync();
2815 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2816 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2817 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2818
2819 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2820
2821 mDevice->sendUp();
2822 mDevice->sendSync();
2823 ASSERT_NO_FATAL_FAILURE(
2824 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2825
2826 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
2827 // The policy should be notified of the stylus presence.
2828 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2829 mDevice->sendToolType(MT_TOOL_PEN);
2830 mDevice->sendMove(centerPoint);
2831 mDevice->sendSync();
2832 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2833 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2834 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2835
2836 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2837}
2838
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002839TEST_F(TouchIntegrationTest, StylusButtonsGenerateKeyEvents) {
2840 mDevice->sendKey(BTN_STYLUS, 1);
2841 mDevice->sendSync();
2842 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2843 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2844 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2845
2846 mDevice->sendKey(BTN_STYLUS, 0);
2847 mDevice->sendSync();
2848 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2849 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2850 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2851}
2852
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002853TEST_F(TouchIntegrationTest, StylusButtonsSurroundingTouchGesture) {
2854 const Point centerPoint = mDevice->getCenterPoint();
2855
2856 // Press the stylus button.
2857 mDevice->sendKey(BTN_STYLUS, 1);
2858 mDevice->sendSync();
2859 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2860 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2861 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2862
2863 // Start and finish a stylus gesture.
2864 mDevice->sendSlot(FIRST_SLOT);
2865 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2866 mDevice->sendToolType(MT_TOOL_PEN);
2867 mDevice->sendDown(centerPoint);
2868 mDevice->sendSync();
2869 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2870 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2871 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
2872 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2873 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2874 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2875 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
2876 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2877
2878 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2879 mDevice->sendSync();
2880 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2881 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2882 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2883 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2884 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2885 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2886
2887 // Release the stylus button.
2888 mDevice->sendKey(BTN_STYLUS, 0);
2889 mDevice->sendSync();
2890 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2891 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2892 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2893}
2894
2895TEST_F(TouchIntegrationTest, StylusButtonsWithinTouchGesture) {
2896 const Point centerPoint = mDevice->getCenterPoint();
2897
2898 // Start a stylus gesture.
2899 mDevice->sendSlot(FIRST_SLOT);
2900 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2901 mDevice->sendToolType(MT_TOOL_PEN);
2902 mDevice->sendDown(centerPoint);
2903 mDevice->sendSync();
2904 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2905 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2906 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2907
2908 // Press and release a stylus button. Each change in button state also generates a MOVE event.
2909 mDevice->sendKey(BTN_STYLUS, 1);
2910 mDevice->sendSync();
2911 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2912 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2913 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2914 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2915 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2916 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
2917 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2918 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2919 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2920 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
2921 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2922
2923 mDevice->sendKey(BTN_STYLUS, 0);
2924 mDevice->sendSync();
2925 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2926 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2927 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2928 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2929 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2930 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2931 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2932 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2933 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2934
2935 // Finish the stylus gesture.
2936 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2937 mDevice->sendSync();
2938 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2939 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2940 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2941}
2942
Michael Wrightd02c5b62014-02-10 15:10:22 -08002943// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002944class InputDeviceTest : public testing::Test {
2945protected:
2946 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002947 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002948 static const int32_t DEVICE_ID;
2949 static const int32_t DEVICE_GENERATION;
2950 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002951 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002952 static const int32_t EVENTHUB_ID;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002953 static const std::string DEVICE_BLUETOOTH_ADDRESS;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002954
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002955 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002956 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002957 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002958 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002959 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002960
Chris Yea52ade12020-08-27 16:49:20 -07002961 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002962 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002963 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002964 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002965 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002966 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002967 InputDeviceIdentifier identifier;
2968 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002969 identifier.location = DEVICE_LOCATION;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002970 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
arthurhungdcef2dc2020-08-11 14:47:50 +08002971 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002972 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002973 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002974 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002975 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002976 }
2977
Chris Yea52ade12020-08-27 16:49:20 -07002978 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002979 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002980 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002981 }
2982};
2983
2984const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002985const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002986const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002987const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2988const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002989const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002990 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002991const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002992const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002993
2994TEST_F(InputDeviceTest, ImmutableProperties) {
2995 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002996 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002997 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002998}
2999
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00003000TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
3001 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
3002
3003 // Configuration
3004 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
3005 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003006 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00003007
3008 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
3009}
3010
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003011TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
3012 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07003013}
3014
Michael Wrightd02c5b62014-02-10 15:10:22 -08003015TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
3016 // Configuration.
3017 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003018 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003019
3020 // Reset.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003021 unused += mDevice->reset(ARBITRARY_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003022
3023 NotifyDeviceResetArgs resetArgs;
3024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3025 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3026 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3027
3028 // Metadata.
3029 ASSERT_TRUE(mDevice->isIgnored());
3030 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
3031
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00003032 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003033 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003034 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003035 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
3036 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
3037
3038 // State queries.
3039 ASSERT_EQ(0, mDevice->getMetaState());
3040
3041 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
3042 << "Ignored device should return unknown key code state.";
3043 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
3044 << "Ignored device should return unknown scan code state.";
3045 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
3046 << "Ignored device should return unknown switch state.";
3047
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003048 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003050 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003051 << "Ignored device should never mark any key codes.";
3052 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
3053 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
3054}
3055
3056TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
3057 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003058 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08003059
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003060 FakeInputMapper& mapper1 =
3061 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003062 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3063 mapper1.setMetaState(AMETA_ALT_ON);
3064 mapper1.addSupportedKeyCode(AKEYCODE_A);
3065 mapper1.addSupportedKeyCode(AKEYCODE_B);
3066 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
3067 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
3068 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
3069 mapper1.setScanCodeState(3, AKEY_STATE_UP);
3070 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003071
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003072 FakeInputMapper& mapper2 =
3073 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003074 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003075
3076 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003077 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003078
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003079 std::string propertyValue;
3080 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003082 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003083
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003084 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
3085 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086
3087 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003088 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003089 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
3090 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003091
3092 NotifyDeviceResetArgs resetArgs;
3093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3094 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3095 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3096
3097 // Metadata.
3098 ASSERT_FALSE(mDevice->isIgnored());
3099 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
3100
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00003101 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003102 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003103 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003104 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
3105 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
3106
3107 // State queries.
3108 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
3109 << "Should query mappers and combine meta states.";
3110
3111 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3112 << "Should return unknown key code state when source not supported.";
3113 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3114 << "Should return unknown scan code state when source not supported.";
3115 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3116 << "Should return unknown switch state when source not supported.";
3117
3118 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
3119 << "Should query mapper when source is supported.";
3120 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
3121 << "Should query mapper when source is supported.";
3122 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
3123 << "Should query mapper when source is supported.";
3124
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003125 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08003126 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003127 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128 << "Should do nothing when source is unsupported.";
3129 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
3130 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
3131 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
3132 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
3133
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003134 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003135 << "Should query mapper when source is supported.";
3136 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
3137 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
3138 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
3139 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
3140
3141 // Event handling.
3142 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003143 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003144 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003145
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003146 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
3147 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003148}
3149
Arthur Hung2c9a3342019-07-23 14:18:59 +08003150// A single input device is associated with a specific display. Check that:
3151// 1. Device is disabled if the viewport corresponding to the associated display is not found
3152// 2. Device is disabled when setEnabled API is called
3153TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003154 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003155
3156 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003157 std::list<NotifyArgs> unused =
3158 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003159
3160 // Device should be enabled by default.
3161 ASSERT_TRUE(mDevice->isEnabled());
3162
3163 // Prepare associated info.
3164 constexpr uint8_t hdmi = 1;
3165 const std::string UNIQUE_ID = "local:1";
3166
3167 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003168 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3169 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003170 // Device should be disabled because it is associated with a specific display via
3171 // input port <-> display port association, but the corresponding display is not found
3172 ASSERT_FALSE(mDevice->isEnabled());
3173
3174 // Prepare displays.
3175 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003176 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
3177 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003178 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3179 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003180 ASSERT_TRUE(mDevice->isEnabled());
3181
3182 // Device should be disabled after set disable.
3183 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003184 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3185 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003186 ASSERT_FALSE(mDevice->isEnabled());
3187
3188 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003189 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3190 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003191 ASSERT_FALSE(mDevice->isEnabled());
3192}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003193
Christine Franks1ba71cc2021-04-07 14:37:42 -07003194TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
3195 // Device should be enabled by default.
3196 mFakePolicy->clearViewports();
3197 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003198 std::list<NotifyArgs> unused =
3199 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003200 ASSERT_TRUE(mDevice->isEnabled());
3201
3202 // Device should be disabled because it is associated with a specific display, but the
3203 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08003204 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003205 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3206 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003207 ASSERT_FALSE(mDevice->isEnabled());
3208
3209 // Device should be enabled when a display is found.
3210 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3211 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3212 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003213 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3214 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003215 ASSERT_TRUE(mDevice->isEnabled());
3216
3217 // Device should be disabled after set disable.
3218 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003219 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3220 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003221 ASSERT_FALSE(mDevice->isEnabled());
3222
3223 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003224 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3225 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003226 ASSERT_FALSE(mDevice->isEnabled());
3227}
3228
Christine Franks2a2293c2022-01-18 11:51:16 -08003229TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
3230 mFakePolicy->clearViewports();
3231 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003232 std::list<NotifyArgs> unused =
3233 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08003234
Christine Franks2a2293c2022-01-18 11:51:16 -08003235 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
3236 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3237 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3238 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003239 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3240 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08003241 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
3242}
3243
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07003244/**
3245 * This test reproduces a crash caused by a dangling reference that remains after device is added
3246 * and removed. The reference is accessed in InputDevice::dump(..);
3247 */
3248TEST_F(InputDeviceTest, DumpDoesNotCrash) {
3249 constexpr int32_t TEST_EVENTHUB_ID = 10;
3250 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
3251
3252 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
3253 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
3254 device.removeEventHubDevice(TEST_EVENTHUB_ID);
3255 std::string dumpStr, eventHubDevStr;
3256 device.dump(dumpStr, eventHubDevStr);
3257}
3258
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003259TEST_F(InputDeviceTest, GetBluetoothAddress) {
3260 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
3261 ASSERT_TRUE(address);
3262 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
3263}
3264
Michael Wrightd02c5b62014-02-10 15:10:22 -08003265// --- InputMapperTest ---
3266
3267class InputMapperTest : public testing::Test {
3268protected:
3269 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003270 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271 static const int32_t DEVICE_ID;
3272 static const int32_t DEVICE_GENERATION;
3273 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003274 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003275 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003277 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003279 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08003280 std::unique_ptr<InstrumentedInputReader> mReader;
3281 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003283 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003284 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003285 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003286 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08003287 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003288 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08003289 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003290 // Consume the device reset notification generated when adding a new device.
3291 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003292 }
3293
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003294 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003295 SetUp(DEVICE_CLASSES);
3296 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003297
Chris Yea52ade12020-08-27 16:49:20 -07003298 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003299 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003301 }
3302
3303 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003304 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003305 }
3306
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003307 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003308 if (!changes ||
3309 (changes &
3310 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3311 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003312 mReader->requestRefreshConfiguration(changes);
3313 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003314 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003315 std::list<NotifyArgs> out =
3316 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003317 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003318 for (const NotifyArgs& args : out) {
3319 mFakeListener->notify(args);
3320 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003321 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003322 return out;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003323 }
3324
arthurhungdcef2dc2020-08-11 14:47:50 +08003325 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3326 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003327 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003328 InputDeviceIdentifier identifier;
3329 identifier.name = name;
3330 identifier.location = location;
3331 std::shared_ptr<InputDevice> device =
3332 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3333 identifier);
3334 mReader->pushNextDevice(device);
3335 mFakeEventHub->addDevice(eventHubId, name, classes);
3336 mReader->loopOnce();
3337 return device;
3338 }
3339
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003340 template <class T, typename... Args>
3341 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003342 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003343 configureDevice(0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003344 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
3345 resetArgList += mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003346 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003347 for (const NotifyArgs& loopArgs : resetArgList) {
3348 mFakeListener->notify(loopArgs);
3349 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003350 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003351 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352 }
3353
3354 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003355 int32_t orientation, const std::string& uniqueId,
3356 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003357 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3358 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003359 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3360 }
3361
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003362 void clearViewports() {
3363 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364 }
3365
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003366 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
3367 int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003368 RawEvent event;
3369 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003370 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003371 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003372 event.type = type;
3373 event.code = code;
3374 event.value = value;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003375 std::list<NotifyArgs> processArgList = mapper.process(&event);
3376 for (const NotifyArgs& args : processArgList) {
3377 mFakeListener->notify(args);
3378 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003379 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003380 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003381 return processArgList;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003382 }
3383
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00003384 void resetMapper(InputMapper& mapper, nsecs_t when) {
3385 const auto resetArgs = mapper.reset(when);
3386 for (const auto args : resetArgs) {
3387 mFakeListener->notify(args);
3388 }
3389 // Loop the reader to flush the input listener queue.
3390 mReader->loopOnce();
3391 }
3392
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00003393 std::list<NotifyArgs> handleTimeout(InputMapper& mapper, nsecs_t when) {
3394 std::list<NotifyArgs> generatedArgs = mapper.timeoutExpired(when);
3395 for (const NotifyArgs& args : generatedArgs) {
3396 mFakeListener->notify(args);
3397 }
3398 // Loop the reader to flush the input listener queue.
3399 mReader->loopOnce();
3400 return generatedArgs;
3401 }
3402
Michael Wrightd02c5b62014-02-10 15:10:22 -08003403 static void assertMotionRange(const InputDeviceInfo& info,
3404 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3405 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003406 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003407 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3408 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3409 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3410 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3411 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3412 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3413 }
3414
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003415 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3416 float size, float touchMajor, float touchMinor, float toolMajor,
3417 float toolMinor, float orientation, float distance,
3418 float scaledAxisEpsilon = 1.f) {
3419 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3420 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003421 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3422 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003423 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3424 scaledAxisEpsilon);
3425 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3426 scaledAxisEpsilon);
3427 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3428 scaledAxisEpsilon);
3429 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3430 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003431 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3432 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3433 }
3434
Michael Wright17db18e2020-06-26 20:51:44 +01003435 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003436 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003437 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003438 ASSERT_NEAR(x, actualX, 1);
3439 ASSERT_NEAR(y, actualY, 1);
3440 }
3441};
3442
3443const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003444const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003445const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003446const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3447const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003448const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3449 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003450const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451
3452// --- SwitchInputMapperTest ---
3453
3454class SwitchInputMapperTest : public InputMapperTest {
3455protected:
3456};
3457
3458TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003459 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003460
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003461 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462}
3463
3464TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003465 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003466
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003467 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003468 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003469
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003470 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003471 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003472}
3473
3474TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003475 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003476 std::list<NotifyArgs> out;
3477 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3478 ASSERT_TRUE(out.empty());
3479 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3480 ASSERT_TRUE(out.empty());
3481 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3482 ASSERT_TRUE(out.empty());
3483 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003484
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003485 ASSERT_EQ(1u, out.size());
3486 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003487 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003488 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3489 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003490 args.switchMask);
3491 ASSERT_EQ(uint32_t(0), args.policyFlags);
3492}
3493
Chris Ye87143712020-11-10 05:05:58 +00003494// --- VibratorInputMapperTest ---
3495class VibratorInputMapperTest : public InputMapperTest {
3496protected:
3497 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3498};
3499
3500TEST_F(VibratorInputMapperTest, GetSources) {
3501 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3502
3503 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3504}
3505
3506TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3507 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3508
3509 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3510}
3511
3512TEST_F(VibratorInputMapperTest, Vibrate) {
3513 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003514 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003515 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3516
3517 VibrationElement pattern(2);
3518 VibrationSequence sequence(2);
3519 pattern.duration = std::chrono::milliseconds(200);
3520 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3521 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3522 sequence.addElement(pattern);
3523 pattern.duration = std::chrono::milliseconds(500);
3524 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3525 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3526 sequence.addElement(pattern);
3527
3528 std::vector<int64_t> timings = {0, 1};
3529 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3530
3531 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003532 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003533 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003534 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003535 // Verify vibrator state listener was notified.
3536 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003537 ASSERT_EQ(1u, out.size());
3538 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3539 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3540 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003541 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003542 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003543 ASSERT_FALSE(mapper.isVibrating());
3544 // Verify vibrator state listener was notified.
3545 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003546 ASSERT_EQ(1u, out.size());
3547 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3548 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3549 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003550}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551
Chris Yef59a2f42020-10-16 12:55:26 -07003552// --- SensorInputMapperTest ---
3553
3554class SensorInputMapperTest : public InputMapperTest {
3555protected:
3556 static const int32_t ACCEL_RAW_MIN;
3557 static const int32_t ACCEL_RAW_MAX;
3558 static const int32_t ACCEL_RAW_FUZZ;
3559 static const int32_t ACCEL_RAW_FLAT;
3560 static const int32_t ACCEL_RAW_RESOLUTION;
3561
3562 static const int32_t GYRO_RAW_MIN;
3563 static const int32_t GYRO_RAW_MAX;
3564 static const int32_t GYRO_RAW_FUZZ;
3565 static const int32_t GYRO_RAW_FLAT;
3566 static const int32_t GYRO_RAW_RESOLUTION;
3567
3568 static const float GRAVITY_MS2_UNIT;
3569 static const float DEGREE_RADIAN_UNIT;
3570
3571 void prepareAccelAxes();
3572 void prepareGyroAxes();
3573 void setAccelProperties();
3574 void setGyroProperties();
3575 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3576};
3577
3578const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3579const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3580const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3581const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3582const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3583
3584const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3585const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3586const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3587const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3588const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3589
3590const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3591const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3592
3593void SensorInputMapperTest::prepareAccelAxes() {
3594 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3595 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3596 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3597 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3598 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3599 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3600}
3601
3602void SensorInputMapperTest::prepareGyroAxes() {
3603 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3604 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3605 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3606 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3607 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3608 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3609}
3610
3611void SensorInputMapperTest::setAccelProperties() {
3612 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3613 /* sensorDataIndex */ 0);
3614 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3615 /* sensorDataIndex */ 1);
3616 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3617 /* sensorDataIndex */ 2);
3618 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3619 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3620 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3621 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3622 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3623}
3624
3625void SensorInputMapperTest::setGyroProperties() {
3626 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3627 /* sensorDataIndex */ 0);
3628 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3629 /* sensorDataIndex */ 1);
3630 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3631 /* sensorDataIndex */ 2);
3632 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3633 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3634 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3635 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3636 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3637}
3638
3639TEST_F(SensorInputMapperTest, GetSources) {
3640 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3641
3642 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3643}
3644
3645TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3646 setAccelProperties();
3647 prepareAccelAxes();
3648 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3649
3650 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3651 std::chrono::microseconds(10000),
3652 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003653 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3655 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3658 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003659
3660 NotifySensorArgs args;
3661 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3662 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3663 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3664
3665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3666 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3667 ASSERT_EQ(args.deviceId, DEVICE_ID);
3668 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3669 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3670 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3671 ASSERT_EQ(args.values, values);
3672 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3673}
3674
3675TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3676 setGyroProperties();
3677 prepareGyroAxes();
3678 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3679
3680 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3681 std::chrono::microseconds(10000),
3682 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003683 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3688 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003689
3690 NotifySensorArgs args;
3691 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3692 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3693 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3694
3695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3696 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3697 ASSERT_EQ(args.deviceId, DEVICE_ID);
3698 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3699 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3700 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3701 ASSERT_EQ(args.values, values);
3702 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3703}
3704
Michael Wrightd02c5b62014-02-10 15:10:22 -08003705// --- KeyboardInputMapperTest ---
3706
3707class KeyboardInputMapperTest : public InputMapperTest {
3708protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003709 const std::string UNIQUE_ID = "local:0";
3710
3711 void prepareDisplay(int32_t orientation);
3712
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003713 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003714 int32_t originalKeyCode, int32_t rotatedKeyCode,
3715 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003716};
3717
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003718/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3719 * orientation.
3720 */
3721void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003722 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3723 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003724}
3725
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003726void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003727 int32_t originalScanCode, int32_t originalKeyCode,
3728 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003729 NotifyKeyArgs args;
3730
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003731 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3733 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3734 ASSERT_EQ(originalScanCode, args.scanCode);
3735 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003736 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003737
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003738 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3740 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3741 ASSERT_EQ(originalScanCode, args.scanCode);
3742 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003743 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003744}
3745
Michael Wrightd02c5b62014-02-10 15:10:22 -08003746TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003747 KeyboardInputMapper& mapper =
3748 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3749 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003750
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003751 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003752}
3753
3754TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3755 const int32_t USAGE_A = 0x070004;
3756 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003757 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3758 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003759 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3760 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3761 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003762
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003763 KeyboardInputMapper& mapper =
3764 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3765 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003766 // Initial metastate is AMETA_NONE.
3767 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003768
3769 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003771 NotifyKeyArgs args;
3772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3773 ASSERT_EQ(DEVICE_ID, args.deviceId);
3774 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3775 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3776 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3777 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3778 ASSERT_EQ(KEY_HOME, args.scanCode);
3779 ASSERT_EQ(AMETA_NONE, args.metaState);
3780 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3781 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3782 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3783
3784 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003785 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3787 ASSERT_EQ(DEVICE_ID, args.deviceId);
3788 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3789 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3790 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3791 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3792 ASSERT_EQ(KEY_HOME, args.scanCode);
3793 ASSERT_EQ(AMETA_NONE, args.metaState);
3794 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3795 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3796 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3797
3798 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3802 ASSERT_EQ(DEVICE_ID, args.deviceId);
3803 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3804 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3805 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3806 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3807 ASSERT_EQ(0, args.scanCode);
3808 ASSERT_EQ(AMETA_NONE, args.metaState);
3809 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3810 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3811 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3812
3813 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003814 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3815 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3817 ASSERT_EQ(DEVICE_ID, args.deviceId);
3818 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3819 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3820 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3821 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3822 ASSERT_EQ(0, args.scanCode);
3823 ASSERT_EQ(AMETA_NONE, args.metaState);
3824 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3825 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3826 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3827
3828 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3832 ASSERT_EQ(DEVICE_ID, args.deviceId);
3833 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3834 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3835 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3836 ASSERT_EQ(0, args.keyCode);
3837 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3838 ASSERT_EQ(AMETA_NONE, args.metaState);
3839 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3840 ASSERT_EQ(0U, args.policyFlags);
3841 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3842
3843 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003844 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3845 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3847 ASSERT_EQ(DEVICE_ID, args.deviceId);
3848 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3849 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3850 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3851 ASSERT_EQ(0, args.keyCode);
3852 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3853 ASSERT_EQ(AMETA_NONE, args.metaState);
3854 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3855 ASSERT_EQ(0U, args.policyFlags);
3856 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3857}
3858
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003859/**
3860 * Ensure that the readTime is set to the time when the EV_KEY is received.
3861 */
3862TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3863 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3864
3865 KeyboardInputMapper& mapper =
3866 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3867 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3868 NotifyKeyArgs args;
3869
3870 // Key down
3871 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3873 ASSERT_EQ(12, args.readTime);
3874
3875 // Key up
3876 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3878 ASSERT_EQ(15, args.readTime);
3879}
3880
Michael Wrightd02c5b62014-02-10 15:10:22 -08003881TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003882 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3883 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003884 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3885 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3886 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003887
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003888 KeyboardInputMapper& mapper =
3889 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3890 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003891
Arthur Hung95f68612022-04-07 14:08:22 +08003892 // Initial metastate is AMETA_NONE.
3893 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003894
3895 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003896 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003897 NotifyKeyArgs args;
3898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3899 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003900 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003901 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003902
3903 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003904 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3906 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003907 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003908
3909 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003910 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3912 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003913 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003914
3915 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003916 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3918 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003919 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003920 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003921}
3922
3923TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003924 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3925 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3926 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3927 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003928
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003929 KeyboardInputMapper& mapper =
3930 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3931 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003932
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003933 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003934 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3935 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3936 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3937 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3938 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3939 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3940 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3941 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3942}
3943
3944TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003945 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3946 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3947 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3948 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003949
Michael Wrightd02c5b62014-02-10 15:10:22 -08003950 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003951 KeyboardInputMapper& mapper =
3952 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3953 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003954
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003955 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003956 ASSERT_NO_FATAL_FAILURE(
3957 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3958 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3959 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3960 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3961 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3962 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3963 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003964
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003965 clearViewports();
3966 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003967 ASSERT_NO_FATAL_FAILURE(
3968 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3969 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3970 AKEYCODE_DPAD_UP, DISPLAY_ID));
3971 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3972 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3973 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3974 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003975
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003976 clearViewports();
3977 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003978 ASSERT_NO_FATAL_FAILURE(
3979 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3980 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3981 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3982 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3983 AKEYCODE_DPAD_UP, DISPLAY_ID));
3984 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3985 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003986
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003987 clearViewports();
3988 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003989 ASSERT_NO_FATAL_FAILURE(
3990 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3991 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3992 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3993 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3994 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3995 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3996 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003997
3998 // Special case: if orientation changes while key is down, we still emit the same keycode
3999 // in the key up as we did in the key down.
4000 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004001 clearViewports();
4002 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4005 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4006 ASSERT_EQ(KEY_UP, args.scanCode);
4007 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
4008
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004009 clearViewports();
4010 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004011 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4013 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4014 ASSERT_EQ(KEY_UP, args.scanCode);
4015 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
4016}
4017
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004018TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
4019 // If the keyboard is not orientation aware,
4020 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004021 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004022
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004023 KeyboardInputMapper& mapper =
4024 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4025 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004026 NotifyKeyArgs args;
4027
4028 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4033 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
4034
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004035 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4040 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
4041}
4042
4043TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
4044 // If the keyboard is orientation aware,
4045 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004046 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004047
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004048 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004049 KeyboardInputMapper& mapper =
4050 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4051 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004052 NotifyKeyArgs args;
4053
4054 // Display id should be ADISPLAY_ID_NONE without any display configuration.
4055 // ^--- already checked by the previous test
4056
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004057 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004058 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004059 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4063 ASSERT_EQ(DISPLAY_ID, args.displayId);
4064
4065 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004066 clearViewports();
4067 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004068 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004069 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4073 ASSERT_EQ(newDisplayId, args.displayId);
4074}
4075
Michael Wrightd02c5b62014-02-10 15:10:22 -08004076TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004077 KeyboardInputMapper& mapper =
4078 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4079 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004081 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004082 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004083
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004084 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004085 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004086}
4087
Philip Junker4af3b3d2021-12-14 10:36:55 +01004088TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
4089 KeyboardInputMapper& mapper =
4090 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4091 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4092
4093 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
4094 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
4095 << "If a mapping is available, the result is equal to the mapping";
4096
4097 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
4098 << "If no mapping is available, the result is the key location";
4099}
4100
Michael Wrightd02c5b62014-02-10 15:10:22 -08004101TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004102 KeyboardInputMapper& mapper =
4103 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4104 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004105
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004106 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004107 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004108
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004109 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004110 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004111}
4112
4113TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004114 KeyboardInputMapper& mapper =
4115 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4116 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004117
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004118 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004119
Michael Wrightd02c5b62014-02-10 15:10:22 -08004120 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004121 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004122 ASSERT_TRUE(flags[0]);
4123 ASSERT_FALSE(flags[1]);
4124}
4125
4126TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004127 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4128 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4129 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4130 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4131 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4132 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004133
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004134 KeyboardInputMapper& mapper =
4135 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4136 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004137 // Initial metastate is AMETA_NONE.
4138 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004139
4140 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004141 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4142 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4143 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144
4145 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4147 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004148 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4149 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4150 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004151 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152
4153 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004156 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4157 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4158 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004159 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160
4161 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004164 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4165 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4166 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004167 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168
4169 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004172 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4173 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4174 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004175 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004176
4177 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004178 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004180 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4181 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4182 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004183 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184
4185 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004188 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4189 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4190 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004191 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004192}
4193
Chris Yea52ade12020-08-27 16:49:20 -07004194TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
4195 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
4196 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
4197 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
4198 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
4199
4200 KeyboardInputMapper& mapper =
4201 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4202 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4203
Chris Yea52ade12020-08-27 16:49:20 -07004204 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004205 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07004206 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4207 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
4208 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
4209 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4210
4211 NotifyKeyArgs args;
4212 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004213 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07004214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4215 ASSERT_EQ(AMETA_NONE, args.metaState);
4216 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4217 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4218 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4219
4220 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004221 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07004222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4223 ASSERT_EQ(AMETA_NONE, args.metaState);
4224 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4225 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4226 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4227}
4228
Arthur Hung2c9a3342019-07-23 14:18:59 +08004229TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
4230 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004231 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4232 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4233 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4234 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004235
4236 // keyboard 2.
4237 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08004238 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08004239 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004240 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08004241 std::shared_ptr<InputDevice> device2 =
4242 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004243 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08004244
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004245 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4246 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4247 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4248 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004249
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004250 KeyboardInputMapper& mapper =
4251 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4252 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004253
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004254 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004255 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004256 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004257 std::list<NotifyArgs> unused =
4258 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4259 0 /*changes*/);
4260 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004261
4262 // Prepared displays and associated info.
4263 constexpr uint8_t hdmi1 = 0;
4264 constexpr uint8_t hdmi2 = 1;
4265 const std::string SECONDARY_UNIQUE_ID = "local:1";
4266
4267 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
4268 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
4269
4270 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004271 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4272 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004273 ASSERT_FALSE(device2->isEnabled());
4274
4275 // Prepare second display.
4276 constexpr int32_t newDisplayId = 2;
4277 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004278 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004279 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004280 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004281 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004282 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4283 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004284
4285 // Device should be enabled after the associated display is found.
4286 ASSERT_TRUE(mDevice->isEnabled());
4287 ASSERT_TRUE(device2->isEnabled());
4288
4289 // Test pad key events
4290 ASSERT_NO_FATAL_FAILURE(
4291 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4292 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4293 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4294 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4295 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4296 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4297 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4298
4299 ASSERT_NO_FATAL_FAILURE(
4300 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
4301 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4302 AKEYCODE_DPAD_RIGHT, newDisplayId));
4303 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4304 AKEYCODE_DPAD_DOWN, newDisplayId));
4305 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4306 AKEYCODE_DPAD_LEFT, newDisplayId));
4307}
Michael Wrightd02c5b62014-02-10 15:10:22 -08004308
arthurhungc903df12020-08-11 15:08:42 +08004309TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
4310 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4311 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4312 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4313 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4314 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4315 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4316
4317 KeyboardInputMapper& mapper =
4318 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4319 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004320 // Initial metastate is AMETA_NONE.
4321 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004322
4323 // Initialization should have turned all of the lights off.
4324 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4325 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4326 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4327
4328 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004329 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4330 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004331 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4332 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4333
4334 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004335 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004337 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4338 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
4339
4340 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004341 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004343 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4344 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4345
4346 mFakeEventHub->removeDevice(EVENTHUB_ID);
4347 mReader->loopOnce();
4348
4349 // keyboard 2 should default toggle keys.
4350 const std::string USB2 = "USB2";
4351 const std::string DEVICE_NAME2 = "KEYBOARD2";
4352 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4353 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4354 std::shared_ptr<InputDevice> device2 =
4355 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004356 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004357 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4358 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4359 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4360 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4361 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4362 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4363
arthurhung6fe95782020-10-05 22:41:16 +08004364 KeyboardInputMapper& mapper2 =
4365 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4366 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004367 std::list<NotifyArgs> unused =
4368 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4369 0 /*changes*/);
4370 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08004371
4372 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4373 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4374 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004375 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4376 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004377}
4378
Arthur Hungcb40a002021-08-03 14:31:01 +00004379TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4380 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4381 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4382 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4383
4384 // Suppose we have two mappers. (DPAD + KEYBOARD)
4385 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4386 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4387 KeyboardInputMapper& mapper =
4388 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4389 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004390 // Initial metastate is AMETA_NONE.
4391 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004392
4393 mReader->toggleCapsLockState(DEVICE_ID);
4394 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4395}
4396
Arthur Hungfb3cc112022-04-13 07:39:50 +00004397TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4398 // keyboard 1.
4399 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4400 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4401 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4402 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4403 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4404 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4405
4406 KeyboardInputMapper& mapper1 =
4407 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4408 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4409
4410 // keyboard 2.
4411 const std::string USB2 = "USB2";
4412 const std::string DEVICE_NAME2 = "KEYBOARD2";
4413 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4414 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4415 std::shared_ptr<InputDevice> device2 =
4416 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4417 ftl::Flags<InputDeviceClass>(0));
4418 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4419 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4420 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4421 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4422 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4423 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4424
4425 KeyboardInputMapper& mapper2 =
4426 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4427 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004428 std::list<NotifyArgs> unused =
4429 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4430 0 /*changes*/);
4431 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004432
Arthur Hung95f68612022-04-07 14:08:22 +08004433 // Initial metastate is AMETA_NONE.
4434 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4435 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4436
4437 // Toggle num lock on and off.
4438 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4439 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004440 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4441 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4442 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4443
4444 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4445 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4446 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4447 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4448 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4449
4450 // Toggle caps lock on and off.
4451 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4452 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4453 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4454 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4455 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4456
4457 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4458 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4459 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4460 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4461 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4462
4463 // Toggle scroll lock on and off.
4464 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4465 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4466 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4467 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4468 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4469
4470 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4471 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4472 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4473 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4474 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4475}
4476
Arthur Hung2141d542022-08-23 07:45:21 +00004477TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4478 const int32_t USAGE_A = 0x070004;
4479 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4480 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4481
4482 KeyboardInputMapper& mapper =
4483 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4484 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4485 // Key down by scan code.
4486 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4487 NotifyKeyArgs args;
4488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4489 ASSERT_EQ(DEVICE_ID, args.deviceId);
4490 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4491 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4492 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4493 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4494 ASSERT_EQ(KEY_HOME, args.scanCode);
4495 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4496
4497 // Disable device, it should synthesize cancellation events for down events.
4498 mFakePolicy->addDisabledDevice(DEVICE_ID);
4499 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4500
4501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4502 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4503 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4504 ASSERT_EQ(KEY_HOME, args.scanCode);
4505 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4506}
4507
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004508// --- KeyboardInputMapperTest_ExternalDevice ---
4509
4510class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4511protected:
Chris Yea52ade12020-08-27 16:49:20 -07004512 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004513};
4514
4515TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004516 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4517 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004518
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004519 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4520 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4521 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4522 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004523
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004524 KeyboardInputMapper& mapper =
4525 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4526 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004527
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004528 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004529 NotifyKeyArgs args;
4530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4531 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4532
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004533 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4535 ASSERT_EQ(uint32_t(0), args.policyFlags);
4536
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004537 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4539 ASSERT_EQ(uint32_t(0), args.policyFlags);
4540
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004541 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4543 ASSERT_EQ(uint32_t(0), args.policyFlags);
4544
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4547 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4548
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004549 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4551 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4552}
4553
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004554TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004555 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004556
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004557 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4558 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4559 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004560
Powei Fengd041c5d2019-05-03 17:11:33 -07004561 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004562 KeyboardInputMapper& mapper =
4563 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4564 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004565
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004567 NotifyKeyArgs args;
4568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4569 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4570
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004571 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4573 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4574
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004575 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4577 ASSERT_EQ(uint32_t(0), args.policyFlags);
4578
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004579 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4581 ASSERT_EQ(uint32_t(0), args.policyFlags);
4582
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004583 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4585 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4586
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004587 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4589 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4590}
4591
Michael Wrightd02c5b62014-02-10 15:10:22 -08004592// --- CursorInputMapperTest ---
4593
4594class CursorInputMapperTest : public InputMapperTest {
4595protected:
4596 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4597
Michael Wright17db18e2020-06-26 20:51:44 +01004598 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004599
Chris Yea52ade12020-08-27 16:49:20 -07004600 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004601 InputMapperTest::SetUp();
4602
Michael Wright17db18e2020-06-26 20:51:44 +01004603 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004604 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605 }
4606
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004607 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4608 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004609
4610 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004611 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4612 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4613 }
4614
4615 void prepareSecondaryDisplay() {
4616 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4617 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4618 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004619 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004620
4621 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4622 float pressure) {
4623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4624 0.0f, 0.0f, 0.0f, EPSILON));
4625 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626};
4627
4628const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4629
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004630void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4631 int32_t originalY, int32_t rotatedX,
4632 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004633 NotifyMotionArgs args;
4634
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4636 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4637 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4639 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004640 ASSERT_NO_FATAL_FAILURE(
4641 assertCursorPointerCoords(args.pointerCoords[0],
4642 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4643 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004644}
4645
4646TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004647 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004648 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004649
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004650 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004651}
4652
4653TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004654 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004655 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004656
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004657 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004658}
4659
4660TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004661 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004662 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663
4664 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004665 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004666
4667 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004668 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4669 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004670 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4671 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4672
4673 // When the bounds are set, then there should be a valid motion range.
4674 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4675
4676 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004677 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004678
4679 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4680 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4681 1, 800 - 1, 0.0f, 0.0f));
4682 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4683 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4684 2, 480 - 1, 0.0f, 0.0f));
4685 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4686 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4687 0.0f, 1.0f, 0.0f, 0.0f));
4688}
4689
4690TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004691 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004692 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004693
4694 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004695 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004696
4697 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4698 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4699 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4700 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4701 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4702 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4703 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4704 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4705 0.0f, 1.0f, 0.0f, 0.0f));
4706}
4707
4708TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004709 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004710 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711
arthurhungdcef2dc2020-08-11 14:47:50 +08004712 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004713
4714 NotifyMotionArgs args;
4715
4716 // Button press.
4717 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004718 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4719 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4721 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4722 ASSERT_EQ(DEVICE_ID, args.deviceId);
4723 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4724 ASSERT_EQ(uint32_t(0), args.policyFlags);
4725 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4726 ASSERT_EQ(0, args.flags);
4727 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4728 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4729 ASSERT_EQ(0, args.edgeFlags);
4730 ASSERT_EQ(uint32_t(1), args.pointerCount);
4731 ASSERT_EQ(0, args.pointerProperties[0].id);
4732 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004733 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004734 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4735 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4736 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4737
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4739 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4740 ASSERT_EQ(DEVICE_ID, args.deviceId);
4741 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4742 ASSERT_EQ(uint32_t(0), args.policyFlags);
4743 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4744 ASSERT_EQ(0, args.flags);
4745 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4746 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4747 ASSERT_EQ(0, args.edgeFlags);
4748 ASSERT_EQ(uint32_t(1), args.pointerCount);
4749 ASSERT_EQ(0, args.pointerProperties[0].id);
4750 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004751 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004752 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4753 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4754 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4755
Michael Wrightd02c5b62014-02-10 15:10:22 -08004756 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004757 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4758 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4760 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4761 ASSERT_EQ(DEVICE_ID, args.deviceId);
4762 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4763 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004764 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4765 ASSERT_EQ(0, args.flags);
4766 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4767 ASSERT_EQ(0, args.buttonState);
4768 ASSERT_EQ(0, args.edgeFlags);
4769 ASSERT_EQ(uint32_t(1), args.pointerCount);
4770 ASSERT_EQ(0, args.pointerProperties[0].id);
4771 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004772 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004773 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4774 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4775 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4776
4777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4778 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4779 ASSERT_EQ(DEVICE_ID, args.deviceId);
4780 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4781 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4783 ASSERT_EQ(0, args.flags);
4784 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4785 ASSERT_EQ(0, args.buttonState);
4786 ASSERT_EQ(0, args.edgeFlags);
4787 ASSERT_EQ(uint32_t(1), args.pointerCount);
4788 ASSERT_EQ(0, args.pointerProperties[0].id);
4789 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004790 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004791 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4792 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4793 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4794}
4795
4796TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004797 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004798 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004799
4800 NotifyMotionArgs args;
4801
4802 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4804 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4806 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004807 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4808 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4809 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810
4811 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004812 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4813 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4815 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004816 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4817 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004818}
4819
4820TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004821 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004822 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823
4824 NotifyMotionArgs args;
4825
4826 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004827 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4830 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004831 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004832
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4834 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004835 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004836
Michael Wrightd02c5b62014-02-10 15:10:22 -08004837 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004838 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004841 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004842 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004843
4844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004845 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004846 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004847}
4848
4849TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004850 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004851 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004852
4853 NotifyMotionArgs args;
4854
4855 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4858 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4861 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004862 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4863 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4864 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4867 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004868 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4869 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4870 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004871
Michael Wrightd02c5b62014-02-10 15:10:22 -08004872 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004873 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4874 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4875 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4877 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004878 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4879 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4880 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004881
4882 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004883 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4884 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004886 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004887 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004888
4889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004890 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004891 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004892}
4893
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004894TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004895 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004896 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004897 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4898 // need to be rotated.
4899 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004900 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004901
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004902 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004903 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4904 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4905 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4906 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4907 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4908 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4909 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4910 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4911}
4912
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004913TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004914 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004915 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004916 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4917 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004918 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004920 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004921 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004922 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4923 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4924 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4925 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4926 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4927 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4928 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4929 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4930
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004931 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004932 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004933 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4934 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4935 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4936 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4937 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4938 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4939 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4940 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004941
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004942 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004943 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004944 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4945 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4946 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4947 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4948 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4949 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4950 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4951 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4952
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004953 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004954 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004955 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4956 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4957 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4958 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4959 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4960 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4961 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4962 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004963}
4964
4965TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004967 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004968
4969 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4970 mFakePointerController->setPosition(100, 200);
4971 mFakePointerController->setButtonState(0);
4972
4973 NotifyMotionArgs motionArgs;
4974 NotifyKeyArgs keyArgs;
4975
4976 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004977 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4978 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4980 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4981 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4982 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004983 ASSERT_NO_FATAL_FAILURE(
4984 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004985
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4987 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4988 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4989 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004990 ASSERT_NO_FATAL_FAILURE(
4991 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004992
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004996 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004997 ASSERT_EQ(0, motionArgs.buttonState);
4998 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004999 ASSERT_NO_FATAL_FAILURE(
5000 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005001
5002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005003 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004 ASSERT_EQ(0, motionArgs.buttonState);
5005 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005006 ASSERT_NO_FATAL_FAILURE(
5007 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005008
5009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005010 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005011 ASSERT_EQ(0, motionArgs.buttonState);
5012 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005013 ASSERT_NO_FATAL_FAILURE(
5014 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005015
5016 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
5018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
5019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5021 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5022 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5023 motionArgs.buttonState);
5024 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5025 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005026 ASSERT_NO_FATAL_FAILURE(
5027 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005028
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5030 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5031 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5032 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5033 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005034 ASSERT_NO_FATAL_FAILURE(
5035 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005036
5037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5038 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5039 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5040 motionArgs.buttonState);
5041 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5042 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005043 ASSERT_NO_FATAL_FAILURE(
5044 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005045
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005046 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
5047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005049 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005050 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5051 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005052 ASSERT_NO_FATAL_FAILURE(
5053 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005054
5055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005056 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005057 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5058 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005059 ASSERT_NO_FATAL_FAILURE(
5060 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005062 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5063 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005065 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5066 ASSERT_EQ(0, motionArgs.buttonState);
5067 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005068 ASSERT_NO_FATAL_FAILURE(
5069 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005070 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005072
5073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005074 ASSERT_EQ(0, motionArgs.buttonState);
5075 ASSERT_EQ(0, mFakePointerController->getButtonState());
5076 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005077 ASSERT_NO_FATAL_FAILURE(
5078 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005079
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5081 ASSERT_EQ(0, motionArgs.buttonState);
5082 ASSERT_EQ(0, mFakePointerController->getButtonState());
5083 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005084 ASSERT_NO_FATAL_FAILURE(
5085 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005086
5087 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005088 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
5089 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5091 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5092 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005093
Michael Wrightd02c5b62014-02-10 15:10:22 -08005094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005095 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005096 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5097 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005098 ASSERT_NO_FATAL_FAILURE(
5099 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005100
5101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5102 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5103 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5104 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005105 ASSERT_NO_FATAL_FAILURE(
5106 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005107
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
5109 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005111 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005112 ASSERT_EQ(0, motionArgs.buttonState);
5113 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005114 ASSERT_NO_FATAL_FAILURE(
5115 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005116
5117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005118 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005119 ASSERT_EQ(0, motionArgs.buttonState);
5120 ASSERT_EQ(0, mFakePointerController->getButtonState());
5121
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005122 ASSERT_NO_FATAL_FAILURE(
5123 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5125 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5126 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5127
5128 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005129 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
5130 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5132 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5133 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005134
Michael Wrightd02c5b62014-02-10 15:10:22 -08005135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005136 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005137 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5138 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005139 ASSERT_NO_FATAL_FAILURE(
5140 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005141
5142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5143 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5144 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5145 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005146 ASSERT_NO_FATAL_FAILURE(
5147 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005148
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
5150 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005152 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005153 ASSERT_EQ(0, motionArgs.buttonState);
5154 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005155 ASSERT_NO_FATAL_FAILURE(
5156 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005157
5158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5159 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5160 ASSERT_EQ(0, motionArgs.buttonState);
5161 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005162 ASSERT_NO_FATAL_FAILURE(
5163 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005164
Michael Wrightd02c5b62014-02-10 15:10:22 -08005165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5166 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5167 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5168
5169 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
5171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5173 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5174 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005175
Michael Wrightd02c5b62014-02-10 15:10:22 -08005176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005177 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005178 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5179 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005180 ASSERT_NO_FATAL_FAILURE(
5181 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005182
5183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5184 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5185 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5186 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005187 ASSERT_NO_FATAL_FAILURE(
5188 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005189
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005190 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
5191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005193 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005194 ASSERT_EQ(0, motionArgs.buttonState);
5195 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005196 ASSERT_NO_FATAL_FAILURE(
5197 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005198
5199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5200 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5201 ASSERT_EQ(0, motionArgs.buttonState);
5202 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005203 ASSERT_NO_FATAL_FAILURE(
5204 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005205
Michael Wrightd02c5b62014-02-10 15:10:22 -08005206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5207 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5208 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5209
5210 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
5212 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5214 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5215 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005216
Michael Wrightd02c5b62014-02-10 15:10:22 -08005217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005218 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005219 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5220 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005221 ASSERT_NO_FATAL_FAILURE(
5222 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005223
5224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5225 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5226 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5227 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005228 ASSERT_NO_FATAL_FAILURE(
5229 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005230
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005231 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
5232 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005234 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005235 ASSERT_EQ(0, motionArgs.buttonState);
5236 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005237 ASSERT_NO_FATAL_FAILURE(
5238 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005239
5240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5241 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5242 ASSERT_EQ(0, motionArgs.buttonState);
5243 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005244 ASSERT_NO_FATAL_FAILURE(
5245 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005246
Michael Wrightd02c5b62014-02-10 15:10:22 -08005247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5248 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5249 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5250}
5251
5252TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005253 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005254 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005255
5256 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5257 mFakePointerController->setPosition(100, 200);
5258 mFakePointerController->setButtonState(0);
5259
5260 NotifyMotionArgs args;
5261
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005262 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005266 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5267 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5269 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 +01005270 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005271}
5272
5273TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005274 addConfigurationProperty("cursor.mode", "pointer");
5275 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005276 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005277
5278 NotifyDeviceResetArgs resetArgs;
5279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5280 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5281 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5282
5283 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5284 mFakePointerController->setPosition(100, 200);
5285 mFakePointerController->setButtonState(0);
5286
5287 NotifyMotionArgs args;
5288
5289 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5291 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5294 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5295 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5297 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 +01005298 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005299
5300 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5304 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5305 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5307 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5309 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5310 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
5311 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5312 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5313
5314 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005315 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5316 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5318 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5319 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
5320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5321 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5323 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5324 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
5325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5326 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5327
5328 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005329 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
5330 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
5331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5333 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5336 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 +01005337 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005338
5339 // Disable pointer capture and check that the device generation got bumped
5340 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08005341 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005342 mFakePolicy->setPointerCapture(false);
5343 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08005344 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005345
5346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005347 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5348
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005349 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5350 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5353 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005354 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5356 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 +01005357 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005358}
5359
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00005360/**
5361 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
5362 * pointer acceleration or speed processing should not be applied.
5363 */
5364TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5365 addConfigurationProperty("cursor.mode", "pointer");
5366 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5367 100.f /*high threshold*/, 10.f /*acceleration*/);
5368 mFakePolicy->setVelocityControlParams(testParams);
5369 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5370
5371 NotifyDeviceResetArgs resetArgs;
5372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5373 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5374 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5375
5376 NotifyMotionArgs args;
5377
5378 // Move and verify scale is applied.
5379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5380 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5383 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5384 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5385 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5386 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5387 ASSERT_GT(relX, 10);
5388 ASSERT_GT(relY, 20);
5389
5390 // Enable Pointer Capture
5391 mFakePolicy->setPointerCapture(true);
5392 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5393 NotifyPointerCaptureChangedArgs captureArgs;
5394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5395 ASSERT_TRUE(captureArgs.request.enable);
5396
5397 // Move and verify scale is not applied.
5398 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5402 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5403 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5404 ASSERT_EQ(10, args.pointerCoords[0].getX());
5405 ASSERT_EQ(20, args.pointerCoords[0].getY());
5406}
5407
Prabir Pradhan208360b2022-06-24 18:37:04 +00005408TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5409 addConfigurationProperty("cursor.mode", "pointer");
5410 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5411
5412 NotifyDeviceResetArgs resetArgs;
5413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5414 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5415 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5416
5417 // Ensure the display is rotated.
5418 prepareDisplay(DISPLAY_ORIENTATION_90);
5419
5420 NotifyMotionArgs args;
5421
5422 // Verify that the coordinates are rotated.
5423 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5424 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5427 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5428 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5429 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5430 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5431
5432 // Enable Pointer Capture.
5433 mFakePolicy->setPointerCapture(true);
5434 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5435 NotifyPointerCaptureChangedArgs captureArgs;
5436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5437 ASSERT_TRUE(captureArgs.request.enable);
5438
5439 // Move and verify rotation is not applied.
5440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5441 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5444 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5445 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5446 ASSERT_EQ(10, args.pointerCoords[0].getX());
5447 ASSERT_EQ(20, args.pointerCoords[0].getY());
5448}
5449
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005450TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005451 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005452
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005453 // Set up the default display.
5454 prepareDisplay(DISPLAY_ORIENTATION_90);
5455
5456 // Set up the secondary display as the display on which the pointer should be shown.
5457 // The InputDevice is not associated with any display.
5458 prepareSecondaryDisplay();
5459 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005460 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5461
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005462 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005463 mFakePointerController->setPosition(100, 200);
5464 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005465
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005466 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005467 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5469 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005471 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5472 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5473 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005474 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005475}
5476
5477TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5478 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5479
5480 // Set up the default display.
5481 prepareDisplay(DISPLAY_ORIENTATION_90);
5482
5483 // Set up the secondary display as the display on which the pointer should be shown,
5484 // and associate the InputDevice with the secondary display.
5485 prepareSecondaryDisplay();
5486 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5487 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5488 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5489
5490 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5491 mFakePointerController->setPosition(100, 200);
5492 mFakePointerController->setButtonState(0);
5493
5494 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5495 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005498 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5499 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5500 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005501 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5502}
5503
5504TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5505 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5506
5507 // Set up the default display as the display on which the pointer should be shown.
5508 prepareDisplay(DISPLAY_ORIENTATION_90);
5509 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5510
5511 // Associate the InputDevice with the secondary display.
5512 prepareSecondaryDisplay();
5513 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5514 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5515
5516 // The mapper should not generate any events because it is associated with a display that is
5517 // different from the pointer display.
5518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005522}
5523
Michael Wrightd02c5b62014-02-10 15:10:22 -08005524// --- TouchInputMapperTest ---
5525
5526class TouchInputMapperTest : public InputMapperTest {
5527protected:
5528 static const int32_t RAW_X_MIN;
5529 static const int32_t RAW_X_MAX;
5530 static const int32_t RAW_Y_MIN;
5531 static const int32_t RAW_Y_MAX;
5532 static const int32_t RAW_TOUCH_MIN;
5533 static const int32_t RAW_TOUCH_MAX;
5534 static const int32_t RAW_TOOL_MIN;
5535 static const int32_t RAW_TOOL_MAX;
5536 static const int32_t RAW_PRESSURE_MIN;
5537 static const int32_t RAW_PRESSURE_MAX;
5538 static const int32_t RAW_ORIENTATION_MIN;
5539 static const int32_t RAW_ORIENTATION_MAX;
5540 static const int32_t RAW_DISTANCE_MIN;
5541 static const int32_t RAW_DISTANCE_MAX;
5542 static const int32_t RAW_TILT_MIN;
5543 static const int32_t RAW_TILT_MAX;
5544 static const int32_t RAW_ID_MIN;
5545 static const int32_t RAW_ID_MAX;
5546 static const int32_t RAW_SLOT_MIN;
5547 static const int32_t RAW_SLOT_MAX;
5548 static const float X_PRECISION;
5549 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005550 static const float X_PRECISION_VIRTUAL;
5551 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005552
5553 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005554 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005555
5556 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5557
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005558 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005559 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005560
Michael Wrightd02c5b62014-02-10 15:10:22 -08005561 enum Axes {
5562 POSITION = 1 << 0,
5563 TOUCH = 1 << 1,
5564 TOOL = 1 << 2,
5565 PRESSURE = 1 << 3,
5566 ORIENTATION = 1 << 4,
5567 MINOR = 1 << 5,
5568 ID = 1 << 6,
5569 DISTANCE = 1 << 7,
5570 TILT = 1 << 8,
5571 SLOT = 1 << 9,
5572 TOOL_TYPE = 1 << 10,
5573 };
5574
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005575 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5576 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005577 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005578 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005579 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005580 int32_t toRawX(float displayX);
5581 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005582 int32_t toRotatedRawX(float displayX);
5583 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005584 float toCookedX(float rawX, float rawY);
5585 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005586 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005587 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005588 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005589 float toDisplayY(int32_t rawY, int32_t displayHeight);
5590
Michael Wrightd02c5b62014-02-10 15:10:22 -08005591};
5592
5593const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5594const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5595const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5596const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5597const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5598const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5599const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5600const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005601const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5602const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005603const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5604const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5605const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5606const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5607const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5608const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5609const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5610const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5611const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5612const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5613const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5614const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005615const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5616 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5617const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5618 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005619const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5620 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005621
5622const float TouchInputMapperTest::GEOMETRIC_SCALE =
5623 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5624 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5625
5626const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5627 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5628 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5629};
5630
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005631void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005632 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5633 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005634}
5635
5636void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5637 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5638 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005639}
5640
Santos Cordonfa5cf462017-04-05 10:37:00 -07005641void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005642 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5643 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5644 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005645}
5646
Michael Wrightd02c5b62014-02-10 15:10:22 -08005647void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005648 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5649 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5650 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5651 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005652}
5653
Jason Gerecke489fda82012-09-07 17:19:40 -07005654void TouchInputMapperTest::prepareLocationCalibration() {
5655 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5656}
5657
Michael Wrightd02c5b62014-02-10 15:10:22 -08005658int32_t TouchInputMapperTest::toRawX(float displayX) {
5659 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5660}
5661
5662int32_t TouchInputMapperTest::toRawY(float displayY) {
5663 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5664}
5665
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005666int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5667 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5668}
5669
5670int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5671 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5672}
5673
Jason Gerecke489fda82012-09-07 17:19:40 -07005674float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5675 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5676 return rawX;
5677}
5678
5679float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5680 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5681 return rawY;
5682}
5683
Michael Wrightd02c5b62014-02-10 15:10:22 -08005684float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005685 return toDisplayX(rawX, DISPLAY_WIDTH);
5686}
5687
5688float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5689 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005690}
5691
5692float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005693 return toDisplayY(rawY, DISPLAY_HEIGHT);
5694}
5695
5696float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5697 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005698}
5699
5700
5701// --- SingleTouchInputMapperTest ---
5702
5703class SingleTouchInputMapperTest : public TouchInputMapperTest {
5704protected:
5705 void prepareButtons();
5706 void prepareAxes(int axes);
5707
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005708 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5709 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5710 void processUp(SingleTouchInputMapper& mappery);
5711 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5712 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5713 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5714 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5715 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5716 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005717};
5718
5719void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005720 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005721}
5722
5723void SingleTouchInputMapperTest::prepareAxes(int axes) {
5724 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005725 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5726 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005727 }
5728 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005729 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5730 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005731 }
5732 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005733 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5734 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005735 }
5736 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005737 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5738 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005739 }
5740 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005741 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5742 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005743 }
5744}
5745
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005746void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5748 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5749 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005750}
5751
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005752void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5754 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005755}
5756
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005757void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005758 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005759}
5760
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005761void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005762 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005763}
5764
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005765void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5766 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005768}
5769
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005770void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005771 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005772}
5773
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005774void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5775 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005776 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005778}
5779
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005780void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5781 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005782 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005783}
5784
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005785void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005787}
5788
Michael Wrightd02c5b62014-02-10 15:10:22 -08005789TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005790 prepareButtons();
5791 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005792 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005793
Harry Cutts16a24cc2022-10-26 15:22:19 +00005794 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005795}
5796
Michael Wrightd02c5b62014-02-10 15:10:22 -08005797TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005798 prepareButtons();
5799 prepareAxes(POSITION);
5800 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005801 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005802
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005803 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005804}
5805
5806TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005807 addConfigurationProperty("touch.deviceType", "touchScreen");
5808 prepareDisplay(DISPLAY_ORIENTATION_0);
5809 prepareButtons();
5810 prepareAxes(POSITION);
5811 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005812 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005813
5814 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005815 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005816
5817 // Virtual key is down.
5818 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5819 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5820 processDown(mapper, x, y);
5821 processSync(mapper);
5822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5823
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005824 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005825
5826 // Virtual key is up.
5827 processUp(mapper);
5828 processSync(mapper);
5829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5830
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005831 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005832}
5833
5834TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005835 addConfigurationProperty("touch.deviceType", "touchScreen");
5836 prepareDisplay(DISPLAY_ORIENTATION_0);
5837 prepareButtons();
5838 prepareAxes(POSITION);
5839 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005840 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005841
5842 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005843 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005844
5845 // Virtual key is down.
5846 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5847 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5848 processDown(mapper, x, y);
5849 processSync(mapper);
5850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5851
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005852 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005853
5854 // Virtual key is up.
5855 processUp(mapper);
5856 processSync(mapper);
5857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5858
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005859 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005860}
5861
5862TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005863 addConfigurationProperty("touch.deviceType", "touchScreen");
5864 prepareDisplay(DISPLAY_ORIENTATION_0);
5865 prepareButtons();
5866 prepareAxes(POSITION);
5867 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005868 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005869
Michael Wrightd02c5b62014-02-10 15:10:22 -08005870 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005871 ASSERT_TRUE(
5872 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005873 ASSERT_TRUE(flags[0]);
5874 ASSERT_FALSE(flags[1]);
5875}
5876
5877TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005878 addConfigurationProperty("touch.deviceType", "touchScreen");
5879 prepareDisplay(DISPLAY_ORIENTATION_0);
5880 prepareButtons();
5881 prepareAxes(POSITION);
5882 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005883 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005884
arthurhungdcef2dc2020-08-11 14:47:50 +08005885 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005886
5887 NotifyKeyArgs args;
5888
5889 // Press virtual key.
5890 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5891 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5892 processDown(mapper, x, y);
5893 processSync(mapper);
5894
5895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5896 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5897 ASSERT_EQ(DEVICE_ID, args.deviceId);
5898 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5899 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5900 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5901 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5902 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5903 ASSERT_EQ(KEY_HOME, args.scanCode);
5904 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5905 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5906
5907 // Release virtual key.
5908 processUp(mapper);
5909 processSync(mapper);
5910
5911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5912 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5913 ASSERT_EQ(DEVICE_ID, args.deviceId);
5914 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5915 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5916 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5917 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5918 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5919 ASSERT_EQ(KEY_HOME, args.scanCode);
5920 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5921 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5922
5923 // Should not have sent any motions.
5924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5925}
5926
5927TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005928 addConfigurationProperty("touch.deviceType", "touchScreen");
5929 prepareDisplay(DISPLAY_ORIENTATION_0);
5930 prepareButtons();
5931 prepareAxes(POSITION);
5932 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005933 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005934
arthurhungdcef2dc2020-08-11 14:47:50 +08005935 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005936
5937 NotifyKeyArgs keyArgs;
5938
5939 // Press virtual key.
5940 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5941 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5942 processDown(mapper, x, y);
5943 processSync(mapper);
5944
5945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5946 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5947 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5948 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5949 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5950 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5951 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5952 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5953 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5954 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5955 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5956
5957 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5958 // into the display area.
5959 y -= 100;
5960 processMove(mapper, x, y);
5961 processSync(mapper);
5962
5963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5964 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5965 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5966 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5967 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5968 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5969 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5970 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5971 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5972 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5973 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5974 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5975
5976 NotifyMotionArgs motionArgs;
5977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5978 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5979 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5980 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5981 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5982 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5983 ASSERT_EQ(0, motionArgs.flags);
5984 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5985 ASSERT_EQ(0, motionArgs.buttonState);
5986 ASSERT_EQ(0, motionArgs.edgeFlags);
5987 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5988 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5989 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5991 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5992 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5993 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5994 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5995
5996 // Keep moving out of bounds. Should generate a pointer move.
5997 y -= 50;
5998 processMove(mapper, x, y);
5999 processSync(mapper);
6000
6001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6002 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6003 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6004 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6005 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6007 ASSERT_EQ(0, motionArgs.flags);
6008 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6009 ASSERT_EQ(0, motionArgs.buttonState);
6010 ASSERT_EQ(0, motionArgs.edgeFlags);
6011 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6012 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6013 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6014 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6015 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6016 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6017 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6018 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6019
6020 // Release out of bounds. Should generate a pointer up.
6021 processUp(mapper);
6022 processSync(mapper);
6023
6024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6025 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6026 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6027 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6028 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6029 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6030 ASSERT_EQ(0, motionArgs.flags);
6031 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6032 ASSERT_EQ(0, motionArgs.buttonState);
6033 ASSERT_EQ(0, motionArgs.edgeFlags);
6034 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6035 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6036 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6037 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6038 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6039 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6040 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6041 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6042
6043 // Should not have sent any more keys or motions.
6044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6046}
6047
6048TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006049 addConfigurationProperty("touch.deviceType", "touchScreen");
6050 prepareDisplay(DISPLAY_ORIENTATION_0);
6051 prepareButtons();
6052 prepareAxes(POSITION);
6053 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006054 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006055
arthurhungdcef2dc2020-08-11 14:47:50 +08006056 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006057
6058 NotifyMotionArgs motionArgs;
6059
6060 // Initially go down out of bounds.
6061 int32_t x = -10;
6062 int32_t y = -10;
6063 processDown(mapper, x, y);
6064 processSync(mapper);
6065
6066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6067
6068 // Move into the display area. Should generate a pointer down.
6069 x = 50;
6070 y = 75;
6071 processMove(mapper, x, y);
6072 processSync(mapper);
6073
6074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6075 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6076 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6077 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6078 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6079 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6080 ASSERT_EQ(0, motionArgs.flags);
6081 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6082 ASSERT_EQ(0, motionArgs.buttonState);
6083 ASSERT_EQ(0, motionArgs.edgeFlags);
6084 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6085 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6086 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6087 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6088 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6089 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6090 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6091 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6092
6093 // Release. Should generate a pointer up.
6094 processUp(mapper);
6095 processSync(mapper);
6096
6097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6098 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6099 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6100 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6101 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6102 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6103 ASSERT_EQ(0, motionArgs.flags);
6104 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6105 ASSERT_EQ(0, motionArgs.buttonState);
6106 ASSERT_EQ(0, motionArgs.edgeFlags);
6107 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6108 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6109 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6110 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6111 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6112 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6113 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6114 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6115
6116 // Should not have sent any more keys or motions.
6117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6119}
6120
Santos Cordonfa5cf462017-04-05 10:37:00 -07006121TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07006122 addConfigurationProperty("touch.deviceType", "touchScreen");
6123 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
6124
6125 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
6126 prepareButtons();
6127 prepareAxes(POSITION);
6128 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006129 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07006130
arthurhungdcef2dc2020-08-11 14:47:50 +08006131 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006132
6133 NotifyMotionArgs motionArgs;
6134
6135 // Down.
6136 int32_t x = 100;
6137 int32_t y = 125;
6138 processDown(mapper, x, y);
6139 processSync(mapper);
6140
6141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6142 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6143 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6144 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6145 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6146 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6147 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6148 ASSERT_EQ(0, motionArgs.flags);
6149 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6150 ASSERT_EQ(0, motionArgs.buttonState);
6151 ASSERT_EQ(0, motionArgs.edgeFlags);
6152 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6153 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6154 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6155 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6156 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6157 1, 0, 0, 0, 0, 0, 0, 0));
6158 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6159 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6160 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6161
6162 // Move.
6163 x += 50;
6164 y += 75;
6165 processMove(mapper, x, y);
6166 processSync(mapper);
6167
6168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6169 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6170 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6171 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6172 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6173 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6175 ASSERT_EQ(0, motionArgs.flags);
6176 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6177 ASSERT_EQ(0, motionArgs.buttonState);
6178 ASSERT_EQ(0, motionArgs.edgeFlags);
6179 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6180 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6181 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6182 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6183 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6184 1, 0, 0, 0, 0, 0, 0, 0));
6185 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6186 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6187 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6188
6189 // Up.
6190 processUp(mapper);
6191 processSync(mapper);
6192
6193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6194 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6195 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6196 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6197 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6198 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6199 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6200 ASSERT_EQ(0, motionArgs.flags);
6201 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6202 ASSERT_EQ(0, motionArgs.buttonState);
6203 ASSERT_EQ(0, motionArgs.edgeFlags);
6204 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6205 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6206 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6207 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6208 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6209 1, 0, 0, 0, 0, 0, 0, 0));
6210 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6211 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6212 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6213
6214 // Should not have sent any more keys or motions.
6215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6217}
6218
Michael Wrightd02c5b62014-02-10 15:10:22 -08006219TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006220 addConfigurationProperty("touch.deviceType", "touchScreen");
6221 prepareDisplay(DISPLAY_ORIENTATION_0);
6222 prepareButtons();
6223 prepareAxes(POSITION);
6224 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006225 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006226
arthurhungdcef2dc2020-08-11 14:47:50 +08006227 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006228
6229 NotifyMotionArgs motionArgs;
6230
6231 // Down.
6232 int32_t x = 100;
6233 int32_t y = 125;
6234 processDown(mapper, x, y);
6235 processSync(mapper);
6236
6237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6238 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6239 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6240 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6241 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6242 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6243 ASSERT_EQ(0, motionArgs.flags);
6244 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6245 ASSERT_EQ(0, motionArgs.buttonState);
6246 ASSERT_EQ(0, motionArgs.edgeFlags);
6247 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6248 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6249 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6251 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6252 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6253 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6254 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6255
6256 // Move.
6257 x += 50;
6258 y += 75;
6259 processMove(mapper, x, y);
6260 processSync(mapper);
6261
6262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6263 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6264 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6265 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6266 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6268 ASSERT_EQ(0, motionArgs.flags);
6269 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6270 ASSERT_EQ(0, motionArgs.buttonState);
6271 ASSERT_EQ(0, motionArgs.edgeFlags);
6272 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6273 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6274 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6276 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6277 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6278 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6279 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6280
6281 // Up.
6282 processUp(mapper);
6283 processSync(mapper);
6284
6285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6286 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6287 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6288 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6289 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6290 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6291 ASSERT_EQ(0, motionArgs.flags);
6292 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6293 ASSERT_EQ(0, motionArgs.buttonState);
6294 ASSERT_EQ(0, motionArgs.edgeFlags);
6295 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6296 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6297 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6299 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6300 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6301 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6302 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6303
6304 // Should not have sent any more keys or motions.
6305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6307}
6308
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006309TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006310 addConfigurationProperty("touch.deviceType", "touchScreen");
6311 prepareButtons();
6312 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006313 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
6314 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006315 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006316
6317 NotifyMotionArgs args;
6318
6319 // Rotation 90.
6320 prepareDisplay(DISPLAY_ORIENTATION_90);
6321 processDown(mapper, toRawX(50), toRawY(75));
6322 processSync(mapper);
6323
6324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6325 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6326 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6327
6328 processUp(mapper);
6329 processSync(mapper);
6330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6331}
6332
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006333TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006334 addConfigurationProperty("touch.deviceType", "touchScreen");
6335 prepareButtons();
6336 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006337 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6338 // orientation-aware are affected by display rotation.
6339 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006340 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006341
6342 NotifyMotionArgs args;
6343
6344 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006345 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006346 prepareDisplay(DISPLAY_ORIENTATION_0);
6347 processDown(mapper, toRawX(50), toRawY(75));
6348 processSync(mapper);
6349
6350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6351 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6352 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6353
6354 processUp(mapper);
6355 processSync(mapper);
6356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6357
6358 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006359 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006360 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006361 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006362 processSync(mapper);
6363
6364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6365 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6366 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6367
6368 processUp(mapper);
6369 processSync(mapper);
6370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6371
6372 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006373 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006374 prepareDisplay(DISPLAY_ORIENTATION_180);
6375 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6376 processSync(mapper);
6377
6378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6379 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6380 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6381
6382 processUp(mapper);
6383 processSync(mapper);
6384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6385
6386 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006387 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006388 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006389 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006390 processSync(mapper);
6391
6392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6393 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6394 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6395
6396 processUp(mapper);
6397 processSync(mapper);
6398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6399}
6400
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006401TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6402 addConfigurationProperty("touch.deviceType", "touchScreen");
6403 prepareButtons();
6404 prepareAxes(POSITION);
6405 addConfigurationProperty("touch.orientationAware", "1");
6406 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6407 clearViewports();
6408 prepareDisplay(DISPLAY_ORIENTATION_0);
6409 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6410 NotifyMotionArgs args;
6411
6412 // Orientation 0.
6413 processDown(mapper, toRawX(50), toRawY(75));
6414 processSync(mapper);
6415
6416 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6417 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6418 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6419
6420 processUp(mapper);
6421 processSync(mapper);
6422 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6423}
6424
6425TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6426 addConfigurationProperty("touch.deviceType", "touchScreen");
6427 prepareButtons();
6428 prepareAxes(POSITION);
6429 addConfigurationProperty("touch.orientationAware", "1");
6430 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6431 clearViewports();
6432 prepareDisplay(DISPLAY_ORIENTATION_0);
6433 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6434 NotifyMotionArgs args;
6435
6436 // Orientation 90.
6437 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6438 processSync(mapper);
6439
6440 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6441 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6442 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6443
6444 processUp(mapper);
6445 processSync(mapper);
6446 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6447}
6448
6449TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6450 addConfigurationProperty("touch.deviceType", "touchScreen");
6451 prepareButtons();
6452 prepareAxes(POSITION);
6453 addConfigurationProperty("touch.orientationAware", "1");
6454 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6455 clearViewports();
6456 prepareDisplay(DISPLAY_ORIENTATION_0);
6457 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6458 NotifyMotionArgs args;
6459
6460 // Orientation 180.
6461 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6462 processSync(mapper);
6463
6464 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6465 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6466 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6467
6468 processUp(mapper);
6469 processSync(mapper);
6470 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6471}
6472
6473TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6474 addConfigurationProperty("touch.deviceType", "touchScreen");
6475 prepareButtons();
6476 prepareAxes(POSITION);
6477 addConfigurationProperty("touch.orientationAware", "1");
6478 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6479 clearViewports();
6480 prepareDisplay(DISPLAY_ORIENTATION_0);
6481 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6482 NotifyMotionArgs args;
6483
6484 // Orientation 270.
6485 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6486 processSync(mapper);
6487
6488 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6489 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6490 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6491
6492 processUp(mapper);
6493 processSync(mapper);
6494 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6495}
6496
6497TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6498 addConfigurationProperty("touch.deviceType", "touchScreen");
6499 prepareButtons();
6500 prepareAxes(POSITION);
6501 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6502 // orientation-aware are affected by display rotation.
6503 addConfigurationProperty("touch.orientationAware", "0");
6504 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6505 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6506
6507 NotifyMotionArgs args;
6508
6509 // Orientation 90, Rotation 0.
6510 clearViewports();
6511 prepareDisplay(DISPLAY_ORIENTATION_0);
6512 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6513 processSync(mapper);
6514
6515 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6516 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6517 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6518
6519 processUp(mapper);
6520 processSync(mapper);
6521 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6522
6523 // Orientation 90, Rotation 90.
6524 clearViewports();
6525 prepareDisplay(DISPLAY_ORIENTATION_90);
6526 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6527 processSync(mapper);
6528
6529 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6530 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6531 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6532
6533 processUp(mapper);
6534 processSync(mapper);
6535 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6536
6537 // Orientation 90, Rotation 180.
6538 clearViewports();
6539 prepareDisplay(DISPLAY_ORIENTATION_180);
6540 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6541 processSync(mapper);
6542
6543 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6544 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6545 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6546
6547 processUp(mapper);
6548 processSync(mapper);
6549 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6550
6551 // Orientation 90, Rotation 270.
6552 clearViewports();
6553 prepareDisplay(DISPLAY_ORIENTATION_270);
6554 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6555 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6556 processSync(mapper);
6557
6558 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6559 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6560 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6561
6562 processUp(mapper);
6563 processSync(mapper);
6564 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6565}
6566
Michael Wrightd02c5b62014-02-10 15:10:22 -08006567TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006568 addConfigurationProperty("touch.deviceType", "touchScreen");
6569 prepareDisplay(DISPLAY_ORIENTATION_0);
6570 prepareButtons();
6571 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006572 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006573
6574 // These calculations are based on the input device calibration documentation.
6575 int32_t rawX = 100;
6576 int32_t rawY = 200;
6577 int32_t rawPressure = 10;
6578 int32_t rawToolMajor = 12;
6579 int32_t rawDistance = 2;
6580 int32_t rawTiltX = 30;
6581 int32_t rawTiltY = 110;
6582
6583 float x = toDisplayX(rawX);
6584 float y = toDisplayY(rawY);
6585 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6586 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6587 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6588 float distance = float(rawDistance);
6589
6590 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6591 float tiltScale = M_PI / 180;
6592 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6593 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6594 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6595 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6596
6597 processDown(mapper, rawX, rawY);
6598 processPressure(mapper, rawPressure);
6599 processToolMajor(mapper, rawToolMajor);
6600 processDistance(mapper, rawDistance);
6601 processTilt(mapper, rawTiltX, rawTiltY);
6602 processSync(mapper);
6603
6604 NotifyMotionArgs args;
6605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6606 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6607 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6608 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6609}
6610
Jason Gerecke489fda82012-09-07 17:19:40 -07006611TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006612 addConfigurationProperty("touch.deviceType", "touchScreen");
6613 prepareDisplay(DISPLAY_ORIENTATION_0);
6614 prepareLocationCalibration();
6615 prepareButtons();
6616 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006617 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006618
6619 int32_t rawX = 100;
6620 int32_t rawY = 200;
6621
6622 float x = toDisplayX(toCookedX(rawX, rawY));
6623 float y = toDisplayY(toCookedY(rawX, rawY));
6624
6625 processDown(mapper, rawX, rawY);
6626 processSync(mapper);
6627
6628 NotifyMotionArgs args;
6629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6631 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6632}
6633
Michael Wrightd02c5b62014-02-10 15:10:22 -08006634TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006635 addConfigurationProperty("touch.deviceType", "touchScreen");
6636 prepareDisplay(DISPLAY_ORIENTATION_0);
6637 prepareButtons();
6638 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006639 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006640
6641 NotifyMotionArgs motionArgs;
6642 NotifyKeyArgs keyArgs;
6643
6644 processDown(mapper, 100, 200);
6645 processSync(mapper);
6646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6647 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6648 ASSERT_EQ(0, motionArgs.buttonState);
6649
6650 // press BTN_LEFT, release BTN_LEFT
6651 processKey(mapper, BTN_LEFT, 1);
6652 processSync(mapper);
6653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6655 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6656
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6658 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6659 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6660
Michael Wrightd02c5b62014-02-10 15:10:22 -08006661 processKey(mapper, BTN_LEFT, 0);
6662 processSync(mapper);
6663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006664 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006665 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006666
6667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006668 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006669 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006670
6671 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6672 processKey(mapper, BTN_RIGHT, 1);
6673 processKey(mapper, BTN_MIDDLE, 1);
6674 processSync(mapper);
6675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6676 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6677 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6678 motionArgs.buttonState);
6679
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6681 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6682 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6683
6684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6685 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6686 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6687 motionArgs.buttonState);
6688
Michael Wrightd02c5b62014-02-10 15:10:22 -08006689 processKey(mapper, BTN_RIGHT, 0);
6690 processSync(mapper);
6691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006692 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006693 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006694
6695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006697 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006698
6699 processKey(mapper, BTN_MIDDLE, 0);
6700 processSync(mapper);
6701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006702 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006703 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006704
6705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006706 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006707 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006708
6709 // press BTN_BACK, release BTN_BACK
6710 processKey(mapper, BTN_BACK, 1);
6711 processSync(mapper);
6712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6713 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6714 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006715
Michael Wrightd02c5b62014-02-10 15:10:22 -08006716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006717 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006718 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6719
6720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6721 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6722 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006723
6724 processKey(mapper, BTN_BACK, 0);
6725 processSync(mapper);
6726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006727 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006728 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006729
6730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006731 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006732 ASSERT_EQ(0, motionArgs.buttonState);
6733
Michael Wrightd02c5b62014-02-10 15:10:22 -08006734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6735 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6736 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6737
6738 // press BTN_SIDE, release BTN_SIDE
6739 processKey(mapper, BTN_SIDE, 1);
6740 processSync(mapper);
6741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6742 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6743 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006744
Michael Wrightd02c5b62014-02-10 15:10:22 -08006745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006747 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6748
6749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6750 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6751 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006752
6753 processKey(mapper, BTN_SIDE, 0);
6754 processSync(mapper);
6755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006756 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006757 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006758
6759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006761 ASSERT_EQ(0, motionArgs.buttonState);
6762
Michael Wrightd02c5b62014-02-10 15:10:22 -08006763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6764 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6765 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6766
6767 // press BTN_FORWARD, release BTN_FORWARD
6768 processKey(mapper, BTN_FORWARD, 1);
6769 processSync(mapper);
6770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6771 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6772 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006773
Michael Wrightd02c5b62014-02-10 15:10:22 -08006774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006775 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006776 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6777
6778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6779 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6780 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006781
6782 processKey(mapper, BTN_FORWARD, 0);
6783 processSync(mapper);
6784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006785 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006786 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006787
6788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006790 ASSERT_EQ(0, motionArgs.buttonState);
6791
Michael Wrightd02c5b62014-02-10 15:10:22 -08006792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6793 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6794 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6795
6796 // press BTN_EXTRA, release BTN_EXTRA
6797 processKey(mapper, BTN_EXTRA, 1);
6798 processSync(mapper);
6799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6800 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6801 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006802
Michael Wrightd02c5b62014-02-10 15:10:22 -08006803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006804 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006805 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6806
6807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6808 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6809 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006810
6811 processKey(mapper, BTN_EXTRA, 0);
6812 processSync(mapper);
6813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006814 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006815 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006816
6817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006818 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006819 ASSERT_EQ(0, motionArgs.buttonState);
6820
Michael Wrightd02c5b62014-02-10 15:10:22 -08006821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6822 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6823 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6824
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6826
Michael Wrightd02c5b62014-02-10 15:10:22 -08006827 // press BTN_STYLUS, release BTN_STYLUS
6828 processKey(mapper, BTN_STYLUS, 1);
6829 processSync(mapper);
6830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006832 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6833
6834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6835 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6836 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006837
6838 processKey(mapper, BTN_STYLUS, 0);
6839 processSync(mapper);
6840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006841 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006842 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006843
6844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006845 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006846 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006847
6848 // press BTN_STYLUS2, release BTN_STYLUS2
6849 processKey(mapper, BTN_STYLUS2, 1);
6850 processSync(mapper);
6851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6852 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006853 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6854
6855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6856 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6857 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006858
6859 processKey(mapper, BTN_STYLUS2, 0);
6860 processSync(mapper);
6861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006862 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006863 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006864
6865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006867 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006868
6869 // release touch
6870 processUp(mapper);
6871 processSync(mapper);
6872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6873 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6874 ASSERT_EQ(0, motionArgs.buttonState);
6875}
6876
6877TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006878 addConfigurationProperty("touch.deviceType", "touchScreen");
6879 prepareDisplay(DISPLAY_ORIENTATION_0);
6880 prepareButtons();
6881 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006882 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006883
6884 NotifyMotionArgs motionArgs;
6885
6886 // default tool type is finger
6887 processDown(mapper, 100, 200);
6888 processSync(mapper);
6889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6890 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6891 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6892
6893 // eraser
6894 processKey(mapper, BTN_TOOL_RUBBER, 1);
6895 processSync(mapper);
6896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6897 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6898 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6899
6900 // stylus
6901 processKey(mapper, BTN_TOOL_RUBBER, 0);
6902 processKey(mapper, BTN_TOOL_PEN, 1);
6903 processSync(mapper);
6904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6905 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6906 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6907
6908 // brush
6909 processKey(mapper, BTN_TOOL_PEN, 0);
6910 processKey(mapper, BTN_TOOL_BRUSH, 1);
6911 processSync(mapper);
6912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6913 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6914 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6915
6916 // pencil
6917 processKey(mapper, BTN_TOOL_BRUSH, 0);
6918 processKey(mapper, BTN_TOOL_PENCIL, 1);
6919 processSync(mapper);
6920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6921 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6922 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6923
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006924 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006925 processKey(mapper, BTN_TOOL_PENCIL, 0);
6926 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6927 processSync(mapper);
6928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6929 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6930 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6931
6932 // mouse
6933 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6934 processKey(mapper, BTN_TOOL_MOUSE, 1);
6935 processSync(mapper);
6936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6937 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6938 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6939
6940 // lens
6941 processKey(mapper, BTN_TOOL_MOUSE, 0);
6942 processKey(mapper, BTN_TOOL_LENS, 1);
6943 processSync(mapper);
6944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6945 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6946 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6947
6948 // double-tap
6949 processKey(mapper, BTN_TOOL_LENS, 0);
6950 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6951 processSync(mapper);
6952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6953 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6954 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6955
6956 // triple-tap
6957 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6958 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6959 processSync(mapper);
6960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6961 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6962 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6963
6964 // quad-tap
6965 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6966 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6967 processSync(mapper);
6968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6969 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6970 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6971
6972 // finger
6973 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6974 processKey(mapper, BTN_TOOL_FINGER, 1);
6975 processSync(mapper);
6976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6977 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6978 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6979
6980 // stylus trumps finger
6981 processKey(mapper, BTN_TOOL_PEN, 1);
6982 processSync(mapper);
6983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6984 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6985 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6986
6987 // eraser trumps stylus
6988 processKey(mapper, BTN_TOOL_RUBBER, 1);
6989 processSync(mapper);
6990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6991 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6992 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6993
6994 // mouse trumps eraser
6995 processKey(mapper, BTN_TOOL_MOUSE, 1);
6996 processSync(mapper);
6997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6998 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6999 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7000
7001 // back to default tool type
7002 processKey(mapper, BTN_TOOL_MOUSE, 0);
7003 processKey(mapper, BTN_TOOL_RUBBER, 0);
7004 processKey(mapper, BTN_TOOL_PEN, 0);
7005 processKey(mapper, BTN_TOOL_FINGER, 0);
7006 processSync(mapper);
7007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7008 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7009 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7010}
7011
7012TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007013 addConfigurationProperty("touch.deviceType", "touchScreen");
7014 prepareDisplay(DISPLAY_ORIENTATION_0);
7015 prepareButtons();
7016 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007017 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007018 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007019
7020 NotifyMotionArgs motionArgs;
7021
7022 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7023 processKey(mapper, BTN_TOOL_FINGER, 1);
7024 processMove(mapper, 100, 200);
7025 processSync(mapper);
7026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7027 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7028 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7029 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7030
7031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7032 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7034 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7035
7036 // move a little
7037 processMove(mapper, 150, 250);
7038 processSync(mapper);
7039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7040 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7042 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7043
7044 // down when BTN_TOUCH is pressed, pressure defaults to 1
7045 processKey(mapper, BTN_TOUCH, 1);
7046 processSync(mapper);
7047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7048 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7049 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7050 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7051
7052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7053 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7054 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7055 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7056
7057 // up when BTN_TOUCH is released, hover restored
7058 processKey(mapper, BTN_TOUCH, 0);
7059 processSync(mapper);
7060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7061 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7063 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7064
7065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7066 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7067 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7068 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7069
7070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7071 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7073 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7074
7075 // exit hover when pointer goes away
7076 processKey(mapper, BTN_TOOL_FINGER, 0);
7077 processSync(mapper);
7078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7079 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7080 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7081 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7082}
7083
7084TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007085 addConfigurationProperty("touch.deviceType", "touchScreen");
7086 prepareDisplay(DISPLAY_ORIENTATION_0);
7087 prepareButtons();
7088 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007089 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007090
7091 NotifyMotionArgs motionArgs;
7092
7093 // initially hovering because pressure is 0
7094 processDown(mapper, 100, 200);
7095 processPressure(mapper, 0);
7096 processSync(mapper);
7097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7098 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7099 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7100 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7101
7102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7103 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7104 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7105 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7106
7107 // move a little
7108 processMove(mapper, 150, 250);
7109 processSync(mapper);
7110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7111 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7112 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7113 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7114
7115 // down when pressure is non-zero
7116 processPressure(mapper, RAW_PRESSURE_MAX);
7117 processSync(mapper);
7118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7119 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7121 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7122
7123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7124 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7126 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7127
7128 // up when pressure becomes 0, hover restored
7129 processPressure(mapper, 0);
7130 processSync(mapper);
7131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7132 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7134 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7135
7136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7137 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7138 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7139 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7140
7141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7142 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7143 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7144 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7145
7146 // exit hover when pointer goes away
7147 processUp(mapper);
7148 processSync(mapper);
7149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7150 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7151 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7152 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7153}
7154
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007155TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
7156 addConfigurationProperty("touch.deviceType", "touchScreen");
7157 prepareDisplay(DISPLAY_ORIENTATION_0);
7158 prepareButtons();
7159 prepareAxes(POSITION | PRESSURE);
7160 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7161
7162 // Touch down.
7163 processDown(mapper, 100, 200);
7164 processPressure(mapper, 1);
7165 processSync(mapper);
7166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7167 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
7168
7169 // Reset the mapper. This should cancel the ongoing gesture.
7170 resetMapper(mapper, ARBITRARY_TIME);
7171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7172 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
7173
7174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7175}
7176
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007177TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
7178 addConfigurationProperty("touch.deviceType", "touchScreen");
7179 prepareDisplay(DISPLAY_ORIENTATION_0);
7180 prepareButtons();
7181 prepareAxes(POSITION | PRESSURE);
7182 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7183
7184 // Set the initial state for the touch pointer.
7185 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
7186 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
7187 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
7188 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7189
7190 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007191 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
7192 // does not generate any events.
7193 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007194
7195 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
7196 // the recreated touch state to generate a down event.
7197 processSync(mapper);
7198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7199 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
7200
7201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7202}
7203
lilinnan687e58f2022-07-19 16:00:50 +08007204TEST_F(SingleTouchInputMapperTest,
7205 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
7206 addConfigurationProperty("touch.deviceType", "touchScreen");
7207 prepareDisplay(DISPLAY_ORIENTATION_0);
7208 prepareButtons();
7209 prepareAxes(POSITION);
7210 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7211 NotifyMotionArgs motionArgs;
7212
7213 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00007214 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08007215 processSync(mapper);
7216
7217 // We should receive a down event
7218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7219 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7220
7221 // Change display id
7222 clearViewports();
7223 prepareSecondaryDisplay(ViewportType::INTERNAL);
7224
7225 // We should receive a cancel event
7226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7227 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7228 // Then receive reset called
7229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7230}
7231
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007232TEST_F(SingleTouchInputMapperTest,
7233 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
7234 addConfigurationProperty("touch.deviceType", "touchScreen");
7235 prepareDisplay(DISPLAY_ORIENTATION_0);
7236 prepareButtons();
7237 prepareAxes(POSITION);
7238 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7240 NotifyMotionArgs motionArgs;
7241
7242 // Start a new gesture.
7243 processDown(mapper, 100, 200);
7244 processSync(mapper);
7245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7246 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7247
7248 // Make the viewport inactive. This will put the device in disabled mode.
7249 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7250 viewport->isActive = false;
7251 mFakePolicy->updateViewport(*viewport);
7252 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7253
7254 // We should receive a cancel event for the ongoing gesture.
7255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7256 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7257 // Then we should be notified that the device was reset.
7258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7259
7260 // No events are generated while the viewport is inactive.
7261 processMove(mapper, 101, 201);
7262 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007263 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007264 processSync(mapper);
7265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7266
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007267 // Start a new gesture while the viewport is still inactive.
7268 processDown(mapper, 300, 400);
7269 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
7270 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
7271 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7272 processSync(mapper);
7273
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007274 // Make the viewport active again. The device should resume processing events.
7275 viewport->isActive = true;
7276 mFakePolicy->updateViewport(*viewport);
7277 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7278
7279 // The device is reset because it changes back to direct mode, without generating any events.
7280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7282
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007283 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007284 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7286 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007287
7288 // No more events.
7289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
7291}
7292
Prabir Pradhan211ba622022-10-31 21:09:21 +00007293TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
7294 addConfigurationProperty("touch.deviceType", "touchScreen");
7295 prepareDisplay(DISPLAY_ORIENTATION_0);
7296 prepareButtons();
7297 prepareAxes(POSITION);
7298 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7300
7301 // Press a stylus button.
7302 processKey(mapper, BTN_STYLUS, 1);
7303 processSync(mapper);
7304
7305 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
7306 processDown(mapper, 100, 200);
7307 processSync(mapper);
7308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7309 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7310 WithCoords(toDisplayX(100), toDisplayY(200)),
7311 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7313 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7314 WithCoords(toDisplayX(100), toDisplayY(200)),
7315 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7316
7317 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
7318 // the button has not actually been released, since there will be no pointers through which the
7319 // button state can be reported. The event is generated at the location of the pointer before
7320 // it went up.
7321 processUp(mapper);
7322 processSync(mapper);
7323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7324 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7325 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7327 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7328 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7329}
7330
Prabir Pradhan5632d622021-09-06 07:57:20 -07007331// --- TouchDisplayProjectionTest ---
7332
7333class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7334public:
7335 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7336 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7337 // rotated equivalent of the given un-rotated physical display bounds.
7338 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
7339 uint32_t inverseRotationFlags;
7340 auto width = DISPLAY_WIDTH;
7341 auto height = DISPLAY_HEIGHT;
7342 switch (orientation) {
7343 case DISPLAY_ORIENTATION_90:
7344 inverseRotationFlags = ui::Transform::ROT_270;
7345 std::swap(width, height);
7346 break;
7347 case DISPLAY_ORIENTATION_180:
7348 inverseRotationFlags = ui::Transform::ROT_180;
7349 break;
7350 case DISPLAY_ORIENTATION_270:
7351 inverseRotationFlags = ui::Transform::ROT_90;
7352 std::swap(width, height);
7353 break;
7354 case DISPLAY_ORIENTATION_0:
7355 inverseRotationFlags = ui::Transform::ROT_0;
7356 break;
7357 default:
7358 FAIL() << "Invalid orientation: " << orientation;
7359 }
7360
7361 const ui::Transform rotation(inverseRotationFlags, width, height);
7362 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7363
7364 std::optional<DisplayViewport> internalViewport =
7365 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7366 DisplayViewport& v = *internalViewport;
7367 v.displayId = DISPLAY_ID;
7368 v.orientation = orientation;
7369
7370 v.logicalLeft = 0;
7371 v.logicalTop = 0;
7372 v.logicalRight = 100;
7373 v.logicalBottom = 100;
7374
7375 v.physicalLeft = rotatedPhysicalDisplay.left;
7376 v.physicalTop = rotatedPhysicalDisplay.top;
7377 v.physicalRight = rotatedPhysicalDisplay.right;
7378 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7379
7380 v.deviceWidth = width;
7381 v.deviceHeight = height;
7382
7383 v.isActive = true;
7384 v.uniqueId = UNIQUE_ID;
7385 v.type = ViewportType::INTERNAL;
7386 mFakePolicy->updateViewport(v);
7387 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7388 }
7389
7390 void assertReceivedMove(const Point& point) {
7391 NotifyMotionArgs motionArgs;
7392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7394 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7395 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7396 1, 0, 0, 0, 0, 0, 0, 0));
7397 }
7398};
7399
7400TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7401 addConfigurationProperty("touch.deviceType", "touchScreen");
7402 prepareDisplay(DISPLAY_ORIENTATION_0);
7403
7404 prepareButtons();
7405 prepareAxes(POSITION);
7406 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7407
7408 NotifyMotionArgs motionArgs;
7409
7410 // Configure the DisplayViewport such that the logical display maps to a subsection of
7411 // the display panel called the physical display. Here, the physical display is bounded by the
7412 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7413 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7414 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7415 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7416
7417 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7418 DISPLAY_ORIENTATION_270}) {
7419 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7420
7421 // Touches outside the physical display should be ignored, and should not generate any
7422 // events. Ensure touches at the following points that lie outside of the physical display
7423 // area do not generate any events.
7424 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7425 processDown(mapper, toRawX(point.x), toRawY(point.y));
7426 processSync(mapper);
7427 processUp(mapper);
7428 processSync(mapper);
7429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7430 << "Unexpected event generated for touch outside physical display at point: "
7431 << point.x << ", " << point.y;
7432 }
7433 }
7434}
7435
7436TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7437 addConfigurationProperty("touch.deviceType", "touchScreen");
7438 prepareDisplay(DISPLAY_ORIENTATION_0);
7439
7440 prepareButtons();
7441 prepareAxes(POSITION);
7442 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7443
7444 NotifyMotionArgs motionArgs;
7445
7446 // Configure the DisplayViewport such that the logical display maps to a subsection of
7447 // the display panel called the physical display. Here, the physical display is bounded by the
7448 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7449 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7450
7451 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7452 DISPLAY_ORIENTATION_270}) {
7453 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7454
7455 // Touches that start outside the physical display should be ignored until it enters the
7456 // physical display bounds, at which point it should generate a down event. Start a touch at
7457 // the point (5, 100), which is outside the physical display bounds.
7458 static const Point kOutsidePoint{5, 100};
7459 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7460 processSync(mapper);
7461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7462
7463 // Move the touch into the physical display area. This should generate a pointer down.
7464 processMove(mapper, toRawX(11), toRawY(21));
7465 processSync(mapper);
7466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7467 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7468 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7469 ASSERT_NO_FATAL_FAILURE(
7470 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7471
7472 // Move the touch inside the physical display area. This should generate a pointer move.
7473 processMove(mapper, toRawX(69), toRawY(159));
7474 processSync(mapper);
7475 assertReceivedMove({69, 159});
7476
7477 // Move outside the physical display area. Since the pointer is already down, this should
7478 // now continue generating events.
7479 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7480 processSync(mapper);
7481 assertReceivedMove(kOutsidePoint);
7482
7483 // Release. This should generate a pointer up.
7484 processUp(mapper);
7485 processSync(mapper);
7486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7487 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7488 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7489 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7490
7491 // Ensure no more events were generated.
7492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7494 }
7495}
7496
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007497// --- ExternalStylusFusionTest ---
7498
7499class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7500public:
7501 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7502 addConfigurationProperty("touch.deviceType", "touchScreen");
7503 prepareDisplay(DISPLAY_ORIENTATION_0);
7504 prepareButtons();
7505 prepareAxes(POSITION);
7506 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7507
7508 mStylusState.when = ARBITRARY_TIME;
7509 mStylusState.pressure = 0.f;
7510 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7511 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
7512 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
7513 processExternalStylusState(mapper);
7514 return mapper;
7515 }
7516
7517 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7518 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7519 for (const NotifyArgs& args : generatedArgs) {
7520 mFakeListener->notify(args);
7521 }
7522 // Loop the reader to flush the input listener queue.
7523 mReader->loopOnce();
7524 return generatedArgs;
7525 }
7526
7527protected:
7528 StylusState mStylusState{};
7529 static constexpr uint32_t EXPECTED_SOURCE =
7530 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7531
7532 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7533 auto toolTypeSource =
7534 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7535
7536 // The first pointer is withheld.
7537 processDown(mapper, 100, 200);
7538 processSync(mapper);
7539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7540 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7541 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7542
7543 // The external stylus reports pressure. The withheld finger pointer is released as a
7544 // stylus.
7545 mStylusState.pressure = 1.f;
7546 processExternalStylusState(mapper);
7547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7548 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7549 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7550
7551 // Subsequent pointer events are not withheld.
7552 processMove(mapper, 101, 201);
7553 processSync(mapper);
7554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7555 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7556
7557 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7559 }
7560
7561 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7562 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7563
7564 // Releasing the touch pointer ends the gesture.
7565 processUp(mapper);
7566 processSync(mapper);
7567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7568 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7569 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7570
7571 mStylusState.pressure = 0.f;
7572 processExternalStylusState(mapper);
7573 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7575 }
7576
7577 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7578 auto toolTypeSource =
7579 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER));
7580
7581 // The first pointer is withheld when an external stylus is connected,
7582 // and a timeout is requested.
7583 processDown(mapper, 100, 200);
7584 processSync(mapper);
7585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7586 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7587 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7588
7589 // If the timeout expires early, it is requested again.
7590 handleTimeout(mapper, ARBITRARY_TIME + 1);
7591 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7592 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7593
7594 // When the timeout expires, the withheld touch is released as a finger pointer.
7595 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7597 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7598
7599 // Subsequent pointer events are not withheld.
7600 processMove(mapper, 101, 201);
7601 processSync(mapper);
7602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7603 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7604 processUp(mapper);
7605 processSync(mapper);
7606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7607 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7608
7609 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7611 }
7612
7613private:
7614 InputDeviceInfo mExternalStylusDeviceInfo{};
7615};
7616
7617TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7618 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7619 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7620}
7621
7622TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7623 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7624 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7625}
7626
7627TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7628 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7629 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7630}
7631
7632// Test a successful stylus fusion gesture where the pressure is reported by the external
7633// before the touch is reported by the touchscreen.
7634TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7635 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7636 auto toolTypeSource =
7637 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7638
7639 // The external stylus reports pressure first. It is ignored for now.
7640 mStylusState.pressure = 1.f;
7641 processExternalStylusState(mapper);
7642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7643 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7644
7645 // When the touch goes down afterwards, it is reported as a stylus pointer.
7646 processDown(mapper, 100, 200);
7647 processSync(mapper);
7648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7649 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7650 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7651
7652 processMove(mapper, 101, 201);
7653 processSync(mapper);
7654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7655 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7656 processUp(mapper);
7657 processSync(mapper);
7658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7659 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7660
7661 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7663}
7664
7665TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7666 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7667
7668 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7669 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7670
7671 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7672 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7673 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7674 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7675}
7676
7677TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7678 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7679 auto toolTypeSource =
7680 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7681
7682 mStylusState.pressure = 0.8f;
7683 processExternalStylusState(mapper);
7684 processDown(mapper, 100, 200);
7685 processSync(mapper);
7686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7687 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7688 WithPressure(0.8f))));
7689 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7690
7691 // The external stylus reports a pressure change. We wait for some time for a touch event.
7692 mStylusState.pressure = 0.6f;
7693 processExternalStylusState(mapper);
7694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7695 ASSERT_NO_FATAL_FAILURE(
7696 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7697
7698 // If a touch is reported within the timeout, it reports the updated pressure.
7699 processMove(mapper, 101, 201);
7700 processSync(mapper);
7701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7702 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7703 WithPressure(0.6f))));
7704 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7705
7706 // There is another pressure change.
7707 mStylusState.pressure = 0.5f;
7708 processExternalStylusState(mapper);
7709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7710 ASSERT_NO_FATAL_FAILURE(
7711 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7712
7713 // If a touch is not reported within the timeout, a move event is generated to report
7714 // the new pressure.
7715 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7717 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7718 WithPressure(0.5f))));
7719
7720 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7721 // repeated indefinitely.
7722 mStylusState.pressure = 0.0f;
7723 processExternalStylusState(mapper);
7724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7725 ASSERT_NO_FATAL_FAILURE(
7726 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7727 processMove(mapper, 102, 202);
7728 processSync(mapper);
7729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7730 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7731 WithPressure(0.5f))));
7732 processMove(mapper, 103, 203);
7733 processSync(mapper);
7734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7735 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7736 WithPressure(0.5f))));
7737
7738 processUp(mapper);
7739 processSync(mapper);
7740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7741 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7742 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7743
7744 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7746}
7747
7748TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7749 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7750 auto source = WithSource(EXPECTED_SOURCE);
7751
7752 mStylusState.pressure = 1.f;
7753 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_ERASER;
7754 processExternalStylusState(mapper);
7755 processDown(mapper, 100, 200);
7756 processSync(mapper);
7757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7758 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7759 WithToolType(AMOTION_EVENT_TOOL_TYPE_ERASER))));
7760 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7761
7762 // The external stylus reports a tool change. We wait for some time for a touch event.
7763 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7764 processExternalStylusState(mapper);
7765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7766 ASSERT_NO_FATAL_FAILURE(
7767 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7768
7769 // If a touch is reported within the timeout, it reports the updated pressure.
7770 processMove(mapper, 101, 201);
7771 processSync(mapper);
7772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7773 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7774 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7775 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7776
7777 // There is another tool type change.
7778 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
7779 processExternalStylusState(mapper);
7780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7781 ASSERT_NO_FATAL_FAILURE(
7782 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7783
7784 // If a touch is not reported within the timeout, a move event is generated to report
7785 // the new tool type.
7786 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7788 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7789 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7790
7791 processUp(mapper);
7792 processSync(mapper);
7793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7794 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
7795 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7796
7797 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7799}
7800
7801TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7802 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7803 auto toolTypeSource =
7804 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7805
7806 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7807
7808 // The external stylus reports a button change. We wait for some time for a touch event.
7809 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7810 processExternalStylusState(mapper);
7811 ASSERT_NO_FATAL_FAILURE(
7812 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7813
7814 // If a touch is reported within the timeout, it reports the updated button state.
7815 processMove(mapper, 101, 201);
7816 processSync(mapper);
7817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7818 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7819 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7821 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7822 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7823 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7824
7825 // The button is now released.
7826 mStylusState.buttons = 0;
7827 processExternalStylusState(mapper);
7828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7829 ASSERT_NO_FATAL_FAILURE(
7830 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7831
7832 // If a touch is not reported within the timeout, a move event is generated to report
7833 // the new button state.
7834 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7835 // TODO(prabirmsp): Fix fused stylus button releases being handled inconsistently.
7836 // The button release event should be sent here, but isn't.
7837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7838 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7839 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7840
7841 processUp(mapper);
7842 processSync(mapper);
7843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7844 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7845 WithButtonState(0))));
7846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7847 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7848
7849 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7851}
7852
Michael Wrightd02c5b62014-02-10 15:10:22 -08007853// --- MultiTouchInputMapperTest ---
7854
7855class MultiTouchInputMapperTest : public TouchInputMapperTest {
7856protected:
7857 void prepareAxes(int axes);
7858
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007859 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7860 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7861 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7862 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7863 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7864 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7865 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7866 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7867 void processId(MultiTouchInputMapper& mapper, int32_t id);
7868 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7869 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7870 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007871 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007872 void processMTSync(MultiTouchInputMapper& mapper);
7873 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007874};
7875
7876void MultiTouchInputMapperTest::prepareAxes(int axes) {
7877 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007878 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7879 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007880 }
7881 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007882 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7883 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007884 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007885 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7886 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007887 }
7888 }
7889 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007890 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7891 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007892 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007893 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007894 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007895 }
7896 }
7897 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007898 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7899 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007900 }
7901 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007902 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7903 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007904 }
7905 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007906 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7907 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007908 }
7909 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007910 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7911 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007912 }
7913 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007914 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7915 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007916 }
7917 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007918 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007919 }
7920}
7921
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007922void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7923 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007924 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7925 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007926}
7927
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007928void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7929 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007930 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007931}
7932
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007933void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7934 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007935 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007936}
7937
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007938void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007939 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007940}
7941
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007942void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007944}
7945
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007946void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7947 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007949}
7950
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007951void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007952 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007953}
7954
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007955void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007956 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007957}
7958
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007959void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007960 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007961}
7962
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007963void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007964 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007965}
7966
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007967void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007968 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007969}
7970
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007971void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7972 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007973 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007974}
7975
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007976void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7977 int32_t value) {
7978 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7979 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7980}
7981
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007982void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007983 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007984}
7985
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007986void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007987 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007988}
7989
Michael Wrightd02c5b62014-02-10 15:10:22 -08007990TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007991 addConfigurationProperty("touch.deviceType", "touchScreen");
7992 prepareDisplay(DISPLAY_ORIENTATION_0);
7993 prepareAxes(POSITION);
7994 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007995 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007996
arthurhungdcef2dc2020-08-11 14:47:50 +08007997 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007998
7999 NotifyMotionArgs motionArgs;
8000
8001 // Two fingers down at once.
8002 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8003 processPosition(mapper, x1, y1);
8004 processMTSync(mapper);
8005 processPosition(mapper, x2, y2);
8006 processMTSync(mapper);
8007 processSync(mapper);
8008
8009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8010 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8011 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8012 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8013 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8014 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8015 ASSERT_EQ(0, motionArgs.flags);
8016 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8017 ASSERT_EQ(0, motionArgs.buttonState);
8018 ASSERT_EQ(0, motionArgs.edgeFlags);
8019 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8020 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8021 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8023 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8024 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8025 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8026 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8027
8028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8029 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8030 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8031 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8032 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008033 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008034 ASSERT_EQ(0, motionArgs.flags);
8035 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8036 ASSERT_EQ(0, motionArgs.buttonState);
8037 ASSERT_EQ(0, motionArgs.edgeFlags);
8038 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8039 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8040 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8041 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8042 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8044 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8045 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8046 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8047 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8048 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8049 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8050
8051 // Move.
8052 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8053 processPosition(mapper, x1, y1);
8054 processMTSync(mapper);
8055 processPosition(mapper, x2, y2);
8056 processMTSync(mapper);
8057 processSync(mapper);
8058
8059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8060 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8061 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8062 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8063 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8065 ASSERT_EQ(0, motionArgs.flags);
8066 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8067 ASSERT_EQ(0, motionArgs.buttonState);
8068 ASSERT_EQ(0, motionArgs.edgeFlags);
8069 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8070 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8071 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8072 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8073 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8074 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8075 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8076 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8077 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8078 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8079 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8080 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8081
8082 // First finger up.
8083 x2 += 15; y2 -= 20;
8084 processPosition(mapper, x2, y2);
8085 processMTSync(mapper);
8086 processSync(mapper);
8087
8088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8089 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8090 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8091 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8092 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008093 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008094 ASSERT_EQ(0, motionArgs.flags);
8095 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8096 ASSERT_EQ(0, motionArgs.buttonState);
8097 ASSERT_EQ(0, motionArgs.edgeFlags);
8098 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8099 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8100 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8101 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8102 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8103 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8104 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8105 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8106 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8107 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8108 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8109 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8110
8111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8112 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8113 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8114 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8115 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8117 ASSERT_EQ(0, motionArgs.flags);
8118 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8119 ASSERT_EQ(0, motionArgs.buttonState);
8120 ASSERT_EQ(0, motionArgs.edgeFlags);
8121 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8122 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8124 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8125 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8126 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8127 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8128 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8129
8130 // Move.
8131 x2 += 20; y2 -= 25;
8132 processPosition(mapper, x2, y2);
8133 processMTSync(mapper);
8134 processSync(mapper);
8135
8136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8137 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8138 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8139 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8140 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8142 ASSERT_EQ(0, motionArgs.flags);
8143 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8144 ASSERT_EQ(0, motionArgs.buttonState);
8145 ASSERT_EQ(0, motionArgs.edgeFlags);
8146 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8147 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8148 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8149 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8150 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8151 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8152 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8153 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8154
8155 // New finger down.
8156 int32_t x3 = 700, y3 = 300;
8157 processPosition(mapper, x2, y2);
8158 processMTSync(mapper);
8159 processPosition(mapper, x3, y3);
8160 processMTSync(mapper);
8161 processSync(mapper);
8162
8163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8164 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8165 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8166 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8167 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008168 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008169 ASSERT_EQ(0, motionArgs.flags);
8170 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8171 ASSERT_EQ(0, motionArgs.buttonState);
8172 ASSERT_EQ(0, motionArgs.edgeFlags);
8173 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8174 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8175 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8176 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8177 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8178 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8179 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8181 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8182 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8183 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8184 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8185
8186 // Second finger up.
8187 x3 += 30; y3 -= 20;
8188 processPosition(mapper, x3, y3);
8189 processMTSync(mapper);
8190 processSync(mapper);
8191
8192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8193 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8194 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8195 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8196 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008197 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008198 ASSERT_EQ(0, motionArgs.flags);
8199 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8200 ASSERT_EQ(0, motionArgs.buttonState);
8201 ASSERT_EQ(0, motionArgs.edgeFlags);
8202 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8203 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8204 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8205 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8206 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8207 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8208 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8210 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8211 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8212 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8213 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8214
8215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8216 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8217 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8218 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8219 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8221 ASSERT_EQ(0, motionArgs.flags);
8222 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8223 ASSERT_EQ(0, motionArgs.buttonState);
8224 ASSERT_EQ(0, motionArgs.edgeFlags);
8225 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8226 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8227 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8229 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8230 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8231 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8232 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8233
8234 // Last finger up.
8235 processMTSync(mapper);
8236 processSync(mapper);
8237
8238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8239 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8240 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8241 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8242 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8243 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8244 ASSERT_EQ(0, motionArgs.flags);
8245 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8246 ASSERT_EQ(0, motionArgs.buttonState);
8247 ASSERT_EQ(0, motionArgs.edgeFlags);
8248 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8249 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8250 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8251 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8252 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8253 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8254 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8255 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8256
8257 // Should not have sent any more keys or motions.
8258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8260}
8261
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008262TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8263 addConfigurationProperty("touch.deviceType", "touchScreen");
8264 prepareDisplay(DISPLAY_ORIENTATION_0);
8265
8266 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8267 /*fuzz*/ 0, /*resolution*/ 10);
8268 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8269 /*fuzz*/ 0, /*resolution*/ 11);
8270 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8271 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8272 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8273 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8274 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8275 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8276 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8277 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8278
8279 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8280
8281 // X and Y axes
8282 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8283 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8284 // Touch major and minor
8285 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8286 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8287 // Tool major and minor
8288 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8289 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8290}
8291
8292TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8293 addConfigurationProperty("touch.deviceType", "touchScreen");
8294 prepareDisplay(DISPLAY_ORIENTATION_0);
8295
8296 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8297 /*fuzz*/ 0, /*resolution*/ 10);
8298 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8299 /*fuzz*/ 0, /*resolution*/ 11);
8300
8301 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8302
8303 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8304
8305 // Touch major and minor
8306 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8307 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8308 // Tool major and minor
8309 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8310 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8311}
8312
Michael Wrightd02c5b62014-02-10 15:10:22 -08008313TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008314 addConfigurationProperty("touch.deviceType", "touchScreen");
8315 prepareDisplay(DISPLAY_ORIENTATION_0);
8316 prepareAxes(POSITION | ID);
8317 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008318 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008319
arthurhungdcef2dc2020-08-11 14:47:50 +08008320 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008321
8322 NotifyMotionArgs motionArgs;
8323
8324 // Two fingers down at once.
8325 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8326 processPosition(mapper, x1, y1);
8327 processId(mapper, 1);
8328 processMTSync(mapper);
8329 processPosition(mapper, x2, y2);
8330 processId(mapper, 2);
8331 processMTSync(mapper);
8332 processSync(mapper);
8333
8334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8335 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8336 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8337 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8338 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8340 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8341
8342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008343 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008344 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8345 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8346 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8347 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8348 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8349 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8350 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8352 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8353
8354 // Move.
8355 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8356 processPosition(mapper, x1, y1);
8357 processId(mapper, 1);
8358 processMTSync(mapper);
8359 processPosition(mapper, x2, y2);
8360 processId(mapper, 2);
8361 processMTSync(mapper);
8362 processSync(mapper);
8363
8364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8365 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8366 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8367 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8368 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8369 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8370 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8372 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8374 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8375
8376 // First finger up.
8377 x2 += 15; y2 -= 20;
8378 processPosition(mapper, x2, y2);
8379 processId(mapper, 2);
8380 processMTSync(mapper);
8381 processSync(mapper);
8382
8383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008384 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008385 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8386 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8388 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8389 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8391 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8393 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8394
8395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8396 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8397 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8398 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8399 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8401 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8402
8403 // Move.
8404 x2 += 20; y2 -= 25;
8405 processPosition(mapper, x2, y2);
8406 processId(mapper, 2);
8407 processMTSync(mapper);
8408 processSync(mapper);
8409
8410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8412 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8413 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8414 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8416 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8417
8418 // New finger down.
8419 int32_t x3 = 700, y3 = 300;
8420 processPosition(mapper, x2, y2);
8421 processId(mapper, 2);
8422 processMTSync(mapper);
8423 processPosition(mapper, x3, y3);
8424 processId(mapper, 3);
8425 processMTSync(mapper);
8426 processSync(mapper);
8427
8428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
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(size_t(2), motionArgs.pointerCount);
8431 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8432 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8433 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8434 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8436 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8437 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8438 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8439
8440 // Second finger up.
8441 x3 += 30; y3 -= 20;
8442 processPosition(mapper, x3, y3);
8443 processId(mapper, 3);
8444 processMTSync(mapper);
8445 processSync(mapper);
8446
8447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008448 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008449 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8450 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8451 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8452 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8453 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8455 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8457 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8458
8459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8461 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8462 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8463 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8465 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8466
8467 // Last finger up.
8468 processMTSync(mapper);
8469 processSync(mapper);
8470
8471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8472 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8473 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8474 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8475 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8477 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8478
8479 // Should not have sent any more keys or motions.
8480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8482}
8483
8484TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008485 addConfigurationProperty("touch.deviceType", "touchScreen");
8486 prepareDisplay(DISPLAY_ORIENTATION_0);
8487 prepareAxes(POSITION | ID | SLOT);
8488 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008489 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008490
arthurhungdcef2dc2020-08-11 14:47:50 +08008491 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008492
8493 NotifyMotionArgs motionArgs;
8494
8495 // Two fingers down at once.
8496 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8497 processPosition(mapper, x1, y1);
8498 processId(mapper, 1);
8499 processSlot(mapper, 1);
8500 processPosition(mapper, x2, y2);
8501 processId(mapper, 2);
8502 processSync(mapper);
8503
8504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8505 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8506 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8507 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8508 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8510 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8511
8512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008513 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008514 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8515 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8516 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8517 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8518 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8520 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8522 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8523
8524 // Move.
8525 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8526 processSlot(mapper, 0);
8527 processPosition(mapper, x1, y1);
8528 processSlot(mapper, 1);
8529 processPosition(mapper, x2, y2);
8530 processSync(mapper);
8531
8532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8533 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8534 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8535 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8536 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8537 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8538 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8540 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8542 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8543
8544 // First finger up.
8545 x2 += 15; y2 -= 20;
8546 processSlot(mapper, 0);
8547 processId(mapper, -1);
8548 processSlot(mapper, 1);
8549 processPosition(mapper, x2, y2);
8550 processSync(mapper);
8551
8552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008553 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008554 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8555 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8556 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8557 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8558 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8560 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8562 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8563
8564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8566 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8567 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8568 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8570 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8571
8572 // Move.
8573 x2 += 20; y2 -= 25;
8574 processPosition(mapper, x2, y2);
8575 processSync(mapper);
8576
8577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8578 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8579 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8580 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8581 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8583 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8584
8585 // New finger down.
8586 int32_t x3 = 700, y3 = 300;
8587 processPosition(mapper, x2, y2);
8588 processSlot(mapper, 0);
8589 processId(mapper, 3);
8590 processPosition(mapper, x3, y3);
8591 processSync(mapper);
8592
8593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008594 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008595 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8596 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8597 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8598 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8599 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8601 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8603 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8604
8605 // Second finger up.
8606 x3 += 30; y3 -= 20;
8607 processSlot(mapper, 1);
8608 processId(mapper, -1);
8609 processSlot(mapper, 0);
8610 processPosition(mapper, x3, y3);
8611 processSync(mapper);
8612
8613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008614 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008615 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8616 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8617 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8618 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8619 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8621 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8623 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8624
8625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8627 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8628 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8631 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8632
8633 // Last finger up.
8634 processId(mapper, -1);
8635 processSync(mapper);
8636
8637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8638 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8639 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8640 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8641 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8642 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8643 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8644
8645 // Should not have sent any more keys or motions.
8646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8648}
8649
8650TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008651 addConfigurationProperty("touch.deviceType", "touchScreen");
8652 prepareDisplay(DISPLAY_ORIENTATION_0);
8653 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008654 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008655
8656 // These calculations are based on the input device calibration documentation.
8657 int32_t rawX = 100;
8658 int32_t rawY = 200;
8659 int32_t rawTouchMajor = 7;
8660 int32_t rawTouchMinor = 6;
8661 int32_t rawToolMajor = 9;
8662 int32_t rawToolMinor = 8;
8663 int32_t rawPressure = 11;
8664 int32_t rawDistance = 0;
8665 int32_t rawOrientation = 3;
8666 int32_t id = 5;
8667
8668 float x = toDisplayX(rawX);
8669 float y = toDisplayY(rawY);
8670 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8671 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8672 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8673 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8674 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8675 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8676 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8677 float distance = float(rawDistance);
8678
8679 processPosition(mapper, rawX, rawY);
8680 processTouchMajor(mapper, rawTouchMajor);
8681 processTouchMinor(mapper, rawTouchMinor);
8682 processToolMajor(mapper, rawToolMajor);
8683 processToolMinor(mapper, rawToolMinor);
8684 processPressure(mapper, rawPressure);
8685 processOrientation(mapper, rawOrientation);
8686 processDistance(mapper, rawDistance);
8687 processId(mapper, id);
8688 processMTSync(mapper);
8689 processSync(mapper);
8690
8691 NotifyMotionArgs args;
8692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8693 ASSERT_EQ(0, args.pointerProperties[0].id);
8694 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8695 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8696 orientation, distance));
8697}
8698
8699TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008700 addConfigurationProperty("touch.deviceType", "touchScreen");
8701 prepareDisplay(DISPLAY_ORIENTATION_0);
8702 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8703 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008704 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008705
8706 // These calculations are based on the input device calibration documentation.
8707 int32_t rawX = 100;
8708 int32_t rawY = 200;
8709 int32_t rawTouchMajor = 140;
8710 int32_t rawTouchMinor = 120;
8711 int32_t rawToolMajor = 180;
8712 int32_t rawToolMinor = 160;
8713
8714 float x = toDisplayX(rawX);
8715 float y = toDisplayY(rawY);
8716 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8717 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8718 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8719 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8720 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8721
8722 processPosition(mapper, rawX, rawY);
8723 processTouchMajor(mapper, rawTouchMajor);
8724 processTouchMinor(mapper, rawTouchMinor);
8725 processToolMajor(mapper, rawToolMajor);
8726 processToolMinor(mapper, rawToolMinor);
8727 processMTSync(mapper);
8728 processSync(mapper);
8729
8730 NotifyMotionArgs args;
8731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8733 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8734}
8735
8736TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008737 addConfigurationProperty("touch.deviceType", "touchScreen");
8738 prepareDisplay(DISPLAY_ORIENTATION_0);
8739 prepareAxes(POSITION | TOUCH | TOOL);
8740 addConfigurationProperty("touch.size.calibration", "diameter");
8741 addConfigurationProperty("touch.size.scale", "10");
8742 addConfigurationProperty("touch.size.bias", "160");
8743 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008744 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008745
8746 // These calculations are based on the input device calibration documentation.
8747 // Note: We only provide a single common touch/tool value because the device is assumed
8748 // not to emit separate values for each pointer (isSummed = 1).
8749 int32_t rawX = 100;
8750 int32_t rawY = 200;
8751 int32_t rawX2 = 150;
8752 int32_t rawY2 = 250;
8753 int32_t rawTouchMajor = 5;
8754 int32_t rawToolMajor = 8;
8755
8756 float x = toDisplayX(rawX);
8757 float y = toDisplayY(rawY);
8758 float x2 = toDisplayX(rawX2);
8759 float y2 = toDisplayY(rawY2);
8760 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8761 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8762 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8763
8764 processPosition(mapper, rawX, rawY);
8765 processTouchMajor(mapper, rawTouchMajor);
8766 processToolMajor(mapper, rawToolMajor);
8767 processMTSync(mapper);
8768 processPosition(mapper, rawX2, rawY2);
8769 processTouchMajor(mapper, rawTouchMajor);
8770 processToolMajor(mapper, rawToolMajor);
8771 processMTSync(mapper);
8772 processSync(mapper);
8773
8774 NotifyMotionArgs args;
8775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8776 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8777
8778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008779 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008780 ASSERT_EQ(size_t(2), args.pointerCount);
8781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8782 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8784 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8785}
8786
8787TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008788 addConfigurationProperty("touch.deviceType", "touchScreen");
8789 prepareDisplay(DISPLAY_ORIENTATION_0);
8790 prepareAxes(POSITION | TOUCH | TOOL);
8791 addConfigurationProperty("touch.size.calibration", "area");
8792 addConfigurationProperty("touch.size.scale", "43");
8793 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008794 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008795
8796 // These calculations are based on the input device calibration documentation.
8797 int32_t rawX = 100;
8798 int32_t rawY = 200;
8799 int32_t rawTouchMajor = 5;
8800 int32_t rawToolMajor = 8;
8801
8802 float x = toDisplayX(rawX);
8803 float y = toDisplayY(rawY);
8804 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8805 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8806 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8807
8808 processPosition(mapper, rawX, rawY);
8809 processTouchMajor(mapper, rawTouchMajor);
8810 processToolMajor(mapper, rawToolMajor);
8811 processMTSync(mapper);
8812 processSync(mapper);
8813
8814 NotifyMotionArgs args;
8815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8817 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8818}
8819
8820TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008821 addConfigurationProperty("touch.deviceType", "touchScreen");
8822 prepareDisplay(DISPLAY_ORIENTATION_0);
8823 prepareAxes(POSITION | PRESSURE);
8824 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8825 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008826 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008827
Michael Wrightaa449c92017-12-13 21:21:43 +00008828 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008829 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008830 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8831 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8832 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8833
Michael Wrightd02c5b62014-02-10 15:10:22 -08008834 // These calculations are based on the input device calibration documentation.
8835 int32_t rawX = 100;
8836 int32_t rawY = 200;
8837 int32_t rawPressure = 60;
8838
8839 float x = toDisplayX(rawX);
8840 float y = toDisplayY(rawY);
8841 float pressure = float(rawPressure) * 0.01f;
8842
8843 processPosition(mapper, rawX, rawY);
8844 processPressure(mapper, rawPressure);
8845 processMTSync(mapper);
8846 processSync(mapper);
8847
8848 NotifyMotionArgs args;
8849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8851 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8852}
8853
8854TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008855 addConfigurationProperty("touch.deviceType", "touchScreen");
8856 prepareDisplay(DISPLAY_ORIENTATION_0);
8857 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008858 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008859
8860 NotifyMotionArgs motionArgs;
8861 NotifyKeyArgs keyArgs;
8862
8863 processId(mapper, 1);
8864 processPosition(mapper, 100, 200);
8865 processSync(mapper);
8866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8867 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8868 ASSERT_EQ(0, motionArgs.buttonState);
8869
8870 // press BTN_LEFT, release BTN_LEFT
8871 processKey(mapper, BTN_LEFT, 1);
8872 processSync(mapper);
8873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8875 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8876
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8878 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8879 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8880
Michael Wrightd02c5b62014-02-10 15:10:22 -08008881 processKey(mapper, BTN_LEFT, 0);
8882 processSync(mapper);
8883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008884 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008885 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008886
8887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008888 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008889 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008890
8891 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8892 processKey(mapper, BTN_RIGHT, 1);
8893 processKey(mapper, BTN_MIDDLE, 1);
8894 processSync(mapper);
8895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8897 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8898 motionArgs.buttonState);
8899
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8901 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8902 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8903
8904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8905 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8906 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8907 motionArgs.buttonState);
8908
Michael Wrightd02c5b62014-02-10 15:10:22 -08008909 processKey(mapper, BTN_RIGHT, 0);
8910 processSync(mapper);
8911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008912 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008913 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008914
8915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008916 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008917 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008918
8919 processKey(mapper, BTN_MIDDLE, 0);
8920 processSync(mapper);
8921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008922 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008923 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008924
8925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008926 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008927 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008928
8929 // press BTN_BACK, release BTN_BACK
8930 processKey(mapper, BTN_BACK, 1);
8931 processSync(mapper);
8932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8933 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8934 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008935
Michael Wrightd02c5b62014-02-10 15:10:22 -08008936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008937 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008938 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8939
8940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8941 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8942 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008943
8944 processKey(mapper, BTN_BACK, 0);
8945 processSync(mapper);
8946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008947 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008948 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008949
8950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008952 ASSERT_EQ(0, motionArgs.buttonState);
8953
Michael Wrightd02c5b62014-02-10 15:10:22 -08008954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8955 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8956 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8957
8958 // press BTN_SIDE, release BTN_SIDE
8959 processKey(mapper, BTN_SIDE, 1);
8960 processSync(mapper);
8961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8962 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8963 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008964
Michael Wrightd02c5b62014-02-10 15:10:22 -08008965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008967 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8968
8969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8970 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8971 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008972
8973 processKey(mapper, BTN_SIDE, 0);
8974 processSync(mapper);
8975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008976 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008977 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008978
8979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008980 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008981 ASSERT_EQ(0, motionArgs.buttonState);
8982
Michael Wrightd02c5b62014-02-10 15:10:22 -08008983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8984 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8985 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8986
8987 // press BTN_FORWARD, release BTN_FORWARD
8988 processKey(mapper, BTN_FORWARD, 1);
8989 processSync(mapper);
8990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8991 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8992 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008993
Michael Wrightd02c5b62014-02-10 15:10:22 -08008994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008995 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008996 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8997
8998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8999 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9000 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009001
9002 processKey(mapper, BTN_FORWARD, 0);
9003 processSync(mapper);
9004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009005 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009006 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009007
9008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009009 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009010 ASSERT_EQ(0, motionArgs.buttonState);
9011
Michael Wrightd02c5b62014-02-10 15:10:22 -08009012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9013 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9014 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9015
9016 // press BTN_EXTRA, release BTN_EXTRA
9017 processKey(mapper, BTN_EXTRA, 1);
9018 processSync(mapper);
9019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9020 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9021 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009022
Michael Wrightd02c5b62014-02-10 15:10:22 -08009023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009025 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9026
9027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9028 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9029 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009030
9031 processKey(mapper, BTN_EXTRA, 0);
9032 processSync(mapper);
9033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009034 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009035 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009036
9037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009039 ASSERT_EQ(0, motionArgs.buttonState);
9040
Michael Wrightd02c5b62014-02-10 15:10:22 -08009041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9042 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9043 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9044
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9046
Michael Wrightd02c5b62014-02-10 15:10:22 -08009047 // press BTN_STYLUS, release BTN_STYLUS
9048 processKey(mapper, BTN_STYLUS, 1);
9049 processSync(mapper);
9050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009052 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
9053
9054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9055 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9056 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009057
9058 processKey(mapper, BTN_STYLUS, 0);
9059 processSync(mapper);
9060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009061 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009062 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009063
9064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009065 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009066 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009067
9068 // press BTN_STYLUS2, release BTN_STYLUS2
9069 processKey(mapper, BTN_STYLUS2, 1);
9070 processSync(mapper);
9071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009073 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
9074
9075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9076 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9077 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009078
9079 processKey(mapper, BTN_STYLUS2, 0);
9080 processSync(mapper);
9081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009082 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009083 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009084
9085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009087 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009088
9089 // release touch
9090 processId(mapper, -1);
9091 processSync(mapper);
9092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9093 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9094 ASSERT_EQ(0, motionArgs.buttonState);
9095}
9096
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009097TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
9098 addConfigurationProperty("touch.deviceType", "touchScreen");
9099 prepareDisplay(DISPLAY_ORIENTATION_0);
9100 prepareAxes(POSITION | ID | SLOT);
9101 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9102
9103 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
9104 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
9105
9106 // Touch down.
9107 processId(mapper, 1);
9108 processPosition(mapper, 100, 200);
9109 processSync(mapper);
9110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9111 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
9112
9113 // Press and release button mapped to the primary stylus button.
9114 processKey(mapper, BTN_A, 1);
9115 processSync(mapper);
9116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9117 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9118 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9120 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9121 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9122
9123 processKey(mapper, BTN_A, 0);
9124 processSync(mapper);
9125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9126 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9128 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9129
9130 // Press and release the HID usage mapped to the secondary stylus button.
9131 processHidUsage(mapper, 0xabcd, 1);
9132 processSync(mapper);
9133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9134 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9135 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9137 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9138 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9139
9140 processHidUsage(mapper, 0xabcd, 0);
9141 processSync(mapper);
9142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9143 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9145 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9146
9147 // Release touch.
9148 processId(mapper, -1);
9149 processSync(mapper);
9150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9151 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
9152}
9153
Michael Wrightd02c5b62014-02-10 15:10:22 -08009154TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009155 addConfigurationProperty("touch.deviceType", "touchScreen");
9156 prepareDisplay(DISPLAY_ORIENTATION_0);
9157 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009158 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009159
9160 NotifyMotionArgs motionArgs;
9161
9162 // default tool type is finger
9163 processId(mapper, 1);
9164 processPosition(mapper, 100, 200);
9165 processSync(mapper);
9166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9167 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9168 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9169
9170 // eraser
9171 processKey(mapper, BTN_TOOL_RUBBER, 1);
9172 processSync(mapper);
9173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9175 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
9176
9177 // stylus
9178 processKey(mapper, BTN_TOOL_RUBBER, 0);
9179 processKey(mapper, BTN_TOOL_PEN, 1);
9180 processSync(mapper);
9181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9183 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9184
9185 // brush
9186 processKey(mapper, BTN_TOOL_PEN, 0);
9187 processKey(mapper, BTN_TOOL_BRUSH, 1);
9188 processSync(mapper);
9189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9192
9193 // pencil
9194 processKey(mapper, BTN_TOOL_BRUSH, 0);
9195 processKey(mapper, BTN_TOOL_PENCIL, 1);
9196 processSync(mapper);
9197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9199 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9200
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009201 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009202 processKey(mapper, BTN_TOOL_PENCIL, 0);
9203 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
9204 processSync(mapper);
9205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9206 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9207 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9208
9209 // mouse
9210 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9211 processKey(mapper, BTN_TOOL_MOUSE, 1);
9212 processSync(mapper);
9213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9215 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
9216
9217 // lens
9218 processKey(mapper, BTN_TOOL_MOUSE, 0);
9219 processKey(mapper, BTN_TOOL_LENS, 1);
9220 processSync(mapper);
9221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9223 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
9224
9225 // double-tap
9226 processKey(mapper, BTN_TOOL_LENS, 0);
9227 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
9228 processSync(mapper);
9229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9230 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9231 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9232
9233 // triple-tap
9234 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9235 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
9236 processSync(mapper);
9237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9238 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9239 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9240
9241 // quad-tap
9242 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9243 processKey(mapper, BTN_TOOL_QUADTAP, 1);
9244 processSync(mapper);
9245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9247 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9248
9249 // finger
9250 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9251 processKey(mapper, BTN_TOOL_FINGER, 1);
9252 processSync(mapper);
9253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9254 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9255 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9256
9257 // stylus trumps finger
9258 processKey(mapper, BTN_TOOL_PEN, 1);
9259 processSync(mapper);
9260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9262 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9263
9264 // eraser trumps stylus
9265 processKey(mapper, BTN_TOOL_RUBBER, 1);
9266 processSync(mapper);
9267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9269 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
9270
9271 // mouse trumps eraser
9272 processKey(mapper, BTN_TOOL_MOUSE, 1);
9273 processSync(mapper);
9274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9276 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
9277
9278 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9279 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9280 processSync(mapper);
9281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9284
9285 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9286 processToolType(mapper, MT_TOOL_PEN);
9287 processSync(mapper);
9288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9290 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9291
9292 // back to default tool type
9293 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9294 processKey(mapper, BTN_TOOL_MOUSE, 0);
9295 processKey(mapper, BTN_TOOL_RUBBER, 0);
9296 processKey(mapper, BTN_TOOL_PEN, 0);
9297 processKey(mapper, BTN_TOOL_FINGER, 0);
9298 processSync(mapper);
9299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9301 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9302}
9303
9304TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009305 addConfigurationProperty("touch.deviceType", "touchScreen");
9306 prepareDisplay(DISPLAY_ORIENTATION_0);
9307 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009308 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009309 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009310
9311 NotifyMotionArgs motionArgs;
9312
9313 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9314 processId(mapper, 1);
9315 processPosition(mapper, 100, 200);
9316 processSync(mapper);
9317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9318 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9319 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9320 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9321
9322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9323 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9324 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9325 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9326
9327 // move a little
9328 processPosition(mapper, 150, 250);
9329 processSync(mapper);
9330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9331 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9333 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9334
9335 // down when BTN_TOUCH is pressed, pressure defaults to 1
9336 processKey(mapper, BTN_TOUCH, 1);
9337 processSync(mapper);
9338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9339 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9341 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9342
9343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9344 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9346 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9347
9348 // up when BTN_TOUCH is released, hover restored
9349 processKey(mapper, BTN_TOUCH, 0);
9350 processSync(mapper);
9351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9352 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9354 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9355
9356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9357 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9358 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9359 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9360
9361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9362 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9364 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9365
9366 // exit hover when pointer goes away
9367 processId(mapper, -1);
9368 processSync(mapper);
9369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9370 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9372 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9373}
9374
9375TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009376 addConfigurationProperty("touch.deviceType", "touchScreen");
9377 prepareDisplay(DISPLAY_ORIENTATION_0);
9378 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009379 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009380
9381 NotifyMotionArgs motionArgs;
9382
9383 // initially hovering because pressure is 0
9384 processId(mapper, 1);
9385 processPosition(mapper, 100, 200);
9386 processPressure(mapper, 0);
9387 processSync(mapper);
9388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9389 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9391 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9392
9393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9394 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9395 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9396 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9397
9398 // move a little
9399 processPosition(mapper, 150, 250);
9400 processSync(mapper);
9401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9402 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9403 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9404 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9405
9406 // down when pressure becomes non-zero
9407 processPressure(mapper, RAW_PRESSURE_MAX);
9408 processSync(mapper);
9409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9410 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9412 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9413
9414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9415 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9417 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9418
9419 // up when pressure becomes 0, hover restored
9420 processPressure(mapper, 0);
9421 processSync(mapper);
9422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9423 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9425 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9426
9427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9428 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9429 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9430 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9431
9432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9433 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9435 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9436
9437 // exit hover when pointer goes away
9438 processId(mapper, -1);
9439 processSync(mapper);
9440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9441 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9443 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9444}
9445
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009446/**
9447 * Set the input device port <--> display port associations, and check that the
9448 * events are routed to the display that matches the display port.
9449 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9450 */
9451TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009452 const std::string usb2 = "USB2";
9453 const uint8_t hdmi1 = 0;
9454 const uint8_t hdmi2 = 1;
9455 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009456 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009457
9458 addConfigurationProperty("touch.deviceType", "touchScreen");
9459 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009460 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009461
9462 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9463 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9464
9465 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9466 // for this input device is specified, and the matching viewport is not present,
9467 // the input device should be disabled (at the mapper level).
9468
9469 // Add viewport for display 2 on hdmi2
9470 prepareSecondaryDisplay(type, hdmi2);
9471 // Send a touch event
9472 processPosition(mapper, 100, 100);
9473 processSync(mapper);
9474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9475
9476 // Add viewport for display 1 on hdmi1
9477 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
9478 // Send a touch event again
9479 processPosition(mapper, 100, 100);
9480 processSync(mapper);
9481
9482 NotifyMotionArgs args;
9483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9484 ASSERT_EQ(DISPLAY_ID, args.displayId);
9485}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009486
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009487TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9488 addConfigurationProperty("touch.deviceType", "touchScreen");
9489 prepareAxes(POSITION);
9490 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9491
9492 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9493
9494 prepareDisplay(DISPLAY_ORIENTATION_0);
9495 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9496
9497 // Send a touch event
9498 processPosition(mapper, 100, 100);
9499 processSync(mapper);
9500
9501 NotifyMotionArgs args;
9502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9503 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9504}
9505
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009506TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009507 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009508 std::shared_ptr<FakePointerController> fakePointerController =
9509 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009510 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009511 fakePointerController->setPosition(100, 200);
9512 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009513 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009514
Garfield Tan888a6a42020-01-09 11:39:16 -08009515 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009516 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009517
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009518 prepareDisplay(DISPLAY_ORIENTATION_0);
9519 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009520 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009521
Harry Cutts16a24cc2022-10-26 15:22:19 +00009522 // Check source is a touchpad that would obtain the PointerController.
9523 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009524
9525 NotifyMotionArgs motionArgs;
9526 processPosition(mapper, 100, 100);
9527 processSync(mapper);
9528
9529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9530 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9531 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9532}
9533
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009534/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009535 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9536 */
9537TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9538 addConfigurationProperty("touch.deviceType", "touchScreen");
9539 prepareAxes(POSITION);
9540 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9541
9542 prepareDisplay(DISPLAY_ORIENTATION_0);
9543 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
9544 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
9545 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
9546 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9547
9548 NotifyMotionArgs args;
9549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9550 ASSERT_EQ(26, args.readTime);
9551
9552 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
9553 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
9554 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9555
9556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9557 ASSERT_EQ(33, args.readTime);
9558}
9559
9560/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009561 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9562 * events should not be delivered to the listener.
9563 */
9564TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9565 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009566 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009567 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9568 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
9569 ViewportType::INTERNAL);
9570 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9571 prepareAxes(POSITION);
9572 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9573
9574 NotifyMotionArgs motionArgs;
9575 processPosition(mapper, 100, 100);
9576 processSync(mapper);
9577
9578 mFakeListener->assertNotifyMotionWasNotCalled();
9579}
9580
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009581/**
9582 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9583 * the touch mapper can process the events and the events can be delivered to the listener.
9584 */
9585TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9586 addConfigurationProperty("touch.deviceType", "touchScreen");
9587 addConfigurationProperty("touch.enableForInactiveViewport", "1");
9588 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9589 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
9590 ViewportType::INTERNAL);
9591 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9592 prepareAxes(POSITION);
9593 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9594
9595 NotifyMotionArgs motionArgs;
9596 processPosition(mapper, 100, 100);
9597 processSync(mapper);
9598
9599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9600 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9601}
9602
Garfield Tanc734e4f2021-01-15 20:01:39 -08009603TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9604 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009605 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Garfield Tanc734e4f2021-01-15 20:01:39 -08009606 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9607 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
9608 ViewportType::INTERNAL);
9609 std::optional<DisplayViewport> optionalDisplayViewport =
9610 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9611 ASSERT_TRUE(optionalDisplayViewport.has_value());
9612 DisplayViewport displayViewport = *optionalDisplayViewport;
9613
9614 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9615 prepareAxes(POSITION);
9616 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9617
9618 // Finger down
9619 int32_t x = 100, y = 100;
9620 processPosition(mapper, x, y);
9621 processSync(mapper);
9622
9623 NotifyMotionArgs motionArgs;
9624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9625 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9626
9627 // Deactivate display viewport
9628 displayViewport.isActive = false;
9629 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9630 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9631
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009632 // The ongoing touch should be canceled immediately
9633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9634 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9635
9636 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009637 x += 10, y += 10;
9638 processPosition(mapper, x, y);
9639 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009641
9642 // Reactivate display viewport
9643 displayViewport.isActive = true;
9644 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9645 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9646
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009647 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009648 x += 10, y += 10;
9649 processPosition(mapper, x, y);
9650 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9652 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009653}
9654
Arthur Hung7c645402019-01-25 17:45:42 +08009655TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9656 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009657 prepareAxes(POSITION | ID | SLOT);
9658 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009659 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009660
9661 // Create the second touch screen device, and enable multi fingers.
9662 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009663 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009664 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009665 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009666 std::shared_ptr<InputDevice> device2 =
9667 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009668 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009669
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009670 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9671 0 /*flat*/, 0 /*fuzz*/);
9672 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9673 0 /*flat*/, 0 /*fuzz*/);
9674 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
9675 0 /*flat*/, 0 /*fuzz*/);
9676 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
9677 0 /*flat*/, 0 /*fuzz*/);
9678 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
9679 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9680 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009681
9682 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009683 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009684 std::list<NotifyArgs> unused =
9685 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9686 0 /*changes*/);
9687 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009688
9689 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009690 std::shared_ptr<FakePointerController> fakePointerController =
9691 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009692 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009693
9694 // Setup policy for associated displays and show touches.
9695 const uint8_t hdmi1 = 0;
9696 const uint8_t hdmi2 = 1;
9697 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9698 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9699 mFakePolicy->setShowTouches(true);
9700
9701 // Create displays.
9702 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009703 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009704
9705 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009706 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9707 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9708 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009709
9710 // Two fingers down at default display.
9711 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9712 processPosition(mapper, x1, y1);
9713 processId(mapper, 1);
9714 processSlot(mapper, 1);
9715 processPosition(mapper, x2, y2);
9716 processId(mapper, 2);
9717 processSync(mapper);
9718
9719 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9720 fakePointerController->getSpots().find(DISPLAY_ID);
9721 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9722 ASSERT_EQ(size_t(2), iter->second.size());
9723
9724 // Two fingers down at second display.
9725 processPosition(mapper2, x1, y1);
9726 processId(mapper2, 1);
9727 processSlot(mapper2, 1);
9728 processPosition(mapper2, x2, y2);
9729 processId(mapper2, 2);
9730 processSync(mapper2);
9731
9732 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9733 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9734 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009735
9736 // Disable the show touches configuration and ensure the spots are cleared.
9737 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009738 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9739 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009740
9741 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009742}
9743
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009744TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009745 prepareAxes(POSITION);
9746 addConfigurationProperty("touch.deviceType", "touchScreen");
9747 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009748 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009749
9750 NotifyMotionArgs motionArgs;
9751 // Unrotated video frame
9752 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9753 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009754 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009755 processPosition(mapper, 100, 200);
9756 processSync(mapper);
9757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9758 ASSERT_EQ(frames, motionArgs.videoFrames);
9759
9760 // Subsequent touch events should not have any videoframes
9761 // This is implemented separately in FakeEventHub,
9762 // but that should match the behaviour of TouchVideoDevice.
9763 processPosition(mapper, 200, 200);
9764 processSync(mapper);
9765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9766 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9767}
9768
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009769TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009770 prepareAxes(POSITION);
9771 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009772 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009773 // Unrotated video frame
9774 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9775 NotifyMotionArgs motionArgs;
9776
9777 // Test all 4 orientations
9778 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009779 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9780 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9781 clearViewports();
9782 prepareDisplay(orientation);
9783 std::vector<TouchVideoFrame> frames{frame};
9784 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9785 processPosition(mapper, 100, 200);
9786 processSync(mapper);
9787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9788 ASSERT_EQ(frames, motionArgs.videoFrames);
9789 }
9790}
9791
9792TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9793 prepareAxes(POSITION);
9794 addConfigurationProperty("touch.deviceType", "touchScreen");
9795 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9796 // orientation-aware are affected by display rotation.
9797 addConfigurationProperty("touch.orientationAware", "0");
9798 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9799 // Unrotated video frame
9800 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9801 NotifyMotionArgs motionArgs;
9802
9803 // Test all 4 orientations
9804 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009805 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9806 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9807 clearViewports();
9808 prepareDisplay(orientation);
9809 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009810 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009811 processPosition(mapper, 100, 200);
9812 processSync(mapper);
9813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009814 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9815 // compared to the display. This is so that when the window transform (which contains the
9816 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9817 // window's coordinate space.
9818 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009819 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009820
9821 // Release finger.
9822 processSync(mapper);
9823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009824 }
9825}
9826
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009827TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009828 prepareAxes(POSITION);
9829 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009830 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009831 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9832 // so mix these.
9833 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9834 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9835 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9836 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9837 NotifyMotionArgs motionArgs;
9838
9839 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009840 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009841 processPosition(mapper, 100, 200);
9842 processSync(mapper);
9843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009844 ASSERT_EQ(frames, motionArgs.videoFrames);
9845}
9846
9847TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9848 prepareAxes(POSITION);
9849 addConfigurationProperty("touch.deviceType", "touchScreen");
9850 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9851 // orientation-aware are affected by display rotation.
9852 addConfigurationProperty("touch.orientationAware", "0");
9853 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9854 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9855 // so mix these.
9856 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9857 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9858 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9859 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9860 NotifyMotionArgs motionArgs;
9861
9862 prepareDisplay(DISPLAY_ORIENTATION_90);
9863 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9864 processPosition(mapper, 100, 200);
9865 processSync(mapper);
9866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9867 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9868 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9869 // compared to the display. This is so that when the window transform (which contains the
9870 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9871 // window's coordinate space.
9872 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
9873 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009874 ASSERT_EQ(frames, motionArgs.videoFrames);
9875}
9876
Arthur Hung9da14732019-09-02 16:16:58 +08009877/**
9878 * If we had defined port associations, but the viewport is not ready, the touch device would be
9879 * expected to be disabled, and it should be enabled after the viewport has found.
9880 */
9881TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009882 constexpr uint8_t hdmi2 = 1;
9883 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009884 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009885
9886 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9887
9888 addConfigurationProperty("touch.deviceType", "touchScreen");
9889 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009890 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009891
9892 ASSERT_EQ(mDevice->isEnabled(), false);
9893
9894 // Add display on hdmi2, the device should be enabled and can receive touch event.
9895 prepareSecondaryDisplay(type, hdmi2);
9896 ASSERT_EQ(mDevice->isEnabled(), true);
9897
9898 // Send a touch event.
9899 processPosition(mapper, 100, 100);
9900 processSync(mapper);
9901
9902 NotifyMotionArgs args;
9903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9904 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9905}
9906
Arthur Hung421eb1c2020-01-16 00:09:42 +08009907TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009908 addConfigurationProperty("touch.deviceType", "touchScreen");
9909 prepareDisplay(DISPLAY_ORIENTATION_0);
9910 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009911 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009912
9913 NotifyMotionArgs motionArgs;
9914
9915 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9916 // finger down
9917 processId(mapper, 1);
9918 processPosition(mapper, x1, y1);
9919 processSync(mapper);
9920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9921 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9922 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9923
9924 // finger move
9925 processId(mapper, 1);
9926 processPosition(mapper, x2, y2);
9927 processSync(mapper);
9928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9929 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9930 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9931
9932 // finger up.
9933 processId(mapper, -1);
9934 processSync(mapper);
9935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9936 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9937 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9938
9939 // new finger down
9940 processId(mapper, 1);
9941 processPosition(mapper, x3, y3);
9942 processSync(mapper);
9943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9944 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9945 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9946}
9947
9948/**
arthurhungcc7f9802020-04-30 17:55:40 +08009949 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9950 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009951 */
arthurhungcc7f9802020-04-30 17:55:40 +08009952TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009953 addConfigurationProperty("touch.deviceType", "touchScreen");
9954 prepareDisplay(DISPLAY_ORIENTATION_0);
9955 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009956 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009957
9958 NotifyMotionArgs motionArgs;
9959
9960 // default tool type is finger
9961 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009962 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009963 processPosition(mapper, x1, y1);
9964 processSync(mapper);
9965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9966 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9967 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9968
9969 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9970 processToolType(mapper, MT_TOOL_PALM);
9971 processSync(mapper);
9972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9973 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9974
9975 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009976 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009977 processPosition(mapper, x2, y2);
9978 processSync(mapper);
9979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9980
9981 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009982 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009983 processSync(mapper);
9984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9985
9986 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009987 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009988 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009989 processPosition(mapper, x3, y3);
9990 processSync(mapper);
9991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9992 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9993 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9994}
9995
arthurhungbf89a482020-04-17 17:37:55 +08009996/**
arthurhungcc7f9802020-04-30 17:55:40 +08009997 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9998 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009999 */
arthurhungcc7f9802020-04-30 17:55:40 +080010000TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +080010001 addConfigurationProperty("touch.deviceType", "touchScreen");
10002 prepareDisplay(DISPLAY_ORIENTATION_0);
10003 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
10004 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10005
10006 NotifyMotionArgs motionArgs;
10007
10008 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +080010009 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10010 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010011 processPosition(mapper, x1, y1);
10012 processSync(mapper);
10013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10014 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10015 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10016
10017 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +080010018 processSlot(mapper, SECOND_SLOT);
10019 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010020 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +080010021 processSync(mapper);
10022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010023 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010024 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
10025
10026 // If the tool type of the first finger changes to MT_TOOL_PALM,
10027 // we expect to receive ACTION_POINTER_UP with cancel flag.
10028 processSlot(mapper, FIRST_SLOT);
10029 processId(mapper, FIRST_TRACKING_ID);
10030 processToolType(mapper, MT_TOOL_PALM);
10031 processSync(mapper);
10032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010033 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010034 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10035
10036 // The following MOVE events of second finger should be processed.
10037 processSlot(mapper, SECOND_SLOT);
10038 processId(mapper, SECOND_TRACKING_ID);
10039 processPosition(mapper, x2 + 1, y2 + 1);
10040 processSync(mapper);
10041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10042 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10043 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10044
10045 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
10046 // it. Second finger receive move.
10047 processSlot(mapper, FIRST_SLOT);
10048 processId(mapper, INVALID_TRACKING_ID);
10049 processSync(mapper);
10050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10052 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10053
10054 // Second finger keeps moving.
10055 processSlot(mapper, SECOND_SLOT);
10056 processId(mapper, SECOND_TRACKING_ID);
10057 processPosition(mapper, x2 + 2, y2 + 2);
10058 processSync(mapper);
10059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10060 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10061 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10062
10063 // Second finger up.
10064 processId(mapper, INVALID_TRACKING_ID);
10065 processSync(mapper);
10066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10067 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10068 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10069}
10070
10071/**
10072 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
10073 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
10074 */
10075TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
10076 addConfigurationProperty("touch.deviceType", "touchScreen");
10077 prepareDisplay(DISPLAY_ORIENTATION_0);
10078 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
10079 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10080
10081 NotifyMotionArgs motionArgs;
10082
10083 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10084 // First finger down.
10085 processId(mapper, FIRST_TRACKING_ID);
10086 processPosition(mapper, x1, y1);
10087 processSync(mapper);
10088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10089 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10090 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10091
10092 // Second finger down.
10093 processSlot(mapper, SECOND_SLOT);
10094 processId(mapper, SECOND_TRACKING_ID);
10095 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +080010096 processSync(mapper);
10097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010098 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +080010099 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10100
arthurhungcc7f9802020-04-30 17:55:40 +080010101 // If the tool type of the first finger changes to MT_TOOL_PALM,
10102 // we expect to receive ACTION_POINTER_UP with cancel flag.
10103 processSlot(mapper, FIRST_SLOT);
10104 processId(mapper, FIRST_TRACKING_ID);
10105 processToolType(mapper, MT_TOOL_PALM);
10106 processSync(mapper);
10107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010108 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010109 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10110
10111 // Second finger keeps moving.
10112 processSlot(mapper, SECOND_SLOT);
10113 processId(mapper, SECOND_TRACKING_ID);
10114 processPosition(mapper, x2 + 1, y2 + 1);
10115 processSync(mapper);
10116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10118
10119 // second finger becomes palm, receive cancel due to only 1 finger is active.
10120 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010121 processToolType(mapper, MT_TOOL_PALM);
10122 processSync(mapper);
10123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10124 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10125
arthurhungcc7f9802020-04-30 17:55:40 +080010126 // third finger down.
10127 processSlot(mapper, THIRD_SLOT);
10128 processId(mapper, THIRD_TRACKING_ID);
10129 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +080010130 processPosition(mapper, x3, y3);
10131 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +080010132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10133 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10134 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010135 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10136
10137 // third finger move
10138 processId(mapper, THIRD_TRACKING_ID);
10139 processPosition(mapper, x3 + 1, y3 + 1);
10140 processSync(mapper);
10141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10142 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10143
10144 // first finger up, third finger receive move.
10145 processSlot(mapper, FIRST_SLOT);
10146 processId(mapper, INVALID_TRACKING_ID);
10147 processSync(mapper);
10148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10150 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10151
10152 // second finger up, third finger receive move.
10153 processSlot(mapper, SECOND_SLOT);
10154 processId(mapper, INVALID_TRACKING_ID);
10155 processSync(mapper);
10156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10157 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10158 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10159
10160 // third finger up.
10161 processSlot(mapper, THIRD_SLOT);
10162 processId(mapper, INVALID_TRACKING_ID);
10163 processSync(mapper);
10164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10165 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10166 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10167}
10168
10169/**
10170 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10171 * and the active finger could still be allowed to receive the events
10172 */
10173TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
10174 addConfigurationProperty("touch.deviceType", "touchScreen");
10175 prepareDisplay(DISPLAY_ORIENTATION_0);
10176 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
10177 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10178
10179 NotifyMotionArgs motionArgs;
10180
10181 // default tool type is finger
10182 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10183 processId(mapper, FIRST_TRACKING_ID);
10184 processPosition(mapper, x1, y1);
10185 processSync(mapper);
10186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10187 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10188 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10189
10190 // Second finger down.
10191 processSlot(mapper, SECOND_SLOT);
10192 processId(mapper, SECOND_TRACKING_ID);
10193 processPosition(mapper, x2, y2);
10194 processSync(mapper);
10195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010196 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010197 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10198
10199 // If the tool type of the second finger changes to MT_TOOL_PALM,
10200 // we expect to receive ACTION_POINTER_UP with cancel flag.
10201 processId(mapper, SECOND_TRACKING_ID);
10202 processToolType(mapper, MT_TOOL_PALM);
10203 processSync(mapper);
10204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010205 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010206 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10207
10208 // The following MOVE event should be processed.
10209 processSlot(mapper, FIRST_SLOT);
10210 processId(mapper, FIRST_TRACKING_ID);
10211 processPosition(mapper, x1 + 1, y1 + 1);
10212 processSync(mapper);
10213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10215 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10216
10217 // second finger up.
10218 processSlot(mapper, SECOND_SLOT);
10219 processId(mapper, INVALID_TRACKING_ID);
10220 processSync(mapper);
10221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10223
10224 // first finger keep moving
10225 processSlot(mapper, FIRST_SLOT);
10226 processId(mapper, FIRST_TRACKING_ID);
10227 processPosition(mapper, x1 + 2, y1 + 2);
10228 processSync(mapper);
10229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10230 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10231
10232 // first finger up.
10233 processId(mapper, INVALID_TRACKING_ID);
10234 processSync(mapper);
10235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10236 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10237 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010238}
10239
Arthur Hung9ad18942021-06-19 02:04:46 +000010240/**
10241 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10242 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10243 * cause slot be valid again.
10244 */
10245TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10246 addConfigurationProperty("touch.deviceType", "touchScreen");
10247 prepareDisplay(DISPLAY_ORIENTATION_0);
10248 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10249 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10250
10251 NotifyMotionArgs motionArgs;
10252
10253 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10254 // First finger down.
10255 processId(mapper, FIRST_TRACKING_ID);
10256 processPosition(mapper, x1, y1);
10257 processPressure(mapper, RAW_PRESSURE_MAX);
10258 processSync(mapper);
10259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10260 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10261 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10262
10263 // First finger move.
10264 processId(mapper, FIRST_TRACKING_ID);
10265 processPosition(mapper, x1 + 1, y1 + 1);
10266 processPressure(mapper, RAW_PRESSURE_MAX);
10267 processSync(mapper);
10268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10270 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10271
10272 // Second finger down.
10273 processSlot(mapper, SECOND_SLOT);
10274 processId(mapper, SECOND_TRACKING_ID);
10275 processPosition(mapper, x2, y2);
10276 processPressure(mapper, RAW_PRESSURE_MAX);
10277 processSync(mapper);
10278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010279 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010280 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10281
10282 // second finger up with some unexpected data.
10283 processSlot(mapper, SECOND_SLOT);
10284 processId(mapper, INVALID_TRACKING_ID);
10285 processPosition(mapper, x2, y2);
10286 processSync(mapper);
10287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010288 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010289 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10290
10291 // first finger up with some unexpected data.
10292 processSlot(mapper, FIRST_SLOT);
10293 processId(mapper, INVALID_TRACKING_ID);
10294 processPosition(mapper, x2, y2);
10295 processPressure(mapper, RAW_PRESSURE_MAX);
10296 processSync(mapper);
10297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10298 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10299 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10300}
10301
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010302TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10303 addConfigurationProperty("touch.deviceType", "touchScreen");
10304 prepareDisplay(DISPLAY_ORIENTATION_0);
10305 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10306 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10307
10308 // First finger down.
10309 processId(mapper, FIRST_TRACKING_ID);
10310 processPosition(mapper, 100, 200);
10311 processPressure(mapper, RAW_PRESSURE_MAX);
10312 processSync(mapper);
10313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10314 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10315
10316 // Second finger down.
10317 processSlot(mapper, SECOND_SLOT);
10318 processId(mapper, SECOND_TRACKING_ID);
10319 processPosition(mapper, 300, 400);
10320 processPressure(mapper, RAW_PRESSURE_MAX);
10321 processSync(mapper);
10322 ASSERT_NO_FATAL_FAILURE(
10323 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10324
10325 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010326 // preserved. Resetting should cancel the ongoing gesture.
10327 resetMapper(mapper, ARBITRARY_TIME);
10328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10329 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010330
10331 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10332 // the existing touch state to generate a down event.
10333 processPosition(mapper, 301, 302);
10334 processSync(mapper);
10335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10336 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10338 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10339
10340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10341}
10342
10343TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10344 addConfigurationProperty("touch.deviceType", "touchScreen");
10345 prepareDisplay(DISPLAY_ORIENTATION_0);
10346 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10347 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10348
10349 // First finger touches down and releases.
10350 processId(mapper, FIRST_TRACKING_ID);
10351 processPosition(mapper, 100, 200);
10352 processPressure(mapper, RAW_PRESSURE_MAX);
10353 processSync(mapper);
10354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10355 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10356 processId(mapper, INVALID_TRACKING_ID);
10357 processSync(mapper);
10358 ASSERT_NO_FATAL_FAILURE(
10359 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10360
10361 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10362 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010363 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10365
10366 // Send an empty sync frame. Since there are no pointers, no events are generated.
10367 processSync(mapper);
10368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10369}
10370
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010371// --- MultiTouchInputMapperTest_ExternalDevice ---
10372
10373class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10374protected:
Chris Yea52ade12020-08-27 16:49:20 -070010375 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010376};
10377
10378/**
10379 * Expect fallback to internal viewport if device is external and external viewport is not present.
10380 */
10381TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10382 prepareAxes(POSITION);
10383 addConfigurationProperty("touch.deviceType", "touchScreen");
10384 prepareDisplay(DISPLAY_ORIENTATION_0);
10385 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10386
10387 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10388
10389 NotifyMotionArgs motionArgs;
10390
10391 // Expect the event to be sent to the internal viewport,
10392 // because an external viewport is not present.
10393 processPosition(mapper, 100, 100);
10394 processSync(mapper);
10395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10396 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10397
10398 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010399 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010400 processPosition(mapper, 100, 100);
10401 processSync(mapper);
10402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10403 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10404}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010405
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010406TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10407 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10408 std::shared_ptr<FakePointerController> fakePointerController =
10409 std::make_shared<FakePointerController>();
10410 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10411 fakePointerController->setPosition(0, 0);
10412 fakePointerController->setButtonState(0);
10413
10414 // prepare device and capture
10415 prepareDisplay(DISPLAY_ORIENTATION_0);
10416 prepareAxes(POSITION | ID | SLOT);
10417 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10418 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10419 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010420 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010421 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10422
10423 // captured touchpad should be a touchpad source
10424 NotifyDeviceResetArgs resetArgs;
10425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10426 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10427
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010428 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010429
10430 const InputDeviceInfo::MotionRange* relRangeX =
10431 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10432 ASSERT_NE(relRangeX, nullptr);
10433 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10434 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10435 const InputDeviceInfo::MotionRange* relRangeY =
10436 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10437 ASSERT_NE(relRangeY, nullptr);
10438 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10439 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10440
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010441 // run captured pointer tests - note that this is unscaled, so input listener events should be
10442 // identical to what the hardware sends (accounting for any
10443 // calibration).
10444 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010445 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010446 processId(mapper, 1);
10447 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10448 processKey(mapper, BTN_TOUCH, 1);
10449 processSync(mapper);
10450
10451 // expect coord[0] to contain initial location of touch 0
10452 NotifyMotionArgs args;
10453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10454 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10455 ASSERT_EQ(1U, args.pointerCount);
10456 ASSERT_EQ(0, args.pointerProperties[0].id);
10457 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10458 ASSERT_NO_FATAL_FAILURE(
10459 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10460
10461 // FINGER 1 DOWN
10462 processSlot(mapper, 1);
10463 processId(mapper, 2);
10464 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10465 processSync(mapper);
10466
10467 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010469 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010470 ASSERT_EQ(2U, args.pointerCount);
10471 ASSERT_EQ(0, args.pointerProperties[0].id);
10472 ASSERT_EQ(1, args.pointerProperties[1].id);
10473 ASSERT_NO_FATAL_FAILURE(
10474 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10475 ASSERT_NO_FATAL_FAILURE(
10476 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10477
10478 // FINGER 1 MOVE
10479 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10480 processSync(mapper);
10481
10482 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10483 // from move
10484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10485 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10486 ASSERT_NO_FATAL_FAILURE(
10487 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10488 ASSERT_NO_FATAL_FAILURE(
10489 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10490
10491 // FINGER 0 MOVE
10492 processSlot(mapper, 0);
10493 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10494 processSync(mapper);
10495
10496 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10498 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10499 ASSERT_NO_FATAL_FAILURE(
10500 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10501 ASSERT_NO_FATAL_FAILURE(
10502 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10503
10504 // BUTTON DOWN
10505 processKey(mapper, BTN_LEFT, 1);
10506 processSync(mapper);
10507
10508 // touchinputmapper design sends a move before button press
10509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10510 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10512 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10513
10514 // BUTTON UP
10515 processKey(mapper, BTN_LEFT, 0);
10516 processSync(mapper);
10517
10518 // touchinputmapper design sends a move after button release
10519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10520 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10522 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10523
10524 // FINGER 0 UP
10525 processId(mapper, -1);
10526 processSync(mapper);
10527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10528 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10529
10530 // FINGER 1 MOVE
10531 processSlot(mapper, 1);
10532 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10533 processSync(mapper);
10534
10535 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10538 ASSERT_EQ(1U, args.pointerCount);
10539 ASSERT_EQ(1, args.pointerProperties[0].id);
10540 ASSERT_NO_FATAL_FAILURE(
10541 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10542
10543 // FINGER 1 UP
10544 processId(mapper, -1);
10545 processKey(mapper, BTN_TOUCH, 0);
10546 processSync(mapper);
10547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10548 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10549
Harry Cutts16a24cc2022-10-26 15:22:19 +000010550 // A non captured touchpad should have a mouse and touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010551 mFakePolicy->setPointerCapture(false);
10552 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Harry Cutts16a24cc2022-10-26 15:22:19 +000010554 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010555}
10556
10557TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10558 std::shared_ptr<FakePointerController> fakePointerController =
10559 std::make_shared<FakePointerController>();
10560 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10561 fakePointerController->setPosition(0, 0);
10562 fakePointerController->setButtonState(0);
10563
10564 // prepare device and capture
10565 prepareDisplay(DISPLAY_ORIENTATION_0);
10566 prepareAxes(POSITION | ID | SLOT);
10567 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10568 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010569 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010570 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10571 // run uncaptured pointer tests - pushes out generic events
10572 // FINGER 0 DOWN
10573 processId(mapper, 3);
10574 processPosition(mapper, 100, 100);
10575 processKey(mapper, BTN_TOUCH, 1);
10576 processSync(mapper);
10577
10578 // start at (100,100), cursor should be at (0,0) * scale
10579 NotifyMotionArgs args;
10580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10581 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10582 ASSERT_NO_FATAL_FAILURE(
10583 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10584
10585 // FINGER 0 MOVE
10586 processPosition(mapper, 200, 200);
10587 processSync(mapper);
10588
10589 // compute scaling to help with touch position checking
10590 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10591 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10592 float scale =
10593 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10594
10595 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10597 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10599 0, 0, 0, 0, 0, 0, 0));
10600}
10601
10602TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10603 std::shared_ptr<FakePointerController> fakePointerController =
10604 std::make_shared<FakePointerController>();
10605
10606 prepareDisplay(DISPLAY_ORIENTATION_0);
10607 prepareAxes(POSITION | ID | SLOT);
10608 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010609 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010610 mFakePolicy->setPointerCapture(false);
10611 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10612
Harry Cutts16a24cc2022-10-26 15:22:19 +000010613 // An uncaptured touchpad should be a pointer device, with additional touchpad source.
10614 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010615
Harry Cutts16a24cc2022-10-26 15:22:19 +000010616 // A captured touchpad should just have a touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010617 mFakePolicy->setPointerCapture(true);
10618 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10619 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10620}
10621
HQ Liue6983c72022-04-19 22:14:56 +000010622class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10623protected:
10624 float mPointerMovementScale;
10625 float mPointerXZoomScale;
10626 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10627 addConfigurationProperty("touch.deviceType", "pointer");
10628 std::shared_ptr<FakePointerController> fakePointerController =
10629 std::make_shared<FakePointerController>();
10630 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10631 fakePointerController->setPosition(0, 0);
10632 fakePointerController->setButtonState(0);
10633 prepareDisplay(DISPLAY_ORIENTATION_0);
10634
10635 prepareAxes(POSITION);
10636 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10637 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10638 // needs to be disabled, and the pointer gesture needs to be enabled.
10639 mFakePolicy->setPointerCapture(false);
10640 mFakePolicy->setPointerGestureEnabled(true);
10641 mFakePolicy->setPointerController(fakePointerController);
10642
10643 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10644 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10645 mPointerMovementScale =
10646 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10647 mPointerXZoomScale =
10648 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10649 }
10650
10651 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10652 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10653 /*flat*/ 0,
10654 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10655 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10656 /*flat*/ 0,
10657 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10658 }
10659};
10660
10661/**
10662 * Two fingers down on a pointer mode touch pad. The width
10663 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10664 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10665 * be greater than the both value to be freeform gesture, so that after two
10666 * fingers start to move downwards, the gesture should be swipe.
10667 */
10668TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10669 // The min freeform gesture width is 25units/mm x 30mm = 750
10670 // which is greater than fraction of the diagnal length of the touchpad (349).
10671 // Thus, MaxSwipWidth is 750.
10672 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10673 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10674 NotifyMotionArgs motionArgs;
10675
10676 // Two fingers down at once.
10677 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10678 // Pointer's initial position is used the [0,0] coordinate.
10679 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10680
10681 processId(mapper, FIRST_TRACKING_ID);
10682 processPosition(mapper, x1, y1);
10683 processMTSync(mapper);
10684 processId(mapper, SECOND_TRACKING_ID);
10685 processPosition(mapper, x2, y2);
10686 processMTSync(mapper);
10687 processSync(mapper);
10688
10689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10690 ASSERT_EQ(1U, motionArgs.pointerCount);
10691 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10692 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010693 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010694 ASSERT_NO_FATAL_FAILURE(
10695 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10696
10697 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10698 // that there should be 1 pointer.
10699 int32_t movingDistance = 200;
10700 y1 += movingDistance;
10701 y2 += movingDistance;
10702
10703 processId(mapper, FIRST_TRACKING_ID);
10704 processPosition(mapper, x1, y1);
10705 processMTSync(mapper);
10706 processId(mapper, SECOND_TRACKING_ID);
10707 processPosition(mapper, x2, y2);
10708 processMTSync(mapper);
10709 processSync(mapper);
10710
10711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10712 ASSERT_EQ(1U, motionArgs.pointerCount);
10713 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10714 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010715 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010716 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10717 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10718 0, 0, 0, 0));
10719}
10720
10721/**
10722 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10723 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10724 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10725 * value to be freeform gesture, so that after two fingers start to move downwards,
10726 * the gesture should be swipe.
10727 */
10728TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10729 // The min freeform gesture width is 5units/mm x 30mm = 150
10730 // which is greater than fraction of the diagnal length of the touchpad (349).
10731 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
10732 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
10733 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10734 NotifyMotionArgs motionArgs;
10735
10736 // Two fingers down at once.
10737 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10738 // Pointer's initial position is used the [0,0] coordinate.
10739 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10740
10741 processId(mapper, FIRST_TRACKING_ID);
10742 processPosition(mapper, x1, y1);
10743 processMTSync(mapper);
10744 processId(mapper, SECOND_TRACKING_ID);
10745 processPosition(mapper, x2, y2);
10746 processMTSync(mapper);
10747 processSync(mapper);
10748
10749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10750 ASSERT_EQ(1U, motionArgs.pointerCount);
10751 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10752 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010753 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010754 ASSERT_NO_FATAL_FAILURE(
10755 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10756
10757 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10758 // and there should be 1 pointer.
10759 int32_t movingDistance = 200;
10760 y1 += movingDistance;
10761 y2 += movingDistance;
10762
10763 processId(mapper, FIRST_TRACKING_ID);
10764 processPosition(mapper, x1, y1);
10765 processMTSync(mapper);
10766 processId(mapper, SECOND_TRACKING_ID);
10767 processPosition(mapper, x2, y2);
10768 processMTSync(mapper);
10769 processSync(mapper);
10770
10771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10772 ASSERT_EQ(1U, motionArgs.pointerCount);
10773 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10774 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010775 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010776 // New coordinate is the scaled relative coordinate from the initial coordinate.
10777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10778 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10779 0, 0, 0, 0));
10780}
10781
10782/**
10783 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10784 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10785 * freeform gestures after two fingers start to move downwards.
10786 */
10787TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
10788 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10789 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10790
10791 NotifyMotionArgs motionArgs;
10792
10793 // Two fingers down at once. Wider than the max swipe width.
10794 // The gesture is expected to be PRESS, then transformed to FREEFORM
10795 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10796
10797 processId(mapper, FIRST_TRACKING_ID);
10798 processPosition(mapper, x1, y1);
10799 processMTSync(mapper);
10800 processId(mapper, SECOND_TRACKING_ID);
10801 processPosition(mapper, x2, y2);
10802 processMTSync(mapper);
10803 processSync(mapper);
10804
10805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10806 ASSERT_EQ(1U, motionArgs.pointerCount);
10807 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10808 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010809 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010810 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10811 ASSERT_NO_FATAL_FAILURE(
10812 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10813
10814 int32_t movingDistance = 200;
10815
10816 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10817 // then two down events for two pointers.
10818 y1 += movingDistance;
10819 y2 += movingDistance;
10820
10821 processId(mapper, FIRST_TRACKING_ID);
10822 processPosition(mapper, x1, y1);
10823 processMTSync(mapper);
10824 processId(mapper, SECOND_TRACKING_ID);
10825 processPosition(mapper, x2, y2);
10826 processMTSync(mapper);
10827 processSync(mapper);
10828
10829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10830 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10831 ASSERT_EQ(1U, motionArgs.pointerCount);
10832 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10834 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10835 ASSERT_EQ(1U, motionArgs.pointerCount);
10836 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10838 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010839 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010840 ASSERT_EQ(2U, motionArgs.pointerCount);
10841 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10842 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010843 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010844 // Two pointers' scaled relative coordinates from their initial centroid.
10845 // Initial y coordinates are 0 as y1 and y2 have the same value.
10846 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10847 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10848 // When pointers move, the new coordinates equal to the initial coordinates plus
10849 // scaled moving distance.
10850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10851 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10852 0, 0, 0, 0));
10853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10854 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10855 0, 0, 0, 0));
10856
10857 // Move two fingers down again, expect one MOVE motion event.
10858 y1 += movingDistance;
10859 y2 += movingDistance;
10860
10861 processId(mapper, FIRST_TRACKING_ID);
10862 processPosition(mapper, x1, y1);
10863 processMTSync(mapper);
10864 processId(mapper, SECOND_TRACKING_ID);
10865 processPosition(mapper, x2, y2);
10866 processMTSync(mapper);
10867 processSync(mapper);
10868
10869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10870 ASSERT_EQ(2U, motionArgs.pointerCount);
10871 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10872 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010873 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10875 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10876 0, 0, 0, 0, 0));
10877 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10878 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10879 0, 0, 0, 0, 0));
10880}
10881
Harry Cutts39b7ca22022-10-05 15:55:48 +000010882TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
10883 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10884 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10885 NotifyMotionArgs motionArgs;
10886
10887 // Place two fingers down.
10888 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10889
10890 processId(mapper, FIRST_TRACKING_ID);
10891 processPosition(mapper, x1, y1);
10892 processMTSync(mapper);
10893 processId(mapper, SECOND_TRACKING_ID);
10894 processPosition(mapper, x2, y2);
10895 processMTSync(mapper);
10896 processSync(mapper);
10897
10898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10899 ASSERT_EQ(1U, motionArgs.pointerCount);
10900 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10901 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10902 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10903 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10904
10905 // Move the two fingers down and to the left.
10906 int32_t movingDistance = 200;
10907 x1 -= movingDistance;
10908 y1 += movingDistance;
10909 x2 -= movingDistance;
10910 y2 += movingDistance;
10911
10912 processId(mapper, FIRST_TRACKING_ID);
10913 processPosition(mapper, x1, y1);
10914 processMTSync(mapper);
10915 processId(mapper, SECOND_TRACKING_ID);
10916 processPosition(mapper, x2, y2);
10917 processMTSync(mapper);
10918 processSync(mapper);
10919
10920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10921 ASSERT_EQ(1U, motionArgs.pointerCount);
10922 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10923 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10924 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10925 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10926}
10927
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010928TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
10929 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10930 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10931 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10933
10934 // Start a stylus gesture.
10935 processKey(mapper, BTN_TOOL_PEN, 1);
10936 processId(mapper, FIRST_TRACKING_ID);
10937 processPosition(mapper, 100, 200);
10938 processSync(mapper);
10939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10940 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10941 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10942 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10943 // TODO(b/257078296): Pointer mode generates extra event.
10944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10945 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10946 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10947 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10949
10950 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10951 // gesture should be disabled.
10952 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10953 viewport->isActive = false;
10954 mFakePolicy->updateViewport(*viewport);
10955 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10957 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10958 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10959 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10960 // TODO(b/257078296): Pointer mode generates extra event.
10961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10962 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10963 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10964 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10966}
10967
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010968// --- JoystickInputMapperTest ---
10969
10970class JoystickInputMapperTest : public InputMapperTest {
10971protected:
10972 static const int32_t RAW_X_MIN;
10973 static const int32_t RAW_X_MAX;
10974 static const int32_t RAW_Y_MIN;
10975 static const int32_t RAW_Y_MAX;
10976
10977 void SetUp() override {
10978 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10979 }
10980 void prepareAxes() {
10981 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10982 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10983 }
10984
10985 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10986 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10987 }
10988
10989 void processSync(JoystickInputMapper& mapper) {
10990 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10991 }
10992
10993 void prepareVirtualDisplay(int32_t orientation) {
10994 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10995 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10996 NO_PORT, ViewportType::VIRTUAL);
10997 }
10998};
10999
11000const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
11001const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
11002const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
11003const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
11004
11005TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
11006 prepareAxes();
11007 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
11008
11009 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
11010
11011 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
11012
11013 // Send an axis event
11014 processAxis(mapper, ABS_X, 100);
11015 processSync(mapper);
11016
11017 NotifyMotionArgs args;
11018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11019 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11020
11021 // Send another axis event
11022 processAxis(mapper, ABS_Y, 100);
11023 processSync(mapper);
11024
11025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11026 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11027}
11028
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011029// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011030
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011031class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011032protected:
11033 static const char* DEVICE_NAME;
11034 static const char* DEVICE_LOCATION;
11035 static const int32_t DEVICE_ID;
11036 static const int32_t DEVICE_GENERATION;
11037 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011038 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011039 static const int32_t EVENTHUB_ID;
11040
11041 std::shared_ptr<FakeEventHub> mFakeEventHub;
11042 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011043 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011044 std::unique_ptr<InstrumentedInputReader> mReader;
11045 std::shared_ptr<InputDevice> mDevice;
11046
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011047 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011048 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011049 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011050 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011051 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011052 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011053 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11054 }
11055
11056 void SetUp() override { SetUp(DEVICE_CLASSES); }
11057
11058 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011059 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011060 mFakePolicy.clear();
11061 }
11062
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070011063 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011064 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
11065 mReader->requestRefreshConfiguration(changes);
11066 mReader->loopOnce();
11067 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070011068 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011069 }
11070
11071 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11072 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011073 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011074 InputDeviceIdentifier identifier;
11075 identifier.name = name;
11076 identifier.location = location;
11077 std::shared_ptr<InputDevice> device =
11078 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11079 identifier);
11080 mReader->pushNextDevice(device);
11081 mFakeEventHub->addDevice(eventHubId, name, classes);
11082 mReader->loopOnce();
11083 return device;
11084 }
11085
11086 template <class T, typename... Args>
11087 T& addControllerAndConfigure(Args... args) {
11088 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11089
11090 return controller;
11091 }
11092};
11093
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011094const char* PeripheralControllerTest::DEVICE_NAME = "device";
11095const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11096const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11097const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11098const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011099const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11100 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011101const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011102
11103// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011104class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011105protected:
11106 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011107 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011108 }
11109};
11110
11111TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011112 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011113
11114 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
11115 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
11116}
11117
11118TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011119 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011120
11121 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
11122 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
11123}
11124
11125// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011126class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011127protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011128 void SetUp() override {
11129 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11130 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011131};
11132
Chris Ye85758332021-05-16 23:05:17 -070011133TEST_F(LightControllerTest, MonoLight) {
11134 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011135 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011136 .maxBrightness = 255,
11137 .flags = InputLightClass::BRIGHTNESS,
11138 .path = ""};
11139 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011140
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011141 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011142 InputDeviceInfo info;
11143 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011144 std::vector<InputDeviceLightInfo> lights = info.getLights();
11145 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011146 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11147 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11148
11149 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11150 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11151}
11152
11153TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11154 RawLightInfo infoMono = {.id = 1,
11155 .name = "mono_keyboard_backlight",
11156 .maxBrightness = 255,
11157 .flags = InputLightClass::BRIGHTNESS |
11158 InputLightClass::KEYBOARD_BACKLIGHT,
11159 .path = ""};
11160 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11161
11162 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11163 InputDeviceInfo info;
11164 controller.populateDeviceInfo(&info);
11165 std::vector<InputDeviceLightInfo> lights = info.getLights();
11166 ASSERT_EQ(1U, lights.size());
11167 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11168 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011169
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011170 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11171 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011172}
11173
11174TEST_F(LightControllerTest, RGBLight) {
11175 RawLightInfo infoRed = {.id = 1,
11176 .name = "red",
11177 .maxBrightness = 255,
11178 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11179 .path = ""};
11180 RawLightInfo infoGreen = {.id = 2,
11181 .name = "green",
11182 .maxBrightness = 255,
11183 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11184 .path = ""};
11185 RawLightInfo infoBlue = {.id = 3,
11186 .name = "blue",
11187 .maxBrightness = 255,
11188 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11189 .path = ""};
11190 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11191 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11192 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11193
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011194 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011195 InputDeviceInfo info;
11196 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011197 std::vector<InputDeviceLightInfo> lights = info.getLights();
11198 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011199 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11200 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11201 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11202
11203 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11204 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11205}
11206
11207TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11208 RawLightInfo infoRed = {.id = 1,
11209 .name = "red_keyboard_backlight",
11210 .maxBrightness = 255,
11211 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11212 InputLightClass::KEYBOARD_BACKLIGHT,
11213 .path = ""};
11214 RawLightInfo infoGreen = {.id = 2,
11215 .name = "green_keyboard_backlight",
11216 .maxBrightness = 255,
11217 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11218 InputLightClass::KEYBOARD_BACKLIGHT,
11219 .path = ""};
11220 RawLightInfo infoBlue = {.id = 3,
11221 .name = "blue_keyboard_backlight",
11222 .maxBrightness = 255,
11223 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11224 InputLightClass::KEYBOARD_BACKLIGHT,
11225 .path = ""};
11226 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11227 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11228 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11229
11230 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11231 InputDeviceInfo info;
11232 controller.populateDeviceInfo(&info);
11233 std::vector<InputDeviceLightInfo> lights = info.getLights();
11234 ASSERT_EQ(1U, lights.size());
11235 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11236 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11237 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11238
11239 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11240 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11241}
11242
11243TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11244 RawLightInfo infoRed = {.id = 1,
11245 .name = "red",
11246 .maxBrightness = 255,
11247 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11248 .path = ""};
11249 RawLightInfo infoGreen = {.id = 2,
11250 .name = "green",
11251 .maxBrightness = 255,
11252 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11253 .path = ""};
11254 RawLightInfo infoBlue = {.id = 3,
11255 .name = "blue",
11256 .maxBrightness = 255,
11257 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11258 .path = ""};
11259 RawLightInfo infoGlobal = {.id = 3,
11260 .name = "global_keyboard_backlight",
11261 .maxBrightness = 255,
11262 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11263 InputLightClass::KEYBOARD_BACKLIGHT,
11264 .path = ""};
11265 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11266 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11267 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11268 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11269
11270 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11271 InputDeviceInfo info;
11272 controller.populateDeviceInfo(&info);
11273 std::vector<InputDeviceLightInfo> lights = info.getLights();
11274 ASSERT_EQ(1U, lights.size());
11275 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11276 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11277 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011278
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011279 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11280 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011281}
11282
11283TEST_F(LightControllerTest, MultiColorRGBLight) {
11284 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011285 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011286 .maxBrightness = 255,
11287 .flags = InputLightClass::BRIGHTNESS |
11288 InputLightClass::MULTI_INTENSITY |
11289 InputLightClass::MULTI_INDEX,
11290 .path = ""};
11291
11292 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11293
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011294 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011295 InputDeviceInfo info;
11296 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011297 std::vector<InputDeviceLightInfo> lights = info.getLights();
11298 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011299 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11300 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11301 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11302
11303 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11304 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11305}
11306
11307TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11308 RawLightInfo infoColor = {.id = 1,
11309 .name = "multi_color_keyboard_backlight",
11310 .maxBrightness = 255,
11311 .flags = InputLightClass::BRIGHTNESS |
11312 InputLightClass::MULTI_INTENSITY |
11313 InputLightClass::MULTI_INDEX |
11314 InputLightClass::KEYBOARD_BACKLIGHT,
11315 .path = ""};
11316
11317 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11318
11319 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11320 InputDeviceInfo info;
11321 controller.populateDeviceInfo(&info);
11322 std::vector<InputDeviceLightInfo> lights = info.getLights();
11323 ASSERT_EQ(1U, lights.size());
11324 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11325 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11326 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011327
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011328 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11329 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011330}
11331
11332TEST_F(LightControllerTest, PlayerIdLight) {
11333 RawLightInfo info1 = {.id = 1,
11334 .name = "player1",
11335 .maxBrightness = 255,
11336 .flags = InputLightClass::BRIGHTNESS,
11337 .path = ""};
11338 RawLightInfo info2 = {.id = 2,
11339 .name = "player2",
11340 .maxBrightness = 255,
11341 .flags = InputLightClass::BRIGHTNESS,
11342 .path = ""};
11343 RawLightInfo info3 = {.id = 3,
11344 .name = "player3",
11345 .maxBrightness = 255,
11346 .flags = InputLightClass::BRIGHTNESS,
11347 .path = ""};
11348 RawLightInfo info4 = {.id = 4,
11349 .name = "player4",
11350 .maxBrightness = 255,
11351 .flags = InputLightClass::BRIGHTNESS,
11352 .path = ""};
11353 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11354 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11355 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11356 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11357
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011358 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011359 InputDeviceInfo info;
11360 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011361 std::vector<InputDeviceLightInfo> lights = info.getLights();
11362 ASSERT_EQ(1U, lights.size());
11363 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011364 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11365 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011366
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011367 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11368 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11369 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011370}
11371
Michael Wrightd02c5b62014-02-10 15:10:22 -080011372} // namespace android