blob: 56c1ade82013328930772bba63824ffbdc217e21 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
19
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <CursorInputMapper.h>
21#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080024#include <InputReaderBase.h>
25#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000026#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070029#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070030#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
32#include <SwitchInputMapper.h>
33#include <TestInputListener.h>
Prabir Pradhan739dca42022-09-09 20:12:01 +000034#include <TestInputListenerMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070035#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080036#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000037#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070038#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080039#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050040#include <gui/constants.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070042#include <thread>
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000043#include "android/hardware/input/InputDeviceCountryCode.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000044#include "input/DisplayViewport.h"
45#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010046
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000047using android::hardware::input::InputDeviceCountryCode;
48
Michael Wrightd02c5b62014-02-10 15:10:22 -080049namespace android {
50
Dominik Laskowski2f01d772022-03-23 16:01:29 -070051using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000052using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070053using std::chrono_literals::operator""ms;
54
55// Timeout for waiting for an expected event
56static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
57
Michael Wrightd02c5b62014-02-10 15:10:22 -080058// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000059static constexpr nsecs_t ARBITRARY_TIME = 1234;
60static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080061
62// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080063static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000064static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080065static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000066static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080067static constexpr int32_t DISPLAY_WIDTH = 480;
68static constexpr int32_t DISPLAY_HEIGHT = 800;
69static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
70static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
71static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070072static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070073static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080074
arthurhungcc7f9802020-04-30 17:55:40 +080075static constexpr int32_t FIRST_SLOT = 0;
76static constexpr int32_t SECOND_SLOT = 1;
77static constexpr int32_t THIRD_SLOT = 2;
78static constexpr int32_t INVALID_TRACKING_ID = -1;
79static constexpr int32_t FIRST_TRACKING_ID = 0;
80static constexpr int32_t SECOND_TRACKING_ID = 1;
81static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080082static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080083static constexpr int32_t BATTERY_STATUS = 4;
84static constexpr int32_t BATTERY_CAPACITY = 66;
Prabir Pradhane287ecd2022-09-07 21:18:05 +000085static const std::string BATTERY_DEVPATH = "/sys/devices/mydevice/power_supply/mybattery";
Chris Ye3fdbfef2021-01-06 18:45:18 -080086static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
87static constexpr int32_t LIGHT_COLOR = 0x7F448866;
88static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080089
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080090static constexpr int32_t ACTION_POINTER_0_DOWN =
91 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
92static constexpr int32_t ACTION_POINTER_0_UP =
93 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
94static constexpr int32_t ACTION_POINTER_1_DOWN =
95 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
96static constexpr int32_t ACTION_POINTER_1_UP =
97 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
98
Michael Wrightd02c5b62014-02-10 15:10:22 -080099// Error tolerance for floating point assertions.
100static const float EPSILON = 0.001f;
101
102template<typename T>
103static inline T min(T a, T b) {
104 return a < b ? a : b;
105}
106
107static inline float avg(float x, float y) {
108 return (x + y) / 2;
109}
110
Chris Ye3fdbfef2021-01-06 18:45:18 -0800111// Mapping for light color name and the light color
112const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
113 {"green", LightColor::GREEN},
114 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800115
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700116static int32_t getInverseRotation(int32_t orientation) {
117 switch (orientation) {
118 case DISPLAY_ORIENTATION_90:
119 return DISPLAY_ORIENTATION_270;
120 case DISPLAY_ORIENTATION_270:
121 return DISPLAY_ORIENTATION_90;
122 default:
123 return orientation;
124 }
125}
126
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800127static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
128 InputDeviceInfo info;
129 mapper.populateDeviceInfo(&info);
130
131 const InputDeviceInfo::MotionRange* motionRange =
132 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
133 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
134}
135
136static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
137 InputDeviceInfo info;
138 mapper.populateDeviceInfo(&info);
139
140 const InputDeviceInfo::MotionRange* motionRange =
141 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
142 ASSERT_EQ(nullptr, motionRange);
143}
144
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700145[[maybe_unused]] static void dumpReader(InputReader& reader) {
146 std::string dump;
147 reader.dump(dump);
148 std::istringstream iss(dump);
149 for (std::string line; std::getline(iss, line);) {
150 ALOGE("%s", line.c_str());
151 std::this_thread::sleep_for(std::chrono::milliseconds(1));
152 }
153}
154
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155// --- FakePointerController ---
156
157class FakePointerController : public PointerControllerInterface {
158 bool mHaveBounds;
159 float mMinX, mMinY, mMaxX, mMaxY;
160 float mX, mY;
161 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800162 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164public:
165 FakePointerController() :
166 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800167 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168 }
169
Michael Wright17db18e2020-06-26 20:51:44 +0100170 virtual ~FakePointerController() {}
171
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172 void setBounds(float minX, float minY, float maxX, float maxY) {
173 mHaveBounds = true;
174 mMinX = minX;
175 mMinY = minY;
176 mMaxX = maxX;
177 mMaxY = maxY;
178 }
179
Chris Yea52ade12020-08-27 16:49:20 -0700180 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181 mX = x;
182 mY = y;
183 }
184
Chris Yea52ade12020-08-27 16:49:20 -0700185 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186
Chris Yea52ade12020-08-27 16:49:20 -0700187 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188
Chris Yea52ade12020-08-27 16:49:20 -0700189 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190 *outX = mX;
191 *outY = mY;
192 }
193
Chris Yea52ade12020-08-27 16:49:20 -0700194 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800195
Chris Yea52ade12020-08-27 16:49:20 -0700196 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800197 mDisplayId = viewport.displayId;
198 }
199
Arthur Hung7c645402019-01-25 17:45:42 +0800200 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
201 return mSpotsByDisplay;
202 }
203
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204private:
Chris Yea52ade12020-08-27 16:49:20 -0700205 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206 *outMinX = mMinX;
207 *outMinY = mMinY;
208 *outMaxX = mMaxX;
209 *outMaxY = mMaxY;
210 return mHaveBounds;
211 }
212
Chris Yea52ade12020-08-27 16:49:20 -0700213 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800214 mX += deltaX;
215 if (mX < mMinX) mX = mMinX;
216 if (mX > mMaxX) mX = mMaxX;
217 mY += deltaY;
218 if (mY < mMinY) mY = mMinY;
219 if (mY > mMaxY) mY = mMaxY;
220 }
221
Chris Yea52ade12020-08-27 16:49:20 -0700222 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800223
Chris Yea52ade12020-08-27 16:49:20 -0700224 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225
Chris Yea52ade12020-08-27 16:49:20 -0700226 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800227
Chris Yea52ade12020-08-27 16:49:20 -0700228 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
229 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800230 std::vector<int32_t> newSpots;
231 // Add spots for fingers that are down.
232 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
233 uint32_t id = idBits.clearFirstMarkedBit();
234 newSpots.push_back(id);
235 }
236
237 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800238 }
239
Prabir Pradhan197e0862022-07-01 14:28:00 +0000240 void clearSpots() override { mSpotsByDisplay.clear(); }
Arthur Hung7c645402019-01-25 17:45:42 +0800241
242 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243};
244
245
246// --- FakeInputReaderPolicy ---
247
248class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700249 std::mutex mLock;
250 std::condition_variable mDevicesChangedCondition;
251
Michael Wrightd02c5b62014-02-10 15:10:22 -0800252 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000253 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700254 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
255 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100256 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700257 TouchAffineTransformation transform;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000258 std::optional<int32_t /*deviceId*/> mStylusGestureNotified GUARDED_BY(mLock){};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259
260protected:
Chris Yea52ade12020-08-27 16:49:20 -0700261 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800262
263public:
264 FakeInputReaderPolicy() {
265 }
266
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700267 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800268 waitForInputDevices([](bool devicesChanged) {
269 if (!devicesChanged) {
270 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
271 }
272 });
273 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700274
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800275 void assertInputDevicesNotChanged() {
276 waitForInputDevices([](bool devicesChanged) {
277 if (devicesChanged) {
278 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
279 }
280 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700281 }
282
Prabir Pradhanda20b172022-09-26 17:01:18 +0000283 void assertStylusGestureNotified(int32_t deviceId) {
284 std::scoped_lock lock(mLock);
285 ASSERT_TRUE(mStylusGestureNotified);
286 ASSERT_EQ(deviceId, *mStylusGestureNotified);
287 mStylusGestureNotified.reset();
288 }
289
290 void assertStylusGestureNotNotified() {
291 std::scoped_lock lock(mLock);
292 ASSERT_FALSE(mStylusGestureNotified);
293 }
294
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700295 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100296 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100297 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700298 }
299
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700300 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
301 return mConfig.getDisplayViewportByUniqueId(uniqueId);
302 }
303 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
304 return mConfig.getDisplayViewportByType(type);
305 }
306
307 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
308 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700309 }
310
Prabir Pradhan5632d622021-09-06 07:57:20 -0700311 void addDisplayViewport(DisplayViewport viewport) {
312 mViewports.push_back(std::move(viewport));
313 mConfig.setDisplayViewports(mViewports);
314 }
315
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700316 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000317 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700318 std::optional<uint8_t> physicalPort, ViewportType type) {
319 const bool isRotated =
320 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
321 DisplayViewport v;
322 v.displayId = displayId;
323 v.orientation = orientation;
324 v.logicalLeft = 0;
325 v.logicalTop = 0;
326 v.logicalRight = isRotated ? height : width;
327 v.logicalBottom = isRotated ? width : height;
328 v.physicalLeft = 0;
329 v.physicalTop = 0;
330 v.physicalRight = isRotated ? height : width;
331 v.physicalBottom = isRotated ? width : height;
332 v.deviceWidth = isRotated ? height : width;
333 v.deviceHeight = isRotated ? width : height;
334 v.isActive = isActive;
335 v.uniqueId = uniqueId;
336 v.physicalPort = physicalPort;
337 v.type = type;
338
339 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800340 }
341
Arthur Hung6cd19a42019-08-30 19:04:12 +0800342 bool updateViewport(const DisplayViewport& viewport) {
343 size_t count = mViewports.size();
344 for (size_t i = 0; i < count; i++) {
345 const DisplayViewport& currentViewport = mViewports[i];
346 if (currentViewport.displayId == viewport.displayId) {
347 mViewports[i] = viewport;
348 mConfig.setDisplayViewports(mViewports);
349 return true;
350 }
351 }
352 // no viewport found.
353 return false;
354 }
355
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100356 void addExcludedDeviceName(const std::string& deviceName) {
357 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800358 }
359
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700360 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
361 mConfig.portAssociations.insert({inputPort, displayPort});
362 }
363
Christine Franks1ba71cc2021-04-07 14:37:42 -0700364 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
365 const std::string& displayUniqueId) {
366 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
367 }
368
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000369 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700370
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000371 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700372
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000373 void setPointerController(std::shared_ptr<FakePointerController> controller) {
374 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800375 }
376
377 const InputReaderConfiguration* getReaderConfiguration() const {
378 return &mConfig;
379 }
380
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800381 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382 return mInputDevices;
383 }
384
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100385 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700386 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700387 return transform;
388 }
389
390 void setTouchAffineTransformation(const TouchAffineTransformation t) {
391 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800392 }
393
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000394 PointerCaptureRequest setPointerCapture(bool enabled) {
395 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
396 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800397 }
398
Arthur Hung7c645402019-01-25 17:45:42 +0800399 void setShowTouches(bool enabled) {
400 mConfig.showTouches = enabled;
401 }
402
Garfield Tan888a6a42020-01-09 11:39:16 -0800403 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
404 mConfig.defaultPointerDisplayId = pointerDisplayId;
405 }
406
HQ Liue6983c72022-04-19 22:14:56 +0000407 void setPointerGestureEnabled(bool enabled) { mConfig.pointerGesturesEnabled = enabled; }
408
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800409 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
410
HQ Liue6983c72022-04-19 22:14:56 +0000411 float getPointerGestureZoomSpeedRatio() { return mConfig.pointerGestureZoomSpeedRatio; }
412
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000413 void setVelocityControlParams(const VelocityControlParameters& params) {
414 mConfig.pointerVelocityControlParameters = params;
415 mConfig.wheelVelocityControlParameters = params;
416 }
417
Michael Wrightd02c5b62014-02-10 15:10:22 -0800418private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000419 uint32_t mNextPointerCaptureSequenceNumber = 0;
420
Chris Yea52ade12020-08-27 16:49:20 -0700421 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800422 *outConfig = mConfig;
423 }
424
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000425 std::shared_ptr<PointerControllerInterface> obtainPointerController(
426 int32_t /*deviceId*/) override {
427 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800428 }
429
Chris Yea52ade12020-08-27 16:49:20 -0700430 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700431 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800432 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700433 mInputDevicesChanged = true;
434 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800435 }
436
Chris Yea52ade12020-08-27 16:49:20 -0700437 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
438 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700439 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800440 }
441
Chris Yea52ade12020-08-27 16:49:20 -0700442 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800443
444 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
445 std::unique_lock<std::mutex> lock(mLock);
446 base::ScopedLockAssertion assumeLocked(mLock);
447
448 const bool devicesChanged =
449 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
450 return mInputDevicesChanged;
451 });
452 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
453 mInputDevicesChanged = false;
454 }
Prabir Pradhanda20b172022-09-26 17:01:18 +0000455
456 void notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) override {
457 std::scoped_lock<std::mutex> lock(mLock);
458 mStylusGestureNotified = deviceId;
459 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800460};
461
Michael Wrightd02c5b62014-02-10 15:10:22 -0800462// --- FakeEventHub ---
463
464class FakeEventHub : public EventHubInterface {
465 struct KeyInfo {
466 int32_t keyCode;
467 uint32_t flags;
468 };
469
Chris Yef59a2f42020-10-16 12:55:26 -0700470 struct SensorInfo {
471 InputDeviceSensorType sensorType;
472 int32_t sensorDataIndex;
473 };
474
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475 struct Device {
476 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700477 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800478 PropertyMap configuration;
479 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
480 KeyedVector<int, bool> relativeAxes;
481 KeyedVector<int32_t, int32_t> keyCodeStates;
482 KeyedVector<int32_t, int32_t> scanCodeStates;
483 KeyedVector<int32_t, int32_t> switchStates;
484 KeyedVector<int32_t, int32_t> absoluteAxisValue;
485 KeyedVector<int32_t, KeyInfo> keysByScanCode;
486 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
487 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100488 // fake mapping which would normally come from keyCharacterMap
489 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700490 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
491 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800492 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700493 bool enabled;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000494 InputDeviceCountryCode countryCode;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700495
496 status_t enable() {
497 enabled = true;
498 return OK;
499 }
500
501 status_t disable() {
502 enabled = false;
503 return OK;
504 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800505
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700506 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800507 };
508
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700509 std::mutex mLock;
510 std::condition_variable mEventsCondition;
511
Michael Wrightd02c5b62014-02-10 15:10:22 -0800512 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100513 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000514 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600515 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000516 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800517 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
518 // Simulates a device light brightness, from light id to light brightness.
519 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
520 // Simulates a device light intensities, from light id to light intensities map.
521 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
522 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800523
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700524public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800525 virtual ~FakeEventHub() {
526 for (size_t i = 0; i < mDevices.size(); i++) {
527 delete mDevices.valueAt(i);
528 }
529 }
530
Michael Wrightd02c5b62014-02-10 15:10:22 -0800531 FakeEventHub() { }
532
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700533 void addDevice(int32_t deviceId, const std::string& name,
534 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800535 Device* device = new Device(classes);
536 device->identifier.name = name;
537 mDevices.add(deviceId, device);
538
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000539 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800540 }
541
542 void removeDevice(int32_t deviceId) {
543 delete mDevices.valueFor(deviceId);
544 mDevices.removeItem(deviceId);
545
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000546 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800547 }
548
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000549 bool isDeviceEnabled(int32_t deviceId) const override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700550 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700551 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700552 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
553 return false;
554 }
555 return device->enabled;
556 }
557
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000558 status_t enableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700559 status_t result;
560 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700561 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700562 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
563 return BAD_VALUE;
564 }
565 if (device->enabled) {
566 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
567 return OK;
568 }
569 result = device->enable();
570 return result;
571 }
572
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000573 status_t disableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700574 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700575 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700576 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
577 return BAD_VALUE;
578 }
579 if (!device->enabled) {
580 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
581 return OK;
582 }
583 return device->disable();
584 }
585
Michael Wrightd02c5b62014-02-10 15:10:22 -0800586 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000587 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588 }
589
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700590 void addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800591 Device* device = getDevice(deviceId);
592 device->configuration.addProperty(key, value);
593 }
594
595 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
596 Device* device = getDevice(deviceId);
597 device->configuration.addAll(configuration);
598 }
599
600 void addAbsoluteAxis(int32_t deviceId, int axis,
601 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
602 Device* device = getDevice(deviceId);
603
604 RawAbsoluteAxisInfo info;
605 info.valid = true;
606 info.minValue = minValue;
607 info.maxValue = maxValue;
608 info.flat = flat;
609 info.fuzz = fuzz;
610 info.resolution = resolution;
611 device->absoluteAxes.add(axis, info);
612 }
613
614 void addRelativeAxis(int32_t deviceId, int32_t axis) {
615 Device* device = getDevice(deviceId);
616 device->relativeAxes.add(axis, true);
617 }
618
619 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
620 Device* device = getDevice(deviceId);
621 device->keyCodeStates.replaceValueFor(keyCode, state);
622 }
623
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000624 void setCountryCode(int32_t deviceId, InputDeviceCountryCode countryCode) {
625 Device* device = getDevice(deviceId);
626 device->countryCode = countryCode;
627 }
628
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
630 Device* device = getDevice(deviceId);
631 device->scanCodeStates.replaceValueFor(scanCode, state);
632 }
633
634 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
635 Device* device = getDevice(deviceId);
636 device->switchStates.replaceValueFor(switchCode, state);
637 }
638
639 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
640 Device* device = getDevice(deviceId);
641 device->absoluteAxisValue.replaceValueFor(axis, value);
642 }
643
644 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
645 int32_t keyCode, uint32_t flags) {
646 Device* device = getDevice(deviceId);
647 KeyInfo info;
648 info.keyCode = keyCode;
649 info.flags = flags;
650 if (scanCode) {
651 device->keysByScanCode.add(scanCode, info);
652 }
653 if (usageCode) {
654 device->keysByUsageCode.add(usageCode, info);
655 }
656 }
657
Philip Junker4af3b3d2021-12-14 10:36:55 +0100658 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
659 Device* device = getDevice(deviceId);
660 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
661 }
662
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 void addLed(int32_t deviceId, int32_t led, bool initialState) {
664 Device* device = getDevice(deviceId);
665 device->leds.add(led, initialState);
666 }
667
Chris Yef59a2f42020-10-16 12:55:26 -0700668 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
669 int32_t sensorDataIndex) {
670 Device* device = getDevice(deviceId);
671 SensorInfo info;
672 info.sensorType = sensorType;
673 info.sensorDataIndex = sensorDataIndex;
674 device->sensorsByAbsCode.emplace(absCode, info);
675 }
676
677 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
678 Device* device = getDevice(deviceId);
679 typename BitArray<MSC_MAX>::Buffer buffer;
680 buffer[mscEvent / 32] = 1 << mscEvent % 32;
681 device->mscBitmask.loadFromBuffer(buffer);
682 }
683
Chris Ye3fdbfef2021-01-06 18:45:18 -0800684 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
685 mRawLightInfos.emplace(rawId, std::move(info));
686 }
687
688 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
689 mLightBrightness.emplace(rawId, brightness);
690 }
691
692 void fakeLightIntensities(int32_t rawId,
693 const std::unordered_map<LightColor, int32_t> intensities) {
694 mLightIntensities.emplace(rawId, std::move(intensities));
695 }
696
Michael Wrightd02c5b62014-02-10 15:10:22 -0800697 bool getLedState(int32_t deviceId, int32_t led) {
698 Device* device = getDevice(deviceId);
699 return device->leds.valueFor(led);
700 }
701
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100702 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703 return mExcludedDevices;
704 }
705
706 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
707 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800708 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800709 }
710
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000711 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
712 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700713 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800714 RawEvent event;
715 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000716 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800717 event.deviceId = deviceId;
718 event.type = type;
719 event.code = code;
720 event.value = value;
721 mEvents.push_back(event);
722
723 if (type == EV_ABS) {
724 setAbsoluteAxisValue(deviceId, code, value);
725 }
726 }
727
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600728 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
729 std::vector<TouchVideoFrame>> videoFrames) {
730 mVideoFrames = std::move(videoFrames);
731 }
732
Michael Wrightd02c5b62014-02-10 15:10:22 -0800733 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700734 std::unique_lock<std::mutex> lock(mLock);
735 base::ScopedLockAssertion assumeLocked(mLock);
736 const bool queueIsEmpty =
737 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
738 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
739 if (!queueIsEmpty) {
740 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
741 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 }
743
744private:
745 Device* getDevice(int32_t deviceId) const {
746 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100747 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800748 }
749
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700750 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800751 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700752 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800753 }
754
Chris Yea52ade12020-08-27 16:49:20 -0700755 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800756 Device* device = getDevice(deviceId);
757 return device ? device->identifier : InputDeviceIdentifier();
758 }
759
Chris Yea52ade12020-08-27 16:49:20 -0700760 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800761
Chris Yea52ade12020-08-27 16:49:20 -0700762 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800763 Device* device = getDevice(deviceId);
764 if (device) {
765 *outConfiguration = device->configuration;
766 }
767 }
768
Chris Yea52ade12020-08-27 16:49:20 -0700769 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
770 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800771 Device* device = getDevice(deviceId);
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700772 if (device) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800773 ssize_t index = device->absoluteAxes.indexOfKey(axis);
774 if (index >= 0) {
775 *outAxisInfo = device->absoluteAxes.valueAt(index);
776 return OK;
777 }
778 }
779 outAxisInfo->clear();
780 return -1;
781 }
782
Chris Yea52ade12020-08-27 16:49:20 -0700783 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800784 Device* device = getDevice(deviceId);
785 if (device) {
786 return device->relativeAxes.indexOfKey(axis) >= 0;
787 }
788 return false;
789 }
790
Chris Yea52ade12020-08-27 16:49:20 -0700791 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800792
Chris Yef59a2f42020-10-16 12:55:26 -0700793 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
794 Device* device = getDevice(deviceId);
795 if (device) {
796 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
797 }
798 return false;
799 }
800
Chris Yea52ade12020-08-27 16:49:20 -0700801 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
802 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800803 Device* device = getDevice(deviceId);
804 if (device) {
805 const KeyInfo* key = getKey(device, scanCode, usageCode);
806 if (key) {
807 if (outKeycode) {
808 *outKeycode = key->keyCode;
809 }
810 if (outFlags) {
811 *outFlags = key->flags;
812 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700813 if (outMetaState) {
814 *outMetaState = metaState;
815 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800816 return OK;
817 }
818 }
819 return NAME_NOT_FOUND;
820 }
821
822 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
823 if (usageCode) {
824 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
825 if (index >= 0) {
826 return &device->keysByUsageCode.valueAt(index);
827 }
828 }
829 if (scanCode) {
830 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
831 if (index >= 0) {
832 return &device->keysByScanCode.valueAt(index);
833 }
834 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700835 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836 }
837
Chris Yea52ade12020-08-27 16:49:20 -0700838 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800839
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000840 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
841 int32_t deviceId, int32_t absCode) const override {
Chris Yef59a2f42020-10-16 12:55:26 -0700842 Device* device = getDevice(deviceId);
843 if (!device) {
844 return Errorf("Sensor device not found.");
845 }
846 auto it = device->sensorsByAbsCode.find(absCode);
847 if (it == device->sensorsByAbsCode.end()) {
848 return Errorf("Sensor map not found.");
849 }
850 const SensorInfo& info = it->second;
851 return std::make_pair(info.sensorType, info.sensorDataIndex);
852 }
853
Chris Yea52ade12020-08-27 16:49:20 -0700854 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800855 mExcludedDevices = devices;
856 }
857
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700858 std::vector<RawEvent> getEvents(int) override {
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000859 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700861 std::vector<RawEvent> buffer;
862 std::swap(buffer, mEvents);
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000863
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700864 mEventsCondition.notify_all();
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700865 return buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800866 }
867
Chris Yea52ade12020-08-27 16:49:20 -0700868 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600869 auto it = mVideoFrames.find(deviceId);
870 if (it != mVideoFrames.end()) {
871 std::vector<TouchVideoFrame> frames = std::move(it->second);
872 mVideoFrames.erase(deviceId);
873 return frames;
874 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800875 return {};
876 }
877
Chris Yea52ade12020-08-27 16:49:20 -0700878 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800879 Device* device = getDevice(deviceId);
880 if (device) {
881 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
882 if (index >= 0) {
883 return device->scanCodeStates.valueAt(index);
884 }
885 }
886 return AKEY_STATE_UNKNOWN;
887 }
888
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000889 InputDeviceCountryCode getCountryCode(int32_t deviceId) const override {
890 Device* device = getDevice(deviceId);
891 if (device) {
892 return device->countryCode;
893 }
894 return InputDeviceCountryCode::INVALID;
895 }
896
Chris Yea52ade12020-08-27 16:49:20 -0700897 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898 Device* device = getDevice(deviceId);
899 if (device) {
900 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
901 if (index >= 0) {
902 return device->keyCodeStates.valueAt(index);
903 }
904 }
905 return AKEY_STATE_UNKNOWN;
906 }
907
Chris Yea52ade12020-08-27 16:49:20 -0700908 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909 Device* device = getDevice(deviceId);
910 if (device) {
911 ssize_t index = device->switchStates.indexOfKey(sw);
912 if (index >= 0) {
913 return device->switchStates.valueAt(index);
914 }
915 }
916 return AKEY_STATE_UNKNOWN;
917 }
918
Chris Yea52ade12020-08-27 16:49:20 -0700919 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
920 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921 Device* device = getDevice(deviceId);
922 if (device) {
923 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
924 if (index >= 0) {
925 *outValue = device->absoluteAxisValue.valueAt(index);
926 return OK;
927 }
928 }
929 *outValue = 0;
930 return -1;
931 }
932
Philip Junker4af3b3d2021-12-14 10:36:55 +0100933 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
934 Device* device = getDevice(deviceId);
935 if (!device) {
936 return AKEYCODE_UNKNOWN;
937 }
938 auto it = device->keyCodeMapping.find(locationKeyCode);
939 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
940 }
941
Chris Yea52ade12020-08-27 16:49:20 -0700942 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700943 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700944 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945 bool result = false;
946 Device* device = getDevice(deviceId);
947 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700948 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700949 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800950 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
951 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
952 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953 }
954 }
955 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
956 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
957 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958 }
959 }
960 }
961 }
962 return result;
963 }
964
Chris Yea52ade12020-08-27 16:49:20 -0700965 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800966 Device* device = getDevice(deviceId);
967 if (device) {
968 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
969 return index >= 0;
970 }
971 return false;
972 }
973
Arthur Hungcb40a002021-08-03 14:31:01 +0000974 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
975 Device* device = getDevice(deviceId);
976 if (!device) {
977 return false;
978 }
979 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
980 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
981 return true;
982 }
983 }
984 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
985 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
986 return true;
987 }
988 }
989 return false;
990 }
991
Chris Yea52ade12020-08-27 16:49:20 -0700992 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800993 Device* device = getDevice(deviceId);
994 return device && device->leds.indexOfKey(led) >= 0;
995 }
996
Chris Yea52ade12020-08-27 16:49:20 -0700997 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800998 Device* device = getDevice(deviceId);
999 if (device) {
1000 ssize_t index = device->leds.indexOfKey(led);
1001 if (index >= 0) {
1002 device->leds.replaceValueAt(led, on);
1003 } else {
1004 ADD_FAILURE()
1005 << "Attempted to set the state of an LED that the EventHub declared "
1006 "was not present. led=" << led;
1007 }
1008 }
1009 }
1010
Chris Yea52ade12020-08-27 16:49:20 -07001011 void getVirtualKeyDefinitions(
1012 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001013 outVirtualKeys.clear();
1014
1015 Device* device = getDevice(deviceId);
1016 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001017 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001018 }
1019 }
1020
Chris Yea52ade12020-08-27 16:49:20 -07001021 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -07001022 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001023 }
1024
Chris Yea52ade12020-08-27 16:49:20 -07001025 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001026 return false;
1027 }
1028
Chris Yea52ade12020-08-27 16:49:20 -07001029 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001030
Chris Yea52ade12020-08-27 16:49:20 -07001031 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001032
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001033 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return mVibrators; };
Chris Ye87143712020-11-10 05:05:58 +00001034
Chris Yee2b1e5c2021-03-10 22:45:12 -08001035 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
1036 return BATTERY_CAPACITY;
1037 }
Kim Low03ea0352020-11-06 12:45:07 -08001038
Chris Yee2b1e5c2021-03-10 22:45:12 -08001039 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
1040 return BATTERY_STATUS;
1041 }
1042
Andy Chenf9f1a022022-08-29 20:07:10 -04001043 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override {
1044 return {DEFAULT_BATTERY};
1045 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001046
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001047 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
1048 int32_t batteryId) const override {
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001049 if (batteryId != DEFAULT_BATTERY) return {};
1050 static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY,
1051 .name = "default battery",
1052 .flags = InputBatteryClass::CAPACITY,
1053 .path = BATTERY_DEVPATH};
1054 return BATTERY_INFO;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001055 }
Kim Low03ea0352020-11-06 12:45:07 -08001056
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001057 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001058 std::vector<int32_t> ids;
1059 for (const auto& [rawId, info] : mRawLightInfos) {
1060 ids.push_back(rawId);
1061 }
1062 return ids;
1063 }
1064
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001065 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001066 auto it = mRawLightInfos.find(lightId);
1067 if (it == mRawLightInfos.end()) {
1068 return std::nullopt;
1069 }
1070 return it->second;
1071 }
1072
1073 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1074 mLightBrightness.emplace(lightId, brightness);
1075 }
1076
1077 void setLightIntensities(int32_t deviceId, int32_t lightId,
1078 std::unordered_map<LightColor, int32_t> intensities) override {
1079 mLightIntensities.emplace(lightId, intensities);
1080 };
1081
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001082 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001083 auto lightIt = mLightBrightness.find(lightId);
1084 if (lightIt == mLightBrightness.end()) {
1085 return std::nullopt;
1086 }
1087 return lightIt->second;
1088 }
1089
1090 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001091 int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001092 auto lightIt = mLightIntensities.find(lightId);
1093 if (lightIt == mLightIntensities.end()) {
1094 return std::nullopt;
1095 }
1096 return lightIt->second;
1097 };
1098
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001099 void dump(std::string&) const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001100
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001101 void monitor() const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102
Chris Yea52ade12020-08-27 16:49:20 -07001103 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001104
Chris Yea52ade12020-08-27 16:49:20 -07001105 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001106};
1107
Michael Wrightd02c5b62014-02-10 15:10:22 -08001108// --- FakeInputMapper ---
1109
1110class FakeInputMapper : public InputMapper {
1111 uint32_t mSources;
1112 int32_t mKeyboardType;
1113 int32_t mMetaState;
1114 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1115 KeyedVector<int32_t, int32_t> mScanCodeStates;
1116 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001117 // fake mapping which would normally come from keyCharacterMap
1118 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001119 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001120
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001121 std::mutex mLock;
1122 std::condition_variable mStateChangedCondition;
1123 bool mConfigureWasCalled GUARDED_BY(mLock);
1124 bool mResetWasCalled GUARDED_BY(mLock);
1125 bool mProcessWasCalled GUARDED_BY(mLock);
1126 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001127
Arthur Hungc23540e2018-11-29 20:42:11 +08001128 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001129public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001130 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1131 : InputMapper(deviceContext),
1132 mSources(sources),
1133 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001134 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001135 mConfigureWasCalled(false),
1136 mResetWasCalled(false),
1137 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001138
Chris Yea52ade12020-08-27 16:49:20 -07001139 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001140
1141 void setKeyboardType(int32_t keyboardType) {
1142 mKeyboardType = keyboardType;
1143 }
1144
1145 void setMetaState(int32_t metaState) {
1146 mMetaState = metaState;
1147 }
1148
1149 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001150 std::unique_lock<std::mutex> lock(mLock);
1151 base::ScopedLockAssertion assumeLocked(mLock);
1152 const bool configureCalled =
1153 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1154 return mConfigureWasCalled;
1155 });
1156 if (!configureCalled) {
1157 FAIL() << "Expected configure() to have been called.";
1158 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001159 mConfigureWasCalled = false;
1160 }
1161
1162 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001163 std::unique_lock<std::mutex> lock(mLock);
1164 base::ScopedLockAssertion assumeLocked(mLock);
1165 const bool resetCalled =
1166 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1167 return mResetWasCalled;
1168 });
1169 if (!resetCalled) {
1170 FAIL() << "Expected reset() to have been called.";
1171 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001172 mResetWasCalled = false;
1173 }
1174
Yi Kong9b14ac62018-07-17 13:48:38 -07001175 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001176 std::unique_lock<std::mutex> lock(mLock);
1177 base::ScopedLockAssertion assumeLocked(mLock);
1178 const bool processCalled =
1179 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1180 return mProcessWasCalled;
1181 });
1182 if (!processCalled) {
1183 FAIL() << "Expected process() to have been called.";
1184 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001185 if (outLastEvent) {
1186 *outLastEvent = mLastEvent;
1187 }
1188 mProcessWasCalled = false;
1189 }
1190
1191 void setKeyCodeState(int32_t keyCode, int32_t state) {
1192 mKeyCodeStates.replaceValueFor(keyCode, state);
1193 }
1194
1195 void setScanCodeState(int32_t scanCode, int32_t state) {
1196 mScanCodeStates.replaceValueFor(scanCode, state);
1197 }
1198
1199 void setSwitchState(int32_t switchCode, int32_t state) {
1200 mSwitchStates.replaceValueFor(switchCode, state);
1201 }
1202
1203 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001204 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001205 }
1206
Philip Junker4af3b3d2021-12-14 10:36:55 +01001207 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1208 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1209 }
1210
Michael Wrightd02c5b62014-02-10 15:10:22 -08001211private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001212 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001213
Chris Yea52ade12020-08-27 16:49:20 -07001214 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001215 InputMapper::populateDeviceInfo(deviceInfo);
1216
1217 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1218 deviceInfo->setKeyboardType(mKeyboardType);
1219 }
1220 }
1221
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001222 std::list<NotifyArgs> configure(nsecs_t, const InputReaderConfiguration* config,
1223 uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001224 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001225 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001226
1227 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001228 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001229 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1230 mViewport = config->getDisplayViewportByPort(*displayPort);
1231 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001232
1233 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001234 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001235 }
1236
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001237 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001238 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001239 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001240 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001241 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001242 }
1243
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001244 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001245 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001246 mLastEvent = *rawEvent;
1247 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001248 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001249 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001250 }
1251
Chris Yea52ade12020-08-27 16:49:20 -07001252 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001253 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1254 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1255 }
1256
Philip Junker4af3b3d2021-12-14 10:36:55 +01001257 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1258 auto it = mKeyCodeMapping.find(locationKeyCode);
1259 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1260 }
1261
Chris Yea52ade12020-08-27 16:49:20 -07001262 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001263 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1264 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1265 }
1266
Chris Yea52ade12020-08-27 16:49:20 -07001267 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001268 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1269 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1270 }
1271
Chris Yea52ade12020-08-27 16:49:20 -07001272 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001273 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001274 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001275 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001276 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1277 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1278 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001279 }
1280 }
1281 }
Chris Yea52ade12020-08-27 16:49:20 -07001282 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001283 return result;
1284 }
1285
1286 virtual int32_t getMetaState() {
1287 return mMetaState;
1288 }
1289
1290 virtual void fadePointer() {
1291 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001292
1293 virtual std::optional<int32_t> getAssociatedDisplay() {
1294 if (mViewport) {
1295 return std::make_optional(mViewport->displayId);
1296 }
1297 return std::nullopt;
1298 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001299};
1300
1301
1302// --- InstrumentedInputReader ---
1303
1304class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001305 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001306
1307public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001308 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1309 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001310 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001311 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001312
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001313 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001314
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001315 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001316
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001317 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001318 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001319 InputDeviceIdentifier identifier;
1320 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001321 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001322 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001323 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001324 }
1325
Prabir Pradhan28efc192019-11-05 01:10:04 +00001326 // Make the protected loopOnce method accessible to tests.
1327 using InputReader::loopOnce;
1328
Michael Wrightd02c5b62014-02-10 15:10:22 -08001329protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001330 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1331 const InputDeviceIdentifier& identifier)
1332 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001333 if (!mNextDevices.empty()) {
1334 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1335 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001336 return device;
1337 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001338 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001339 }
1340
arthurhungdcef2dc2020-08-11 14:47:50 +08001341 // --- FakeInputReaderContext ---
1342 class FakeInputReaderContext : public ContextImpl {
1343 int32_t mGlobalMetaState;
1344 bool mUpdateGlobalMetaStateWasCalled;
1345 int32_t mGeneration;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00001346 std::optional<nsecs_t> mRequestedTimeout;
1347 std::vector<InputDeviceInfo> mExternalStylusDevices;
arthurhungdcef2dc2020-08-11 14:47:50 +08001348
1349 public:
1350 FakeInputReaderContext(InputReader* reader)
1351 : ContextImpl(reader),
1352 mGlobalMetaState(0),
1353 mUpdateGlobalMetaStateWasCalled(false),
1354 mGeneration(1) {}
1355
1356 virtual ~FakeInputReaderContext() {}
1357
1358 void assertUpdateGlobalMetaStateWasCalled() {
1359 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1360 << "Expected updateGlobalMetaState() to have been called.";
1361 mUpdateGlobalMetaStateWasCalled = false;
1362 }
1363
1364 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1365
1366 uint32_t getGeneration() { return mGeneration; }
1367
1368 void updateGlobalMetaState() override {
1369 mUpdateGlobalMetaStateWasCalled = true;
1370 ContextImpl::updateGlobalMetaState();
1371 }
1372
1373 int32_t getGlobalMetaState() override {
1374 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1375 }
1376
1377 int32_t bumpGeneration() override {
1378 mGeneration = ContextImpl::bumpGeneration();
1379 return mGeneration;
1380 }
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00001381
1382 void requestTimeoutAtTime(nsecs_t when) override { mRequestedTimeout = when; }
1383
1384 void assertTimeoutWasRequested(nsecs_t when) {
1385 ASSERT_TRUE(mRequestedTimeout) << "Expected timeout at time " << when
1386 << " but there was no timeout requested.";
1387 ASSERT_EQ(when, *mRequestedTimeout);
1388 mRequestedTimeout.reset();
1389 }
1390
1391 void assertTimeoutWasNotRequested() {
1392 ASSERT_FALSE(mRequestedTimeout) << "Expected no timeout to have been requested,"
1393 " but one was requested at time "
1394 << *mRequestedTimeout;
1395 }
1396
1397 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override {
1398 outDevices = mExternalStylusDevices;
1399 }
1400
1401 void setExternalStylusDevices(std::vector<InputDeviceInfo>&& devices) {
1402 mExternalStylusDevices = devices;
1403 }
arthurhungdcef2dc2020-08-11 14:47:50 +08001404 } mFakeContext;
1405
Michael Wrightd02c5b62014-02-10 15:10:22 -08001406 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001407
1408public:
1409 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001410};
1411
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001412// --- InputReaderPolicyTest ---
1413class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001414protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001415 sp<FakeInputReaderPolicy> mFakePolicy;
1416
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001417 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001418 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001419};
1420
1421/**
1422 * Check that empty set of viewports is an acceptable configuration.
1423 * Also try to get internal viewport two different ways - by type and by uniqueId.
1424 *
1425 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1426 * Such configuration is not currently allowed.
1427 */
1428TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001429 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001430
1431 // We didn't add any viewports yet, so there shouldn't be any.
1432 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001433 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001434 ASSERT_FALSE(internalViewport);
1435
1436 // Add an internal viewport, then clear it
1437 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001438 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001439 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001440
1441 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001442 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001443 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001444 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001445
1446 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001447 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001448 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001449 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001450
1451 mFakePolicy->clearViewports();
1452 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001453 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001454 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001455 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001456 ASSERT_FALSE(internalViewport);
1457}
1458
1459TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1460 const std::string internalUniqueId = "local:0";
1461 const std::string externalUniqueId = "local:1";
1462 const std::string virtualUniqueId1 = "virtual:2";
1463 const std::string virtualUniqueId2 = "virtual:3";
1464 constexpr int32_t virtualDisplayId1 = 2;
1465 constexpr int32_t virtualDisplayId2 = 3;
1466
1467 // Add an internal viewport
1468 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001469 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1470 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001471 // Add an external viewport
1472 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001473 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1474 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001475 // Add an virtual viewport
1476 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001477 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1478 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001479 // Add another virtual viewport
1480 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001481 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1482 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001483
1484 // Check matching by type for internal
1485 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001486 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001487 ASSERT_TRUE(internalViewport);
1488 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1489
1490 // Check matching by type for external
1491 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001492 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001493 ASSERT_TRUE(externalViewport);
1494 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1495
1496 // Check matching by uniqueId for virtual viewport #1
1497 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001498 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001499 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001500 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001501 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1502 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1503
1504 // Check matching by uniqueId for virtual viewport #2
1505 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001506 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001507 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001508 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001509 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1510 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1511}
1512
1513
1514/**
1515 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1516 * that lookup works by checking display id.
1517 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1518 */
1519TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1520 const std::string uniqueId1 = "uniqueId1";
1521 const std::string uniqueId2 = "uniqueId2";
1522 constexpr int32_t displayId1 = 2;
1523 constexpr int32_t displayId2 = 3;
1524
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001525 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1526 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001527 for (const ViewportType& type : types) {
1528 mFakePolicy->clearViewports();
1529 // Add a viewport
1530 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001531 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1532 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001533 // Add another viewport
1534 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001535 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1536 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001537
1538 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001539 std::optional<DisplayViewport> viewport1 =
1540 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001541 ASSERT_TRUE(viewport1);
1542 ASSERT_EQ(displayId1, viewport1->displayId);
1543 ASSERT_EQ(type, viewport1->type);
1544
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001545 std::optional<DisplayViewport> viewport2 =
1546 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001547 ASSERT_TRUE(viewport2);
1548 ASSERT_EQ(displayId2, viewport2->displayId);
1549 ASSERT_EQ(type, viewport2->type);
1550
1551 // When there are multiple viewports of the same kind, and uniqueId is not specified
1552 // in the call to getDisplayViewport, then that situation is not supported.
1553 // The viewports can be stored in any order, so we cannot rely on the order, since that
1554 // is just implementation detail.
1555 // However, we can check that it still returns *a* viewport, we just cannot assert
1556 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001557 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001558 ASSERT_TRUE(someViewport);
1559 }
1560}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001561
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001562/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001563 * When we have multiple internal displays make sure we always return the default display when
1564 * querying by type.
1565 */
1566TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1567 const std::string uniqueId1 = "uniqueId1";
1568 const std::string uniqueId2 = "uniqueId2";
1569 constexpr int32_t nonDefaultDisplayId = 2;
1570 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1571 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1572
1573 // Add the default display first and ensure it gets returned.
1574 mFakePolicy->clearViewports();
1575 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001576 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001577 ViewportType::INTERNAL);
1578 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001579 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001580 ViewportType::INTERNAL);
1581
1582 std::optional<DisplayViewport> viewport =
1583 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1584 ASSERT_TRUE(viewport);
1585 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1586 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1587
1588 // Add the default display second to make sure order doesn't matter.
1589 mFakePolicy->clearViewports();
1590 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001591 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001592 ViewportType::INTERNAL);
1593 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001594 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001595 ViewportType::INTERNAL);
1596
1597 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1598 ASSERT_TRUE(viewport);
1599 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1600 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1601}
1602
1603/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001604 * Check getDisplayViewportByPort
1605 */
1606TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001607 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001608 const std::string uniqueId1 = "uniqueId1";
1609 const std::string uniqueId2 = "uniqueId2";
1610 constexpr int32_t displayId1 = 1;
1611 constexpr int32_t displayId2 = 2;
1612 const uint8_t hdmi1 = 0;
1613 const uint8_t hdmi2 = 1;
1614 const uint8_t hdmi3 = 2;
1615
1616 mFakePolicy->clearViewports();
1617 // Add a viewport that's associated with some display port that's not of interest.
1618 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001619 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1620 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001621 // Add another viewport, connected to HDMI1 port
1622 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001623 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1624 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001625
1626 // Check that correct display viewport was returned by comparing the display ports.
1627 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1628 ASSERT_TRUE(hdmi1Viewport);
1629 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1630 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1631
1632 // Check that we can still get the same viewport using the uniqueId
1633 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1634 ASSERT_TRUE(hdmi1Viewport);
1635 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1636 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1637 ASSERT_EQ(type, hdmi1Viewport->type);
1638
1639 // Check that we cannot find a port with "HDMI2", because we never added one
1640 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1641 ASSERT_FALSE(hdmi2Viewport);
1642}
1643
Michael Wrightd02c5b62014-02-10 15:10:22 -08001644// --- InputReaderTest ---
1645
1646class InputReaderTest : public testing::Test {
1647protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001648 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001649 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001650 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001651 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001652
Chris Yea52ade12020-08-27 16:49:20 -07001653 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001654 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001655 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001656 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001657
Prabir Pradhan28efc192019-11-05 01:10:04 +00001658 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001659 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001660 }
1661
Chris Yea52ade12020-08-27 16:49:20 -07001662 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001663 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001664 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001665 }
1666
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001667 void addDevice(int32_t eventHubId, const std::string& name,
1668 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001669 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001670
1671 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001672 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001673 }
1674 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001675 mReader->loopOnce();
1676 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001677 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1678 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001679 }
1680
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001681 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001682 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001683 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001684 }
1685
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001686 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001687 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001688 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001689 }
1690
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001691 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001692 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001693 ftl::Flags<InputDeviceClass> classes,
1694 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001695 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001696 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1697 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001698 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001699 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001700 return mapper;
1701 }
1702};
1703
Chris Ye98d3f532020-10-01 21:48:59 -07001704TEST_F(InputReaderTest, PolicyGetInputDevices) {
1705 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001706 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001707 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001708
1709 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001710 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001711 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001712 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001713 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001714 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1715 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001716 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001717}
1718
Chris Yee7310032020-09-22 15:36:28 -07001719TEST_F(InputReaderTest, GetMergedInputDevices) {
1720 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1721 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1722 // Add two subdevices to device
1723 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1724 // Must add at least one mapper or the device will be ignored!
1725 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1726 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1727
1728 // Push same device instance for next device to be added, so they'll have same identifier.
1729 mReader->pushNextDevice(device);
1730 mReader->pushNextDevice(device);
1731 ASSERT_NO_FATAL_FAILURE(
1732 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1733 ASSERT_NO_FATAL_FAILURE(
1734 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1735
1736 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001737 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001738}
1739
Chris Yee14523a2020-12-19 13:46:00 -08001740TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1741 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1742 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1743 // Add two subdevices to device
1744 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1745 // Must add at least one mapper or the device will be ignored!
1746 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1747 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1748
1749 // Push same device instance for next device to be added, so they'll have same identifier.
1750 mReader->pushNextDevice(device);
1751 mReader->pushNextDevice(device);
1752 // Sensor device is initially disabled
1753 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1754 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1755 nullptr));
1756 // Device is disabled because the only sub device is a sensor device and disabled initially.
1757 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1758 ASSERT_FALSE(device->isEnabled());
1759 ASSERT_NO_FATAL_FAILURE(
1760 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1761 // The merged device is enabled if any sub device is enabled
1762 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1763 ASSERT_TRUE(device->isEnabled());
1764}
1765
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001766TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001767 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001768 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001769 constexpr int32_t eventHubId = 1;
1770 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001771 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001772 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001773 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001774 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001775
Yi Kong9b14ac62018-07-17 13:48:38 -07001776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001777
1778 NotifyDeviceResetArgs resetArgs;
1779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001780 ASSERT_EQ(deviceId, resetArgs.deviceId);
1781
1782 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001783 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001784 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001785
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001787 ASSERT_EQ(deviceId, resetArgs.deviceId);
1788 ASSERT_EQ(device->isEnabled(), false);
1789
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001790 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001791 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001794 ASSERT_EQ(device->isEnabled(), false);
1795
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001796 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001797 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001799 ASSERT_EQ(deviceId, resetArgs.deviceId);
1800 ASSERT_EQ(device->isEnabled(), true);
1801}
1802
Michael Wrightd02c5b62014-02-10 15:10:22 -08001803TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001804 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001805 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001806 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001807 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001808 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001809 AINPUT_SOURCE_KEYBOARD, nullptr);
1810 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001811
1812 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1813 AINPUT_SOURCE_ANY, AKEYCODE_A))
1814 << "Should return unknown when the device id is >= 0 but unknown.";
1815
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001816 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1817 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1818 << "Should return unknown when the device id is valid but the sources are not "
1819 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001820
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001821 ASSERT_EQ(AKEY_STATE_DOWN,
1822 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1823 AKEYCODE_A))
1824 << "Should return value provided by mapper when device id is valid and the device "
1825 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001826
1827 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1828 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1829 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1830
1831 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1832 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1833 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1834}
1835
Philip Junker4af3b3d2021-12-14 10:36:55 +01001836TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1837 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1838 constexpr int32_t eventHubId = 1;
1839 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1840 InputDeviceClass::KEYBOARD,
1841 AINPUT_SOURCE_KEYBOARD, nullptr);
1842 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1843
1844 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1845 << "Should return unknown when the device with the specified id is not found.";
1846
1847 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1848 << "Should return correct mapping when device id is valid and mapping exists.";
1849
1850 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1851 << "Should return the location key code when device id is valid and there's no "
1852 "mapping.";
1853}
1854
1855TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1856 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1857 constexpr int32_t eventHubId = 1;
1858 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1859 InputDeviceClass::JOYSTICK,
1860 AINPUT_SOURCE_GAMEPAD, nullptr);
1861 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1862
1863 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1864 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1865}
1866
Michael Wrightd02c5b62014-02-10 15:10:22 -08001867TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001868 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001869 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001870 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001871 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001872 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001873 AINPUT_SOURCE_KEYBOARD, nullptr);
1874 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001875
1876 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1877 AINPUT_SOURCE_ANY, KEY_A))
1878 << "Should return unknown when the device id is >= 0 but unknown.";
1879
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001880 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1881 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1882 << "Should return unknown when the device id is valid but the sources are not "
1883 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001884
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001885 ASSERT_EQ(AKEY_STATE_DOWN,
1886 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1887 KEY_A))
1888 << "Should return value provided by mapper when device id is valid and the device "
1889 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001890
1891 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1892 AINPUT_SOURCE_TRACKBALL, KEY_A))
1893 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1894
1895 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1896 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1897 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1898}
1899
1900TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001901 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001902 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001903 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001904 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001905 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001906 AINPUT_SOURCE_KEYBOARD, nullptr);
1907 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001908
1909 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1910 AINPUT_SOURCE_ANY, SW_LID))
1911 << "Should return unknown when the device id is >= 0 but unknown.";
1912
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001913 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1914 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1915 << "Should return unknown when the device id is valid but the sources are not "
1916 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001917
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001918 ASSERT_EQ(AKEY_STATE_DOWN,
1919 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1920 SW_LID))
1921 << "Should return value provided by mapper when device id is valid and the device "
1922 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001923
1924 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1925 AINPUT_SOURCE_TRACKBALL, SW_LID))
1926 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1927
1928 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1929 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1930 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1931}
1932
1933TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001934 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001935 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001936 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001937 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001938 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001939 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001940
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001941 mapper.addSupportedKeyCode(AKEYCODE_A);
1942 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001943
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001944 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001945 uint8_t flags[4] = { 0, 0, 0, 1 };
1946
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001947 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001948 << "Should return false when device id is >= 0 but unknown.";
1949 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1950
1951 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001952 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001953 << "Should return false when device id is valid but the sources are not supported by "
1954 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001955 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1956
1957 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001958 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001959 keyCodes, flags))
1960 << "Should return value provided by mapper when device id is valid and the device "
1961 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001962 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1963
1964 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001965 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1966 << "Should return false when the device id is < 0 but the sources are not supported by "
1967 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001968 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1969
1970 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001971 ASSERT_TRUE(
1972 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1973 << "Should return value provided by mapper when device id is < 0 and one of the "
1974 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001975 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1976}
1977
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001978TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001979 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001980 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001981
1982 NotifyConfigurationChangedArgs args;
1983
1984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1985 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1986}
1987
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001988TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001989 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001990 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001991 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001992 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001993 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001994 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001995 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001996 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001997
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001998 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001999 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002000 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
2001
2002 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002003 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002004 ASSERT_EQ(when, event.when);
2005 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002006 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002007 ASSERT_EQ(EV_KEY, event.type);
2008 ASSERT_EQ(KEY_A, event.code);
2009 ASSERT_EQ(1, event.value);
2010}
2011
Garfield Tan1c7bc862020-01-28 13:24:04 -08002012TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002013 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002014 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002015 constexpr int32_t eventHubId = 1;
2016 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08002017 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002018 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002019 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002020 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08002021
2022 NotifyDeviceResetArgs resetArgs;
2023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002024 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002025
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002026 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002027 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002029 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002030 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002031
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002032 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002033 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002035 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002036 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002037
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002038 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002039 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002041 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002042 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002043}
2044
Garfield Tan1c7bc862020-01-28 13:24:04 -08002045TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
2046 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002047 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002048 constexpr int32_t eventHubId = 1;
2049 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2050 // Must add at least one mapper or the device will be ignored!
2051 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002052 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002053 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
2054
2055 NotifyDeviceResetArgs resetArgs;
2056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2057 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
2058}
2059
Arthur Hungc23540e2018-11-29 20:42:11 +08002060TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002061 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002062 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002063 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08002064 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002065 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2066 FakeInputMapper& mapper =
2067 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002068 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002069
2070 const uint8_t hdmi1 = 1;
2071
2072 // Associated touch screen with second display.
2073 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2074
2075 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002076 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002077 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002078 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002079 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002080 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002081 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002082 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002083 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002084 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002085
2086 // Add the device, and make sure all of the callbacks are triggered.
2087 // The device is added after the input port associations are processed since
2088 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002089 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002092 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002093
Arthur Hung2c9a3342019-07-23 14:18:59 +08002094 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002095 ASSERT_EQ(deviceId, device->getId());
2096 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2097 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002098
2099 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002100 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002101 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002102 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002103}
2104
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002105TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2106 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002107 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002108 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2109 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2110 // Must add at least one mapper or the device will be ignored!
2111 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2112 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2113 mReader->pushNextDevice(device);
2114 mReader->pushNextDevice(device);
2115 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2116 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2117
2118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2119
2120 NotifyDeviceResetArgs resetArgs;
2121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2122 ASSERT_EQ(deviceId, resetArgs.deviceId);
2123 ASSERT_TRUE(device->isEnabled());
2124 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2125 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2126
2127 disableDevice(deviceId);
2128 mReader->loopOnce();
2129
2130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2131 ASSERT_EQ(deviceId, resetArgs.deviceId);
2132 ASSERT_FALSE(device->isEnabled());
2133 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2134 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2135
2136 enableDevice(deviceId);
2137 mReader->loopOnce();
2138
2139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2140 ASSERT_EQ(deviceId, resetArgs.deviceId);
2141 ASSERT_TRUE(device->isEnabled());
2142 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2143 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2144}
2145
2146TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2147 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002148 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002149 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2150 // Add two subdevices to device
2151 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2152 FakeInputMapper& mapperDevice1 =
2153 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2154 FakeInputMapper& mapperDevice2 =
2155 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2156 mReader->pushNextDevice(device);
2157 mReader->pushNextDevice(device);
2158 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2159 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2160
2161 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2162 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2163
2164 ASSERT_EQ(AKEY_STATE_DOWN,
2165 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2166 ASSERT_EQ(AKEY_STATE_DOWN,
2167 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2168 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2169 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2170}
2171
Prabir Pradhan7e186182020-11-10 13:56:45 -08002172TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2173 NotifyPointerCaptureChangedArgs args;
2174
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002175 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002176 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2177 mReader->loopOnce();
2178 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002179 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2180 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002181
2182 mFakePolicy->setPointerCapture(false);
2183 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2184 mReader->loopOnce();
2185 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002186 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002187
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002188 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002189 // does not change.
2190 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2191 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002192 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002193}
2194
Chris Ye87143712020-11-10 05:05:58 +00002195class FakeVibratorInputMapper : public FakeInputMapper {
2196public:
2197 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2198 : FakeInputMapper(deviceContext, sources) {}
2199
2200 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2201};
2202
2203TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2204 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002205 ftl::Flags<InputDeviceClass> deviceClass =
2206 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002207 constexpr int32_t eventHubId = 1;
2208 const char* DEVICE_LOCATION = "BLUETOOTH";
2209 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2210 FakeVibratorInputMapper& mapper =
2211 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2212 mReader->pushNextDevice(device);
2213
2214 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2215 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2216
2217 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2218 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2219}
2220
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002221// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002222
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002223class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002224public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002225 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002226
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002227 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002228
Andy Chenf9f1a022022-08-29 20:07:10 -04002229 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2230
Chris Yee2b1e5c2021-03-10 22:45:12 -08002231 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2232
2233 void dump(std::string& dump) override {}
2234
2235 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2236 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002237 }
2238
Chris Yee2b1e5c2021-03-10 22:45:12 -08002239 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2240 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002241 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002242
2243 bool setLightColor(int32_t lightId, int32_t color) override {
2244 getDeviceContext().setLightBrightness(lightId, color >> 24);
2245 return true;
2246 }
2247
2248 std::optional<int32_t> getLightColor(int32_t lightId) override {
2249 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2250 if (!result.has_value()) {
2251 return std::nullopt;
2252 }
2253 return result.value() << 24;
2254 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002255
2256 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2257
2258 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2259
2260private:
2261 InputDeviceContext& mDeviceContext;
2262 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2263 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04002264 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002265};
2266
Chris Yee2b1e5c2021-03-10 22:45:12 -08002267TEST_F(InputReaderTest, BatteryGetCapacity) {
2268 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002269 ftl::Flags<InputDeviceClass> deviceClass =
2270 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002271 constexpr int32_t eventHubId = 1;
2272 const char* DEVICE_LOCATION = "BLUETOOTH";
2273 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002274 FakePeripheralController& controller =
2275 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002276 mReader->pushNextDevice(device);
2277
2278 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2279
2280 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2281 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2282}
2283
2284TEST_F(InputReaderTest, BatteryGetStatus) {
2285 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002286 ftl::Flags<InputDeviceClass> deviceClass =
2287 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002288 constexpr int32_t eventHubId = 1;
2289 const char* DEVICE_LOCATION = "BLUETOOTH";
2290 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002291 FakePeripheralController& controller =
2292 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002293 mReader->pushNextDevice(device);
2294
2295 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2296
2297 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2298 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2299}
2300
Prabir Pradhane287ecd2022-09-07 21:18:05 +00002301TEST_F(InputReaderTest, BatteryGetDevicePath) {
2302 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2303 ftl::Flags<InputDeviceClass> deviceClass =
2304 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2305 constexpr int32_t eventHubId = 1;
2306 const char* DEVICE_LOCATION = "BLUETOOTH";
2307 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2308 device->addController<FakePeripheralController>(eventHubId);
2309 mReader->pushNextDevice(device);
2310
2311 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2312
2313 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), BATTERY_DEVPATH);
2314}
2315
Chris Ye3fdbfef2021-01-06 18:45:18 -08002316TEST_F(InputReaderTest, LightGetColor) {
2317 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002318 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002319 constexpr int32_t eventHubId = 1;
2320 const char* DEVICE_LOCATION = "BLUETOOTH";
2321 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002322 FakePeripheralController& controller =
2323 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002324 mReader->pushNextDevice(device);
2325 RawLightInfo info = {.id = 1,
2326 .name = "Mono",
2327 .maxBrightness = 255,
2328 .flags = InputLightClass::BRIGHTNESS,
2329 .path = ""};
2330 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2331 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2332
2333 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002334
Chris Yee2b1e5c2021-03-10 22:45:12 -08002335 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2336 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002337 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2338 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2339}
2340
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002341// --- InputReaderIntegrationTest ---
2342
2343// These tests create and interact with the InputReader only through its interface.
2344// The InputReader is started during SetUp(), which starts its processing in its own
2345// thread. The tests use linux uinput to emulate input devices.
2346// NOTE: Interacting with the physical device while these tests are running may cause
2347// the tests to fail.
2348class InputReaderIntegrationTest : public testing::Test {
2349protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002350 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002351 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002352 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002353
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002354 std::shared_ptr<FakePointerController> mFakePointerController;
2355
Chris Yea52ade12020-08-27 16:49:20 -07002356 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002357#if !defined(__ANDROID__)
2358 GTEST_SKIP();
2359#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002360 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002361 mFakePointerController = std::make_shared<FakePointerController>();
2362 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002363 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2364 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002365
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002366 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2367 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002368 ASSERT_EQ(mReader->start(), OK);
2369
2370 // Since this test is run on a real device, all the input devices connected
2371 // to the test device will show up in mReader. We wait for those input devices to
2372 // show up before beginning the tests.
2373 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2374 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2375 }
2376
Chris Yea52ade12020-08-27 16:49:20 -07002377 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002378#if !defined(__ANDROID__)
2379 return;
2380#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002381 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002382 mReader.reset();
2383 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002384 mFakePolicy.clear();
2385 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00002386
2387 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
2388 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
2389 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
2390 [&name](const InputDeviceInfo& info) {
2391 return info.getIdentifier().name == name;
2392 });
2393 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
2394 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002395};
2396
2397TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2398 // An invalid input device that is only used for this test.
2399 class InvalidUinputDevice : public UinputDevice {
2400 public:
Prabir Pradhanb7d434e2022-10-14 22:41:38 +00002401 InvalidUinputDevice() : UinputDevice("Invalid Device", 99 /*productId*/) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002402
2403 private:
2404 void configureDevice(int fd, uinput_user_dev* device) override {}
2405 };
2406
2407 const size_t numDevices = mFakePolicy->getInputDevices().size();
2408
2409 // UinputDevice does not set any event or key bits, so InputReader should not
2410 // consider it as a valid device.
2411 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2412 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2413 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2414 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2415
2416 invalidDevice.reset();
2417 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2418 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2419 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2420}
2421
2422TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2423 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2424
2425 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2426 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2427 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2428 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2429
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002430 const auto device = findDeviceByName(keyboard->getName());
2431 ASSERT_TRUE(device.has_value());
2432 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
2433 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
2434 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002435
2436 keyboard.reset();
2437 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2438 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2439 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2440}
2441
2442TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2443 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2444 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2445
2446 NotifyConfigurationChangedArgs configChangedArgs;
2447 ASSERT_NO_FATAL_FAILURE(
2448 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002449 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002450 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2451
2452 NotifyKeyArgs keyArgs;
2453 keyboard->pressAndReleaseHomeKey();
2454 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2455 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002456 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002457 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002458 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002459 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002460 prevTimestamp = keyArgs.eventTime;
2461
2462 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2463 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002464 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002465 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002466 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002467}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002468
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002469TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
2470 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2471 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2472
2473 const auto device = findDeviceByName(stylus->getName());
2474 ASSERT_TRUE(device.has_value());
2475
Prabir Pradhana3621852022-10-14 18:57:23 +00002476 // An external stylus with buttons should also be recognized as a keyboard.
2477 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002478 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
2479 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
2480
2481 const auto DOWN =
2482 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
2483 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
2484
2485 stylus->pressAndReleaseKey(BTN_STYLUS);
2486 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2487 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2488 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2489 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2490
2491 stylus->pressAndReleaseKey(BTN_STYLUS2);
2492 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2493 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
2494 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2495 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
2496
2497 stylus->pressAndReleaseKey(BTN_STYLUS3);
2498 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2499 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
2500 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2501 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
2502}
2503
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002504/**
2505 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2506 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2507 * are passed to the listener.
2508 */
2509static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2510TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2511 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2512 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2513 NotifyKeyArgs keyArgs;
2514
2515 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2516 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2517 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2518 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2519
2520 controller->pressAndReleaseKey(BTN_GEAR_UP);
2521 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2522 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2523 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2524}
2525
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002526// --- TouchIntegrationTest ---
2527
Arthur Hungaab25622020-01-16 11:22:11 +08002528class TouchIntegrationTest : public InputReaderIntegrationTest {
2529protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002530 const std::string UNIQUE_ID = "local:0";
2531
Chris Yea52ade12020-08-27 16:49:20 -07002532 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002533#if !defined(__ANDROID__)
2534 GTEST_SKIP();
2535#endif
Arthur Hungaab25622020-01-16 11:22:11 +08002536 InputReaderIntegrationTest::SetUp();
2537 // At least add an internal display.
2538 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2539 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002540 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002541
2542 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2543 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2544 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00002545 const auto info = findDeviceByName(mDevice->getName());
2546 ASSERT_TRUE(info);
2547 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08002548 }
2549
2550 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2551 int32_t orientation, const std::string& uniqueId,
2552 std::optional<uint8_t> physicalPort,
2553 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002554 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2555 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002556 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2557 }
2558
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002559 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2560 NotifyMotionArgs args;
2561 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2562 EXPECT_EQ(action, args.action);
2563 ASSERT_EQ(points.size(), args.pointerCount);
2564 for (size_t i = 0; i < args.pointerCount; i++) {
2565 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2566 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2567 }
2568 }
2569
Arthur Hungaab25622020-01-16 11:22:11 +08002570 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00002571 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08002572};
2573
2574TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2575 NotifyMotionArgs args;
2576 const Point centerPoint = mDevice->getCenterPoint();
2577
2578 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002579 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002580 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002581 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002582 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2583 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2584
2585 // ACTION_MOVE
2586 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002587 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002588 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2589 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2590
2591 // ACTION_UP
2592 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002593 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002594 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2595 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2596}
2597
2598TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2599 NotifyMotionArgs args;
2600 const Point centerPoint = mDevice->getCenterPoint();
2601
2602 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002603 mDevice->sendSlot(FIRST_SLOT);
2604 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002605 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002606 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002607 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2608 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2609
2610 // ACTION_POINTER_DOWN (Second slot)
2611 const Point secondPoint = centerPoint + Point(100, 100);
2612 mDevice->sendSlot(SECOND_SLOT);
2613 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002614 mDevice->sendDown(secondPoint);
2615 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002616 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002617 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002618
2619 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002620 mDevice->sendMove(secondPoint + Point(1, 1));
2621 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002622 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2624
2625 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002626 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002627 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002628 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002629 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002630
2631 // ACTION_UP
2632 mDevice->sendSlot(FIRST_SLOT);
2633 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002634 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002635 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2636 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2637}
2638
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002639/**
2640 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2641 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2642 * data?
2643 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2644 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2645 * for Pointer 0 only is generated after.
2646 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2647 * events, we will not miss any information.
2648 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2649 * event generated afterwards that contains the newest movement of pointer 0.
2650 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2651 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2652 * losing information about non-palm pointers.
2653 */
2654TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2655 NotifyMotionArgs args;
2656 const Point centerPoint = mDevice->getCenterPoint();
2657
2658 // ACTION_DOWN
2659 mDevice->sendSlot(FIRST_SLOT);
2660 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2661 mDevice->sendDown(centerPoint);
2662 mDevice->sendSync();
2663 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2664
2665 // ACTION_POINTER_DOWN (Second slot)
2666 const Point secondPoint = centerPoint + Point(100, 100);
2667 mDevice->sendSlot(SECOND_SLOT);
2668 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2669 mDevice->sendDown(secondPoint);
2670 mDevice->sendSync();
2671 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2672
2673 // ACTION_MOVE (First slot)
2674 mDevice->sendSlot(FIRST_SLOT);
2675 mDevice->sendMove(centerPoint + Point(5, 5));
2676 // ACTION_POINTER_UP (Second slot)
2677 mDevice->sendSlot(SECOND_SLOT);
2678 mDevice->sendPointerUp();
2679 // Send a single sync for the above 2 pointer updates
2680 mDevice->sendSync();
2681
2682 // First, we should get POINTER_UP for the second pointer
2683 assertReceivedMotion(ACTION_POINTER_1_UP,
2684 {/*first pointer */ centerPoint + Point(5, 5),
2685 /*second pointer*/ secondPoint});
2686
2687 // Next, the MOVE event for the first pointer
2688 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2689}
2690
2691/**
2692 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2693 * move, and then it will go up, all in the same frame.
2694 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2695 * gets sent to the listener.
2696 */
2697TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2698 NotifyMotionArgs args;
2699 const Point centerPoint = mDevice->getCenterPoint();
2700
2701 // ACTION_DOWN
2702 mDevice->sendSlot(FIRST_SLOT);
2703 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2704 mDevice->sendDown(centerPoint);
2705 mDevice->sendSync();
2706 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2707
2708 // ACTION_POINTER_DOWN (Second slot)
2709 const Point secondPoint = centerPoint + Point(100, 100);
2710 mDevice->sendSlot(SECOND_SLOT);
2711 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2712 mDevice->sendDown(secondPoint);
2713 mDevice->sendSync();
2714 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2715
2716 // ACTION_MOVE (First slot)
2717 mDevice->sendSlot(FIRST_SLOT);
2718 mDevice->sendMove(centerPoint + Point(5, 5));
2719 // ACTION_POINTER_UP (Second slot)
2720 mDevice->sendSlot(SECOND_SLOT);
2721 mDevice->sendMove(secondPoint + Point(6, 6));
2722 mDevice->sendPointerUp();
2723 // Send a single sync for the above 2 pointer updates
2724 mDevice->sendSync();
2725
2726 // First, we should get POINTER_UP for the second pointer
2727 // The movement of the second pointer during the liftoff frame is ignored.
2728 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2729 assertReceivedMotion(ACTION_POINTER_1_UP,
2730 {/*first pointer */ centerPoint + Point(5, 5),
2731 /*second pointer*/ secondPoint});
2732
2733 // Next, the MOVE event for the first pointer
2734 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2735}
2736
Arthur Hungaab25622020-01-16 11:22:11 +08002737TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2738 NotifyMotionArgs args;
2739 const Point centerPoint = mDevice->getCenterPoint();
2740
2741 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002742 mDevice->sendSlot(FIRST_SLOT);
2743 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002744 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002745 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002746 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2747 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2748
arthurhungcc7f9802020-04-30 17:55:40 +08002749 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002750 const Point secondPoint = centerPoint + Point(100, 100);
2751 mDevice->sendSlot(SECOND_SLOT);
2752 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2753 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002754 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002755 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002756 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002757
arthurhungcc7f9802020-04-30 17:55:40 +08002758 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002759 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002760 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002761 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2762 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2763
arthurhungcc7f9802020-04-30 17:55:40 +08002764 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2765 // a palm event.
2766 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002767 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002768 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002769 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002770 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002771 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002772
arthurhungcc7f9802020-04-30 17:55:40 +08002773 // Send up to second slot, expect first slot send moving.
2774 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002775 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002776 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2777 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002778
arthurhungcc7f9802020-04-30 17:55:40 +08002779 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002780 mDevice->sendSlot(FIRST_SLOT);
2781 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002782 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002783
arthurhungcc7f9802020-04-30 17:55:40 +08002784 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2785 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002786}
2787
Prabir Pradhanda20b172022-09-26 17:01:18 +00002788TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
2789 const Point centerPoint = mDevice->getCenterPoint();
2790
2791 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
2792 mDevice->sendSlot(FIRST_SLOT);
2793 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2794 mDevice->sendToolType(MT_TOOL_PEN);
2795 mDevice->sendDown(centerPoint);
2796 mDevice->sendSync();
2797 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2798 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2799 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2800
2801 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2802
2803 // Release the stylus touch.
2804 mDevice->sendUp();
2805 mDevice->sendSync();
2806 ASSERT_NO_FATAL_FAILURE(
2807 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2808
2809 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2810
2811 // Touch down with the finger, without the pen tool selected. The policy is not notified.
2812 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2813 mDevice->sendToolType(MT_TOOL_FINGER);
2814 mDevice->sendDown(centerPoint);
2815 mDevice->sendSync();
2816 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2817 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2818 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2819
2820 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2821
2822 mDevice->sendUp();
2823 mDevice->sendSync();
2824 ASSERT_NO_FATAL_FAILURE(
2825 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2826
2827 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
2828 // The policy should be notified of the stylus presence.
2829 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2830 mDevice->sendToolType(MT_TOOL_PEN);
2831 mDevice->sendMove(centerPoint);
2832 mDevice->sendSync();
2833 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2834 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2835 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2836
2837 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2838}
2839
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002840TEST_F(TouchIntegrationTest, StylusButtonsGenerateKeyEvents) {
2841 mDevice->sendKey(BTN_STYLUS, 1);
2842 mDevice->sendSync();
2843 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2844 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2845 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2846
2847 mDevice->sendKey(BTN_STYLUS, 0);
2848 mDevice->sendSync();
2849 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2850 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2851 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2852}
2853
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002854TEST_F(TouchIntegrationTest, StylusButtonsSurroundingTouchGesture) {
2855 const Point centerPoint = mDevice->getCenterPoint();
2856
2857 // Press the stylus button.
2858 mDevice->sendKey(BTN_STYLUS, 1);
2859 mDevice->sendSync();
2860 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2861 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2862 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2863
2864 // Start and finish a stylus gesture.
2865 mDevice->sendSlot(FIRST_SLOT);
2866 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2867 mDevice->sendToolType(MT_TOOL_PEN);
2868 mDevice->sendDown(centerPoint);
2869 mDevice->sendSync();
2870 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2871 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2872 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
2873 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2874 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2875 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2876 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
2877 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2878
2879 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2880 mDevice->sendSync();
2881 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2882 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2883 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2884 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2885 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2886 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2887
2888 // Release the stylus button.
2889 mDevice->sendKey(BTN_STYLUS, 0);
2890 mDevice->sendSync();
2891 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2892 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2893 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2894}
2895
2896TEST_F(TouchIntegrationTest, StylusButtonsWithinTouchGesture) {
2897 const Point centerPoint = mDevice->getCenterPoint();
2898
2899 // Start a stylus gesture.
2900 mDevice->sendSlot(FIRST_SLOT);
2901 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2902 mDevice->sendToolType(MT_TOOL_PEN);
2903 mDevice->sendDown(centerPoint);
2904 mDevice->sendSync();
2905 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2906 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2907 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2908
2909 // Press and release a stylus button. Each change in button state also generates a MOVE event.
2910 mDevice->sendKey(BTN_STYLUS, 1);
2911 mDevice->sendSync();
2912 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2913 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2914 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2915 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2916 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2917 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
2918 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2919 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2920 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2921 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
2922 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2923
2924 mDevice->sendKey(BTN_STYLUS, 0);
2925 mDevice->sendSync();
2926 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2927 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2928 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2929 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2930 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2931 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2932 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2933 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2934 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2935
2936 // Finish the stylus gesture.
2937 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2938 mDevice->sendSync();
2939 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2940 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2941 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2942}
2943
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002944// --- ExternalStylusIntegrationTest ---
2945
2946// Verify the behavior of an external stylus. An external stylus can report pressure or button
2947// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2948// ongoing stylus gesture that is being emitted by the touchscreen.
2949using ExternalStylusIntegrationTest = TouchIntegrationTest;
2950
2951TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
2952 const Point centerPoint = mDevice->getCenterPoint();
2953
2954 // Create an external stylus capable of reporting pressure data that
2955 // should be fused with a touch pointer.
2956 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2957 createUinputDevice<UinputExternalStylusWithPressure>();
2958 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2959 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2960 const auto stylusInfo = findDeviceByName(stylus->getName());
2961 ASSERT_TRUE(stylusInfo);
2962
2963 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2964
2965 const auto touchscreenId = mDeviceInfo.getId();
2966
2967 // Set a pressure value on the stylus. It doesn't generate any events.
2968 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2969 stylus->setPressure(100);
2970 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2971
2972 // Start a finger gesture, and ensure it shows up as stylus gesture
2973 // with the pressure set by the external stylus.
2974 mDevice->sendSlot(FIRST_SLOT);
2975 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2976 mDevice->sendToolType(MT_TOOL_FINGER);
2977 mDevice->sendDown(centerPoint);
2978 mDevice->sendSync();
2979 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2980 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2981 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2982 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
2983
2984 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2985 // event with the updated pressure.
2986 stylus->setPressure(200);
2987 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2988 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2989 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2990 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2991
2992 // The external stylus did not generate any events.
2993 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2994 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2995}
2996
2997TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
2998 const Point centerPoint = mDevice->getCenterPoint();
2999
3000 // Create an external stylus capable of reporting pressure data that
3001 // should be fused with a touch pointer.
3002 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
3003 createUinputDevice<UinputExternalStylusWithPressure>();
3004 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
3005 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
3006 const auto stylusInfo = findDeviceByName(stylus->getName());
3007 ASSERT_TRUE(stylusInfo);
3008
3009 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
3010
3011 const auto touchscreenId = mDeviceInfo.getId();
3012
3013 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
3014 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
3015 stylus->setPressure(0);
3016
3017 // Start a finger gesture. The touch device will withhold generating any touches for
3018 // up to 72 milliseconds while waiting for pressure data from the external stylus.
3019 mDevice->sendSlot(FIRST_SLOT);
3020 mDevice->sendTrackingId(FIRST_TRACKING_ID);
3021 mDevice->sendToolType(MT_TOOL_FINGER);
3022 mDevice->sendDown(centerPoint);
3023 auto waitUntil = std::chrono::system_clock::now() +
3024 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
3025 mDevice->sendSync();
3026 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
3027
3028 // Since the external stylus did not report a pressure value within the timeout,
3029 // it shows up as a finger pointer.
3030 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3031 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3032 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithDeviceId(touchscreenId),
3033 WithPressure(1.f))));
3034
3035 // Change the pressure on the external stylus. Since the pressure was not present at the start
3036 // of the gesture, it is ignored for now.
3037 stylus->setPressure(200);
3038 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3039
3040 // Finish the finger gesture.
3041 mDevice->sendTrackingId(INVALID_TRACKING_ID);
3042 mDevice->sendSync();
3043 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3044 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3045 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
3046
3047 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
3048 mDevice->sendTrackingId(FIRST_TRACKING_ID);
3049 mDevice->sendToolType(MT_TOOL_FINGER);
3050 mDevice->sendDown(centerPoint);
3051 mDevice->sendSync();
3052 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3053 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3054 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3055 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
3056
3057 // The external stylus did not generate any events.
3058 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3059 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
3060}
3061
Michael Wrightd02c5b62014-02-10 15:10:22 -08003062// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063class InputDeviceTest : public testing::Test {
3064protected:
3065 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08003066 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067 static const int32_t DEVICE_ID;
3068 static const int32_t DEVICE_GENERATION;
3069 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003070 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003071 static const int32_t EVENTHUB_ID;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003072 static const std::string DEVICE_BLUETOOTH_ADDRESS;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003074 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003075 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003076 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08003077 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00003078 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079
Chris Yea52ade12020-08-27 16:49:20 -07003080 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003081 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003082 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003083 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08003084 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003085 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003086 InputDeviceIdentifier identifier;
3087 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08003088 identifier.location = DEVICE_LOCATION;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003089 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
arthurhungdcef2dc2020-08-11 14:47:50 +08003090 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003091 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08003092 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003093 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003094 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003095 }
3096
Chris Yea52ade12020-08-27 16:49:20 -07003097 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003098 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003099 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100 }
3101};
3102
3103const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003104const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003105const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003106const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
3107const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003108const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07003109 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003110const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003111const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
Michael Wrightd02c5b62014-02-10 15:10:22 -08003112
3113TEST_F(InputDeviceTest, ImmutableProperties) {
3114 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003115 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003116 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003117}
3118
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00003119TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
3120 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
3121
3122 // Configuration
3123 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
3124 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003125 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00003126
3127 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
3128}
3129
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003130TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
3131 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07003132}
3133
Michael Wrightd02c5b62014-02-10 15:10:22 -08003134TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
3135 // Configuration.
3136 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003137 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003138
3139 // Reset.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003140 unused += mDevice->reset(ARBITRARY_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003141
3142 NotifyDeviceResetArgs resetArgs;
3143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3144 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3145 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3146
3147 // Metadata.
3148 ASSERT_TRUE(mDevice->isIgnored());
3149 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
3150
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00003151 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003152 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003153 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003154 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
3155 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
3156
3157 // State queries.
3158 ASSERT_EQ(0, mDevice->getMetaState());
3159
3160 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
3161 << "Ignored device should return unknown key code state.";
3162 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
3163 << "Ignored device should return unknown scan code state.";
3164 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
3165 << "Ignored device should return unknown switch state.";
3166
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003167 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003169 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003170 << "Ignored device should never mark any key codes.";
3171 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
3172 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
3173}
3174
3175TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
3176 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003177 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08003178
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003179 FakeInputMapper& mapper1 =
3180 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003181 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3182 mapper1.setMetaState(AMETA_ALT_ON);
3183 mapper1.addSupportedKeyCode(AKEYCODE_A);
3184 mapper1.addSupportedKeyCode(AKEYCODE_B);
3185 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
3186 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
3187 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
3188 mapper1.setScanCodeState(3, AKEY_STATE_UP);
3189 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003190
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003191 FakeInputMapper& mapper2 =
3192 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003193 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003194
3195 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003196 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003197
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003198 std::string propertyValue;
3199 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003200 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003201 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003202
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003203 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
3204 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003205
3206 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003207 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003208 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
3209 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003210
3211 NotifyDeviceResetArgs resetArgs;
3212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3213 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3214 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3215
3216 // Metadata.
3217 ASSERT_FALSE(mDevice->isIgnored());
3218 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
3219
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00003220 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003221 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003222 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003223 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
3224 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
3225
3226 // State queries.
3227 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
3228 << "Should query mappers and combine meta states.";
3229
3230 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3231 << "Should return unknown key code state when source not supported.";
3232 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3233 << "Should return unknown scan code state when source not supported.";
3234 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3235 << "Should return unknown switch state when source not supported.";
3236
3237 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
3238 << "Should query mapper when source is supported.";
3239 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
3240 << "Should query mapper when source is supported.";
3241 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
3242 << "Should query mapper when source is supported.";
3243
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003244 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08003245 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003246 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003247 << "Should do nothing when source is unsupported.";
3248 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
3249 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
3250 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
3251 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
3252
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003253 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003254 << "Should query mapper when source is supported.";
3255 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
3256 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
3257 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
3258 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
3259
3260 // Event handling.
3261 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003262 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003263 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003264
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003265 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
3266 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003267}
3268
Arthur Hung2c9a3342019-07-23 14:18:59 +08003269// A single input device is associated with a specific display. Check that:
3270// 1. Device is disabled if the viewport corresponding to the associated display is not found
3271// 2. Device is disabled when setEnabled API is called
3272TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003273 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003274
3275 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003276 std::list<NotifyArgs> unused =
3277 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003278
3279 // Device should be enabled by default.
3280 ASSERT_TRUE(mDevice->isEnabled());
3281
3282 // Prepare associated info.
3283 constexpr uint8_t hdmi = 1;
3284 const std::string UNIQUE_ID = "local:1";
3285
3286 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003287 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3288 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003289 // Device should be disabled because it is associated with a specific display via
3290 // input port <-> display port association, but the corresponding display is not found
3291 ASSERT_FALSE(mDevice->isEnabled());
3292
3293 // Prepare displays.
3294 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003295 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
3296 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003297 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3298 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003299 ASSERT_TRUE(mDevice->isEnabled());
3300
3301 // Device should be disabled after set disable.
3302 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003303 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3304 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003305 ASSERT_FALSE(mDevice->isEnabled());
3306
3307 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003308 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3309 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003310 ASSERT_FALSE(mDevice->isEnabled());
3311}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312
Christine Franks1ba71cc2021-04-07 14:37:42 -07003313TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
3314 // Device should be enabled by default.
3315 mFakePolicy->clearViewports();
3316 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003317 std::list<NotifyArgs> unused =
3318 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003319 ASSERT_TRUE(mDevice->isEnabled());
3320
3321 // Device should be disabled because it is associated with a specific display, but the
3322 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08003323 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003324 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3325 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003326 ASSERT_FALSE(mDevice->isEnabled());
3327
3328 // Device should be enabled when a display is found.
3329 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3330 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3331 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003332 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3333 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003334 ASSERT_TRUE(mDevice->isEnabled());
3335
3336 // Device should be disabled after set disable.
3337 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003338 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3339 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003340 ASSERT_FALSE(mDevice->isEnabled());
3341
3342 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003343 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3344 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003345 ASSERT_FALSE(mDevice->isEnabled());
3346}
3347
Christine Franks2a2293c2022-01-18 11:51:16 -08003348TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
3349 mFakePolicy->clearViewports();
3350 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003351 std::list<NotifyArgs> unused =
3352 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08003353
Christine Franks2a2293c2022-01-18 11:51:16 -08003354 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
3355 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3356 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3357 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003358 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3359 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08003360 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
3361}
3362
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07003363/**
3364 * This test reproduces a crash caused by a dangling reference that remains after device is added
3365 * and removed. The reference is accessed in InputDevice::dump(..);
3366 */
3367TEST_F(InputDeviceTest, DumpDoesNotCrash) {
3368 constexpr int32_t TEST_EVENTHUB_ID = 10;
3369 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
3370
3371 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
3372 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
3373 device.removeEventHubDevice(TEST_EVENTHUB_ID);
3374 std::string dumpStr, eventHubDevStr;
3375 device.dump(dumpStr, eventHubDevStr);
3376}
3377
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003378TEST_F(InputDeviceTest, GetBluetoothAddress) {
3379 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
3380 ASSERT_TRUE(address);
3381 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
3382}
3383
Michael Wrightd02c5b62014-02-10 15:10:22 -08003384// --- InputMapperTest ---
3385
3386class InputMapperTest : public testing::Test {
3387protected:
3388 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003389 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003390 static const int32_t DEVICE_ID;
3391 static const int32_t DEVICE_GENERATION;
3392 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003393 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003394 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003395
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003396 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003397 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003398 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08003399 std::unique_ptr<InstrumentedInputReader> mReader;
3400 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003401
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003402 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003403 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003404 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003405 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08003406 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003407 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08003408 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003409 // Consume the device reset notification generated when adding a new device.
3410 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003411 }
3412
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003413 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003414 SetUp(DEVICE_CLASSES);
3415 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003416
Chris Yea52ade12020-08-27 16:49:20 -07003417 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003418 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003419 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003420 }
3421
3422 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003423 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003424 }
3425
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003426 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003427 if (!changes ||
3428 (changes &
3429 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3430 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003431 mReader->requestRefreshConfiguration(changes);
3432 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003433 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003434 std::list<NotifyArgs> out =
3435 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003436 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003437 for (const NotifyArgs& args : out) {
3438 mFakeListener->notify(args);
3439 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003440 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003441 return out;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003442 }
3443
arthurhungdcef2dc2020-08-11 14:47:50 +08003444 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3445 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003446 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003447 InputDeviceIdentifier identifier;
3448 identifier.name = name;
3449 identifier.location = location;
3450 std::shared_ptr<InputDevice> device =
3451 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3452 identifier);
3453 mReader->pushNextDevice(device);
3454 mFakeEventHub->addDevice(eventHubId, name, classes);
3455 mReader->loopOnce();
3456 return device;
3457 }
3458
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003459 template <class T, typename... Args>
3460 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003461 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003462 configureDevice(0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003463 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
3464 resetArgList += mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003465 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003466 for (const NotifyArgs& loopArgs : resetArgList) {
3467 mFakeListener->notify(loopArgs);
3468 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003469 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003470 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003471 }
3472
3473 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003474 int32_t orientation, const std::string& uniqueId,
3475 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003476 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3477 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003478 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3479 }
3480
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003481 void clearViewports() {
3482 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483 }
3484
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003485 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
3486 int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003487 RawEvent event;
3488 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003489 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003490 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003491 event.type = type;
3492 event.code = code;
3493 event.value = value;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003494 std::list<NotifyArgs> processArgList = mapper.process(&event);
3495 for (const NotifyArgs& args : processArgList) {
3496 mFakeListener->notify(args);
3497 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003498 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003499 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003500 return processArgList;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003501 }
3502
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00003503 void resetMapper(InputMapper& mapper, nsecs_t when) {
3504 const auto resetArgs = mapper.reset(when);
3505 for (const auto args : resetArgs) {
3506 mFakeListener->notify(args);
3507 }
3508 // Loop the reader to flush the input listener queue.
3509 mReader->loopOnce();
3510 }
3511
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00003512 std::list<NotifyArgs> handleTimeout(InputMapper& mapper, nsecs_t when) {
3513 std::list<NotifyArgs> generatedArgs = mapper.timeoutExpired(when);
3514 for (const NotifyArgs& args : generatedArgs) {
3515 mFakeListener->notify(args);
3516 }
3517 // Loop the reader to flush the input listener queue.
3518 mReader->loopOnce();
3519 return generatedArgs;
3520 }
3521
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522 static void assertMotionRange(const InputDeviceInfo& info,
3523 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3524 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003525 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003526 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3527 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3528 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3529 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3530 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3531 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3532 }
3533
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003534 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3535 float size, float touchMajor, float touchMinor, float toolMajor,
3536 float toolMinor, float orientation, float distance,
3537 float scaledAxisEpsilon = 1.f) {
3538 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3539 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003540 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3541 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003542 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3543 scaledAxisEpsilon);
3544 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3545 scaledAxisEpsilon);
3546 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3547 scaledAxisEpsilon);
3548 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3549 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003550 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3551 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3552 }
3553
Michael Wright17db18e2020-06-26 20:51:44 +01003554 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003555 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003556 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557 ASSERT_NEAR(x, actualX, 1);
3558 ASSERT_NEAR(y, actualY, 1);
3559 }
3560};
3561
3562const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003563const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003564const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003565const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3566const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003567const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3568 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003569const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003570
3571// --- SwitchInputMapperTest ---
3572
3573class SwitchInputMapperTest : public InputMapperTest {
3574protected:
3575};
3576
3577TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003578 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003579
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003580 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003581}
3582
3583TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003584 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003585
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003586 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003587 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003588
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003589 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003590 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003591}
3592
3593TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003594 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003595 std::list<NotifyArgs> out;
3596 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3597 ASSERT_TRUE(out.empty());
3598 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3599 ASSERT_TRUE(out.empty());
3600 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3601 ASSERT_TRUE(out.empty());
3602 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003603
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003604 ASSERT_EQ(1u, out.size());
3605 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003606 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003607 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3608 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003609 args.switchMask);
3610 ASSERT_EQ(uint32_t(0), args.policyFlags);
3611}
3612
Chris Ye87143712020-11-10 05:05:58 +00003613// --- VibratorInputMapperTest ---
3614class VibratorInputMapperTest : public InputMapperTest {
3615protected:
3616 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3617};
3618
3619TEST_F(VibratorInputMapperTest, GetSources) {
3620 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3621
3622 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3623}
3624
3625TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3626 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3627
3628 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3629}
3630
3631TEST_F(VibratorInputMapperTest, Vibrate) {
3632 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003633 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003634 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3635
3636 VibrationElement pattern(2);
3637 VibrationSequence sequence(2);
3638 pattern.duration = std::chrono::milliseconds(200);
3639 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3640 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3641 sequence.addElement(pattern);
3642 pattern.duration = std::chrono::milliseconds(500);
3643 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3644 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3645 sequence.addElement(pattern);
3646
3647 std::vector<int64_t> timings = {0, 1};
3648 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3649
3650 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003651 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003652 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003653 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003654 // Verify vibrator state listener was notified.
3655 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003656 ASSERT_EQ(1u, out.size());
3657 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3658 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3659 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003660 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003661 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003662 ASSERT_FALSE(mapper.isVibrating());
3663 // Verify vibrator state listener was notified.
3664 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003665 ASSERT_EQ(1u, out.size());
3666 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3667 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3668 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003669}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003670
Chris Yef59a2f42020-10-16 12:55:26 -07003671// --- SensorInputMapperTest ---
3672
3673class SensorInputMapperTest : public InputMapperTest {
3674protected:
3675 static const int32_t ACCEL_RAW_MIN;
3676 static const int32_t ACCEL_RAW_MAX;
3677 static const int32_t ACCEL_RAW_FUZZ;
3678 static const int32_t ACCEL_RAW_FLAT;
3679 static const int32_t ACCEL_RAW_RESOLUTION;
3680
3681 static const int32_t GYRO_RAW_MIN;
3682 static const int32_t GYRO_RAW_MAX;
3683 static const int32_t GYRO_RAW_FUZZ;
3684 static const int32_t GYRO_RAW_FLAT;
3685 static const int32_t GYRO_RAW_RESOLUTION;
3686
3687 static const float GRAVITY_MS2_UNIT;
3688 static const float DEGREE_RADIAN_UNIT;
3689
3690 void prepareAccelAxes();
3691 void prepareGyroAxes();
3692 void setAccelProperties();
3693 void setGyroProperties();
3694 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3695};
3696
3697const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3698const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3699const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3700const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3701const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3702
3703const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3704const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3705const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3706const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3707const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3708
3709const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3710const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3711
3712void SensorInputMapperTest::prepareAccelAxes() {
3713 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3714 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3715 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3716 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3717 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3718 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3719}
3720
3721void SensorInputMapperTest::prepareGyroAxes() {
3722 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3723 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3724 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3725 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3726 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3727 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3728}
3729
3730void SensorInputMapperTest::setAccelProperties() {
3731 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3732 /* sensorDataIndex */ 0);
3733 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3734 /* sensorDataIndex */ 1);
3735 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3736 /* sensorDataIndex */ 2);
3737 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3738 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3739 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3740 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3741 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3742}
3743
3744void SensorInputMapperTest::setGyroProperties() {
3745 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3746 /* sensorDataIndex */ 0);
3747 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3748 /* sensorDataIndex */ 1);
3749 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3750 /* sensorDataIndex */ 2);
3751 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3752 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3753 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3754 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3755 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3756}
3757
3758TEST_F(SensorInputMapperTest, GetSources) {
3759 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3760
3761 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3762}
3763
3764TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3765 setAccelProperties();
3766 prepareAccelAxes();
3767 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3768
3769 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3770 std::chrono::microseconds(10000),
3771 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003772 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003773 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3774 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3776 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003778
3779 NotifySensorArgs args;
3780 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3781 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3782 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3783
3784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3785 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3786 ASSERT_EQ(args.deviceId, DEVICE_ID);
3787 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3788 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3789 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3790 ASSERT_EQ(args.values, values);
3791 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3792}
3793
3794TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3795 setGyroProperties();
3796 prepareGyroAxes();
3797 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3798
3799 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3800 std::chrono::microseconds(10000),
3801 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003802 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3804 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3805 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3806 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003808
3809 NotifySensorArgs args;
3810 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3811 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3812 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3813
3814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3815 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3816 ASSERT_EQ(args.deviceId, DEVICE_ID);
3817 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3818 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3819 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3820 ASSERT_EQ(args.values, values);
3821 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3822}
3823
Michael Wrightd02c5b62014-02-10 15:10:22 -08003824// --- KeyboardInputMapperTest ---
3825
3826class KeyboardInputMapperTest : public InputMapperTest {
3827protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003828 const std::string UNIQUE_ID = "local:0";
3829
3830 void prepareDisplay(int32_t orientation);
3831
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003832 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003833 int32_t originalKeyCode, int32_t rotatedKeyCode,
3834 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003835};
3836
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003837/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3838 * orientation.
3839 */
3840void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003841 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3842 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003843}
3844
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003845void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003846 int32_t originalScanCode, int32_t originalKeyCode,
3847 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003848 NotifyKeyArgs args;
3849
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003850 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3852 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3853 ASSERT_EQ(originalScanCode, args.scanCode);
3854 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003855 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003856
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3859 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3860 ASSERT_EQ(originalScanCode, args.scanCode);
3861 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003862 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003863}
3864
Michael Wrightd02c5b62014-02-10 15:10:22 -08003865TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003866 KeyboardInputMapper& mapper =
3867 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3868 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003869
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003870 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003871}
3872
3873TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3874 const int32_t USAGE_A = 0x070004;
3875 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003876 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3877 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003878 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3879 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3880 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003881
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003882 KeyboardInputMapper& mapper =
3883 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3884 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003885 // Initial metastate is AMETA_NONE.
3886 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003887
3888 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003890 NotifyKeyArgs args;
3891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3892 ASSERT_EQ(DEVICE_ID, args.deviceId);
3893 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3894 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3895 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3896 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3897 ASSERT_EQ(KEY_HOME, args.scanCode);
3898 ASSERT_EQ(AMETA_NONE, args.metaState);
3899 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3900 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3901 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3902
3903 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003904 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3906 ASSERT_EQ(DEVICE_ID, args.deviceId);
3907 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3908 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3909 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3910 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3911 ASSERT_EQ(KEY_HOME, args.scanCode);
3912 ASSERT_EQ(AMETA_NONE, args.metaState);
3913 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3914 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3915 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3916
3917 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003918 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3919 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3921 ASSERT_EQ(DEVICE_ID, args.deviceId);
3922 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3923 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3924 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3925 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3926 ASSERT_EQ(0, args.scanCode);
3927 ASSERT_EQ(AMETA_NONE, args.metaState);
3928 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3929 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3930 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3931
3932 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003933 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3934 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3936 ASSERT_EQ(DEVICE_ID, args.deviceId);
3937 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3938 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3939 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3940 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3941 ASSERT_EQ(0, args.scanCode);
3942 ASSERT_EQ(AMETA_NONE, args.metaState);
3943 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3944 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3945 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3946
3947 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3951 ASSERT_EQ(DEVICE_ID, args.deviceId);
3952 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3953 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3954 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3955 ASSERT_EQ(0, args.keyCode);
3956 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3957 ASSERT_EQ(AMETA_NONE, args.metaState);
3958 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3959 ASSERT_EQ(0U, args.policyFlags);
3960 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3961
3962 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3964 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3966 ASSERT_EQ(DEVICE_ID, args.deviceId);
3967 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3968 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3969 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3970 ASSERT_EQ(0, args.keyCode);
3971 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3972 ASSERT_EQ(AMETA_NONE, args.metaState);
3973 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3974 ASSERT_EQ(0U, args.policyFlags);
3975 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3976}
3977
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003978/**
3979 * Ensure that the readTime is set to the time when the EV_KEY is received.
3980 */
3981TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3982 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3983
3984 KeyboardInputMapper& mapper =
3985 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3986 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3987 NotifyKeyArgs args;
3988
3989 // Key down
3990 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3992 ASSERT_EQ(12, args.readTime);
3993
3994 // Key up
3995 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3997 ASSERT_EQ(15, args.readTime);
3998}
3999
Michael Wrightd02c5b62014-02-10 15:10:22 -08004000TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004001 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
4002 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07004003 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
4004 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
4005 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004006
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004007 KeyboardInputMapper& mapper =
4008 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4009 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004010
Arthur Hung95f68612022-04-07 14:08:22 +08004011 // Initial metastate is AMETA_NONE.
4012 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004013
4014 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004015 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004016 NotifyKeyArgs args;
4017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4018 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004019 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08004020 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021
4022 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004023 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4025 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004026 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004027
4028 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004029 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4031 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004032 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004033
4034 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004035 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4037 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004038 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08004039 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004040}
4041
4042TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004043 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4044 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4045 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4046 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004047
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004048 KeyboardInputMapper& mapper =
4049 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4050 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004051
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004052 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004053 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4054 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
4055 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4056 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
4057 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4058 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
4059 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4060 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
4061}
4062
4063TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004064 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4065 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4066 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4067 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004068
Michael Wrightd02c5b62014-02-10 15:10:22 -08004069 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004070 KeyboardInputMapper& mapper =
4071 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4072 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004073
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004074 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004075 ASSERT_NO_FATAL_FAILURE(
4076 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4077 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4078 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4079 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4080 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4081 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4082 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004083
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004084 clearViewports();
4085 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004086 ASSERT_NO_FATAL_FAILURE(
4087 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4088 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4089 AKEYCODE_DPAD_UP, DISPLAY_ID));
4090 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4091 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4092 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4093 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004095 clearViewports();
4096 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004097 ASSERT_NO_FATAL_FAILURE(
4098 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4099 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4100 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4101 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4102 AKEYCODE_DPAD_UP, DISPLAY_ID));
4103 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4104 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004105
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004106 clearViewports();
4107 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004108 ASSERT_NO_FATAL_FAILURE(
4109 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4110 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4111 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4112 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4113 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4114 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4115 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004116
4117 // Special case: if orientation changes while key is down, we still emit the same keycode
4118 // in the key up as we did in the key down.
4119 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004120 clearViewports();
4121 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4124 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4125 ASSERT_EQ(KEY_UP, args.scanCode);
4126 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
4127
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004128 clearViewports();
4129 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004130 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4132 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4133 ASSERT_EQ(KEY_UP, args.scanCode);
4134 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
4135}
4136
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004137TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
4138 // If the keyboard is not orientation aware,
4139 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004140 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004141
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004142 KeyboardInputMapper& mapper =
4143 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4144 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004145 NotifyKeyArgs args;
4146
4147 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004150 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4152 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
4153
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004154 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4159 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
4160}
4161
4162TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
4163 // If the keyboard is orientation aware,
4164 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004165 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004166
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004167 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004168 KeyboardInputMapper& mapper =
4169 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4170 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004171 NotifyKeyArgs args;
4172
4173 // Display id should be ADISPLAY_ID_NONE without any display configuration.
4174 // ^--- already checked by the previous test
4175
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004176 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004177 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004178 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4182 ASSERT_EQ(DISPLAY_ID, args.displayId);
4183
4184 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004185 clearViewports();
4186 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004187 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004190 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4192 ASSERT_EQ(newDisplayId, args.displayId);
4193}
4194
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004196 KeyboardInputMapper& mapper =
4197 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4198 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004200 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004201 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004203 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004204 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004205}
4206
Philip Junker4af3b3d2021-12-14 10:36:55 +01004207TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
4208 KeyboardInputMapper& mapper =
4209 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4210 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4211
4212 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
4213 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
4214 << "If a mapping is available, the result is equal to the mapping";
4215
4216 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
4217 << "If no mapping is available, the result is the key location";
4218}
4219
Michael Wrightd02c5b62014-02-10 15:10:22 -08004220TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004221 KeyboardInputMapper& mapper =
4222 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4223 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004224
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004225 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004226 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004227
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004228 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004229 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230}
4231
4232TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004233 KeyboardInputMapper& mapper =
4234 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4235 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004236
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004237 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004238
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004240 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241 ASSERT_TRUE(flags[0]);
4242 ASSERT_FALSE(flags[1]);
4243}
4244
4245TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004246 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4247 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4248 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4249 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4250 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4251 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004252
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004253 KeyboardInputMapper& mapper =
4254 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4255 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004256 // Initial metastate is AMETA_NONE.
4257 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004258
4259 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004260 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4261 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4262 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263
4264 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004265 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4266 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004267 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4268 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4269 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004270 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004271
4272 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004275 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4276 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4277 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004278 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279
4280 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004283 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4284 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4285 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004286 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004287
4288 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004291 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4292 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4293 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004294 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295
4296 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004299 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4300 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4301 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004302 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004303
4304 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4306 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004307 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4308 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4309 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004310 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004311}
4312
Chris Yea52ade12020-08-27 16:49:20 -07004313TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
4314 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
4315 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
4316 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
4317 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
4318
4319 KeyboardInputMapper& mapper =
4320 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4321 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4322
Chris Yea52ade12020-08-27 16:49:20 -07004323 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004324 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07004325 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4326 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
4327 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
4328 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4329
4330 NotifyKeyArgs args;
4331 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07004333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4334 ASSERT_EQ(AMETA_NONE, args.metaState);
4335 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4336 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4337 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4338
4339 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004340 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07004341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4342 ASSERT_EQ(AMETA_NONE, args.metaState);
4343 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4344 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4345 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4346}
4347
Arthur Hung2c9a3342019-07-23 14:18:59 +08004348TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
4349 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004350 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4351 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4352 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4353 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004354
4355 // keyboard 2.
4356 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08004357 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08004358 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004359 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08004360 std::shared_ptr<InputDevice> device2 =
4361 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004362 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08004363
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004364 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4365 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4366 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4367 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004368
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004369 KeyboardInputMapper& mapper =
4370 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4371 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004372
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004373 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004374 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004375 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004376 std::list<NotifyArgs> unused =
4377 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4378 0 /*changes*/);
4379 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004380
4381 // Prepared displays and associated info.
4382 constexpr uint8_t hdmi1 = 0;
4383 constexpr uint8_t hdmi2 = 1;
4384 const std::string SECONDARY_UNIQUE_ID = "local:1";
4385
4386 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
4387 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
4388
4389 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004390 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4391 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004392 ASSERT_FALSE(device2->isEnabled());
4393
4394 // Prepare second display.
4395 constexpr int32_t newDisplayId = 2;
4396 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004397 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004398 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004399 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004400 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004401 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4402 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004403
4404 // Device should be enabled after the associated display is found.
4405 ASSERT_TRUE(mDevice->isEnabled());
4406 ASSERT_TRUE(device2->isEnabled());
4407
4408 // Test pad key events
4409 ASSERT_NO_FATAL_FAILURE(
4410 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4411 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4412 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4413 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4414 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4415 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4416 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4417
4418 ASSERT_NO_FATAL_FAILURE(
4419 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
4420 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4421 AKEYCODE_DPAD_RIGHT, newDisplayId));
4422 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4423 AKEYCODE_DPAD_DOWN, newDisplayId));
4424 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4425 AKEYCODE_DPAD_LEFT, newDisplayId));
4426}
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427
arthurhungc903df12020-08-11 15:08:42 +08004428TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
4429 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4430 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4431 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4432 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4433 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4434 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4435
4436 KeyboardInputMapper& mapper =
4437 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4438 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004439 // Initial metastate is AMETA_NONE.
4440 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004441
4442 // Initialization should have turned all of the lights off.
4443 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4444 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4445 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4446
4447 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004450 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4451 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4452
4453 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004454 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004456 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4457 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
4458
4459 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004460 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4461 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004462 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4463 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4464
4465 mFakeEventHub->removeDevice(EVENTHUB_ID);
4466 mReader->loopOnce();
4467
4468 // keyboard 2 should default toggle keys.
4469 const std::string USB2 = "USB2";
4470 const std::string DEVICE_NAME2 = "KEYBOARD2";
4471 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4472 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4473 std::shared_ptr<InputDevice> device2 =
4474 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004475 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004476 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4477 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4478 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4479 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4480 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4481 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4482
arthurhung6fe95782020-10-05 22:41:16 +08004483 KeyboardInputMapper& mapper2 =
4484 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4485 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004486 std::list<NotifyArgs> unused =
4487 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4488 0 /*changes*/);
4489 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08004490
4491 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4492 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4493 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004494 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4495 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004496}
4497
Arthur Hungcb40a002021-08-03 14:31:01 +00004498TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4499 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4500 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4501 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4502
4503 // Suppose we have two mappers. (DPAD + KEYBOARD)
4504 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4505 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4506 KeyboardInputMapper& mapper =
4507 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4508 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004509 // Initial metastate is AMETA_NONE.
4510 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004511
4512 mReader->toggleCapsLockState(DEVICE_ID);
4513 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4514}
4515
Arthur Hungfb3cc112022-04-13 07:39:50 +00004516TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4517 // keyboard 1.
4518 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4519 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4520 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4521 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4522 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4523 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4524
4525 KeyboardInputMapper& mapper1 =
4526 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4527 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4528
4529 // keyboard 2.
4530 const std::string USB2 = "USB2";
4531 const std::string DEVICE_NAME2 = "KEYBOARD2";
4532 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4533 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4534 std::shared_ptr<InputDevice> device2 =
4535 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4536 ftl::Flags<InputDeviceClass>(0));
4537 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4538 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4539 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4540 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4541 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4542 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4543
4544 KeyboardInputMapper& mapper2 =
4545 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4546 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004547 std::list<NotifyArgs> unused =
4548 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4549 0 /*changes*/);
4550 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004551
Arthur Hung95f68612022-04-07 14:08:22 +08004552 // Initial metastate is AMETA_NONE.
4553 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4554 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4555
4556 // Toggle num lock on and off.
4557 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4558 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004559 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4560 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4561 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4562
4563 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4564 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4565 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4566 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4567 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4568
4569 // Toggle caps lock on and off.
4570 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4571 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4572 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4573 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4574 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4575
4576 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4577 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4578 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4579 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4580 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4581
4582 // Toggle scroll lock on and off.
4583 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4584 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4585 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4586 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4587 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4588
4589 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4590 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4591 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4592 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4593 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4594}
4595
Arthur Hung2141d542022-08-23 07:45:21 +00004596TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4597 const int32_t USAGE_A = 0x070004;
4598 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4599 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4600
4601 KeyboardInputMapper& mapper =
4602 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4603 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4604 // Key down by scan code.
4605 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4606 NotifyKeyArgs args;
4607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4608 ASSERT_EQ(DEVICE_ID, args.deviceId);
4609 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4610 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4611 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4612 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4613 ASSERT_EQ(KEY_HOME, args.scanCode);
4614 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4615
4616 // Disable device, it should synthesize cancellation events for down events.
4617 mFakePolicy->addDisabledDevice(DEVICE_ID);
4618 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4619
4620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4621 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4622 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4623 ASSERT_EQ(KEY_HOME, args.scanCode);
4624 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4625}
4626
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004627// --- KeyboardInputMapperTest_ExternalDevice ---
4628
4629class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4630protected:
Chris Yea52ade12020-08-27 16:49:20 -07004631 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004632};
4633
4634TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004635 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4636 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004637
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004638 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4639 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4640 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4641 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004642
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004643 KeyboardInputMapper& mapper =
4644 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4645 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004646
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004648 NotifyKeyArgs args;
4649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4650 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4651
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004652 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4654 ASSERT_EQ(uint32_t(0), args.policyFlags);
4655
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4658 ASSERT_EQ(uint32_t(0), args.policyFlags);
4659
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004660 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4662 ASSERT_EQ(uint32_t(0), args.policyFlags);
4663
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4666 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4667
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004668 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4670 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4671}
4672
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004673TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004674 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004675
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004676 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4677 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4678 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004679
Powei Fengd041c5d2019-05-03 17:11:33 -07004680 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004681 KeyboardInputMapper& mapper =
4682 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4683 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004684
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004686 NotifyKeyArgs args;
4687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4688 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4689
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004690 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4692 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4693
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004694 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4696 ASSERT_EQ(uint32_t(0), args.policyFlags);
4697
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004698 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4700 ASSERT_EQ(uint32_t(0), args.policyFlags);
4701
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004702 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4704 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4705
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004706 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4708 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4709}
4710
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711// --- CursorInputMapperTest ---
4712
4713class CursorInputMapperTest : public InputMapperTest {
4714protected:
4715 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4716
Michael Wright17db18e2020-06-26 20:51:44 +01004717 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004718
Chris Yea52ade12020-08-27 16:49:20 -07004719 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004720 InputMapperTest::SetUp();
4721
Michael Wright17db18e2020-06-26 20:51:44 +01004722 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004723 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004724 }
4725
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004726 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4727 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004728
4729 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004730 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4731 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4732 }
4733
4734 void prepareSecondaryDisplay() {
4735 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4736 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4737 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004738 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004739
4740 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4741 float pressure) {
4742 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4743 0.0f, 0.0f, 0.0f, EPSILON));
4744 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004745};
4746
4747const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4748
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004749void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4750 int32_t originalY, int32_t rotatedX,
4751 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004752 NotifyMotionArgs args;
4753
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004754 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4755 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4756 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4758 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004759 ASSERT_NO_FATAL_FAILURE(
4760 assertCursorPointerCoords(args.pointerCoords[0],
4761 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4762 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763}
4764
4765TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004766 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004767 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004768
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004769 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770}
4771
4772TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004773 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004774 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004775
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004776 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004777}
4778
4779TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004780 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004781 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782
4783 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004784 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004785
4786 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004787 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4788 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004789 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4790 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4791
4792 // When the bounds are set, then there should be a valid motion range.
4793 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4794
4795 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004796 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004797
4798 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4799 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4800 1, 800 - 1, 0.0f, 0.0f));
4801 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4802 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4803 2, 480 - 1, 0.0f, 0.0f));
4804 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4805 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4806 0.0f, 1.0f, 0.0f, 0.0f));
4807}
4808
4809TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004811 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004812
4813 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004814 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004815
4816 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4817 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4818 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4819 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4820 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4821 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4822 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4823 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4824 0.0f, 1.0f, 0.0f, 0.0f));
4825}
4826
4827TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004828 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004829 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004830
arthurhungdcef2dc2020-08-11 14:47:50 +08004831 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004832
4833 NotifyMotionArgs args;
4834
4835 // Button press.
4836 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004837 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4838 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4840 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4841 ASSERT_EQ(DEVICE_ID, args.deviceId);
4842 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4843 ASSERT_EQ(uint32_t(0), args.policyFlags);
4844 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4845 ASSERT_EQ(0, args.flags);
4846 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4847 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4848 ASSERT_EQ(0, args.edgeFlags);
4849 ASSERT_EQ(uint32_t(1), args.pointerCount);
4850 ASSERT_EQ(0, args.pointerProperties[0].id);
4851 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004852 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4854 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4855 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4856
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4858 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4859 ASSERT_EQ(DEVICE_ID, args.deviceId);
4860 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4861 ASSERT_EQ(uint32_t(0), args.policyFlags);
4862 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4863 ASSERT_EQ(0, args.flags);
4864 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4865 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4866 ASSERT_EQ(0, args.edgeFlags);
4867 ASSERT_EQ(uint32_t(1), args.pointerCount);
4868 ASSERT_EQ(0, args.pointerProperties[0].id);
4869 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004870 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004871 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4872 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4873 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4874
Michael Wrightd02c5b62014-02-10 15:10:22 -08004875 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004876 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4877 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4879 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4880 ASSERT_EQ(DEVICE_ID, args.deviceId);
4881 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4882 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004883 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4884 ASSERT_EQ(0, args.flags);
4885 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4886 ASSERT_EQ(0, args.buttonState);
4887 ASSERT_EQ(0, args.edgeFlags);
4888 ASSERT_EQ(uint32_t(1), args.pointerCount);
4889 ASSERT_EQ(0, args.pointerProperties[0].id);
4890 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004891 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004892 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4893 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4894 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4895
4896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4897 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4898 ASSERT_EQ(DEVICE_ID, args.deviceId);
4899 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4900 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004901 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4902 ASSERT_EQ(0, args.flags);
4903 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4904 ASSERT_EQ(0, args.buttonState);
4905 ASSERT_EQ(0, args.edgeFlags);
4906 ASSERT_EQ(uint32_t(1), args.pointerCount);
4907 ASSERT_EQ(0, args.pointerProperties[0].id);
4908 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004909 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004910 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4911 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4912 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4913}
4914
4915TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004916 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004917 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004918
4919 NotifyMotionArgs args;
4920
4921 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004922 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4923 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4925 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004926 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4927 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4928 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004929
4930 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004931 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4932 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4934 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004935 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4936 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004937}
4938
4939TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004940 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004941 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004942
4943 NotifyMotionArgs args;
4944
4945 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4949 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004950 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004951
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4953 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004954 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004955
Michael Wrightd02c5b62014-02-10 15:10:22 -08004956 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004957 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4958 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004960 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004961 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004962
4963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004965 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966}
4967
4968TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004969 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004970 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004971
4972 NotifyMotionArgs args;
4973
4974 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4976 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4977 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 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(&args));
4980 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004981 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4982 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4983 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4986 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004987 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4988 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4989 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004990
Michael Wrightd02c5b62014-02-10 15:10:22 -08004991 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
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(&args));
4996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004997 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4998 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4999 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000
5001 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005002 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005005 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005006 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005007
5008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005010 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005011}
5012
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005013TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005014 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005015 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005016 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5017 // need to be rotated.
5018 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005019 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005020
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005021 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005022 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
5023 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
5024 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
5025 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
5026 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
5027 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
5028 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
5029 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
5030}
5031
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005032TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005033 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005034 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005035 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5036 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005037 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005038
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005039 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005040 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005041 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
5042 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
5043 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
5044 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
5045 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
5046 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
5047 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
5048 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
5049
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005050 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005051 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005052 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
5053 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
5054 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
5055 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
5056 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
5057 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
5058 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
5059 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005061 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005062 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005063 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
5064 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
5065 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
5066 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
5067 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
5068 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
5069 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
5070 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
5071
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005072 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005073 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005074 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
5075 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
5076 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
5077 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
5078 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
5079 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
5080 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
5081 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005082}
5083
5084TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005085 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005086 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005087
5088 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5089 mFakePointerController->setPosition(100, 200);
5090 mFakePointerController->setButtonState(0);
5091
5092 NotifyMotionArgs motionArgs;
5093 NotifyKeyArgs keyArgs;
5094
5095 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
5097 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5099 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5100 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5101 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005102 ASSERT_NO_FATAL_FAILURE(
5103 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005104
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5106 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5107 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5108 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005109 ASSERT_NO_FATAL_FAILURE(
5110 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005111
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005112 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
5113 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005115 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116 ASSERT_EQ(0, motionArgs.buttonState);
5117 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005118 ASSERT_NO_FATAL_FAILURE(
5119 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005120
5121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005122 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123 ASSERT_EQ(0, motionArgs.buttonState);
5124 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005125 ASSERT_NO_FATAL_FAILURE(
5126 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005127
5128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005129 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005130 ASSERT_EQ(0, motionArgs.buttonState);
5131 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005132 ASSERT_NO_FATAL_FAILURE(
5133 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005134
5135 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
5137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
5138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5140 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5141 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5142 motionArgs.buttonState);
5143 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5144 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005145 ASSERT_NO_FATAL_FAILURE(
5146 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005147
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5149 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5150 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5151 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5152 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005153 ASSERT_NO_FATAL_FAILURE(
5154 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005155
5156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5157 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5158 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5159 motionArgs.buttonState);
5160 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5161 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005162 ASSERT_NO_FATAL_FAILURE(
5163 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005164
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
5166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005168 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005169 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5170 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005171 ASSERT_NO_FATAL_FAILURE(
5172 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005173
5174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005175 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005176 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5177 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005178 ASSERT_NO_FATAL_FAILURE(
5179 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005180
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005181 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5182 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005184 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5185 ASSERT_EQ(0, motionArgs.buttonState);
5186 ASSERT_EQ(0, 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));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005189 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5190 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005191
5192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005193 ASSERT_EQ(0, motionArgs.buttonState);
5194 ASSERT_EQ(0, mFakePointerController->getButtonState());
5195 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
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
Michael Wrightd02c5b62014-02-10 15:10:22 -08005199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5200 ASSERT_EQ(0, motionArgs.buttonState);
5201 ASSERT_EQ(0, mFakePointerController->getButtonState());
5202 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005203 ASSERT_NO_FATAL_FAILURE(
5204 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005205
5206 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
5208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5210 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5211 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005212
Michael Wrightd02c5b62014-02-10 15:10:22 -08005213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005214 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005215 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5216 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005217 ASSERT_NO_FATAL_FAILURE(
5218 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005219
5220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5221 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5222 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5223 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005224 ASSERT_NO_FATAL_FAILURE(
5225 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005226
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005227 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
5228 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005230 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005231 ASSERT_EQ(0, motionArgs.buttonState);
5232 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005233 ASSERT_NO_FATAL_FAILURE(
5234 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005235
5236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005237 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005238 ASSERT_EQ(0, motionArgs.buttonState);
5239 ASSERT_EQ(0, mFakePointerController->getButtonState());
5240
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005241 ASSERT_NO_FATAL_FAILURE(
5242 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5244 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5245 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5246
5247 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005248 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
5249 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5251 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5252 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005253
Michael Wrightd02c5b62014-02-10 15:10:22 -08005254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005255 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005256 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5257 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005258 ASSERT_NO_FATAL_FAILURE(
5259 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005260
5261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5262 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5263 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5264 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005265 ASSERT_NO_FATAL_FAILURE(
5266 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005267
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005268 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
5269 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005271 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005272 ASSERT_EQ(0, motionArgs.buttonState);
5273 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005274 ASSERT_NO_FATAL_FAILURE(
5275 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005276
5277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5278 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5279 ASSERT_EQ(0, motionArgs.buttonState);
5280 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005281 ASSERT_NO_FATAL_FAILURE(
5282 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005283
Michael Wrightd02c5b62014-02-10 15:10:22 -08005284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5285 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5286 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5287
5288 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
5290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5292 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5293 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005294
Michael Wrightd02c5b62014-02-10 15:10:22 -08005295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005296 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005297 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5298 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005299 ASSERT_NO_FATAL_FAILURE(
5300 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005301
5302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5303 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5304 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5305 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005306 ASSERT_NO_FATAL_FAILURE(
5307 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005308
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
5310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005312 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005313 ASSERT_EQ(0, motionArgs.buttonState);
5314 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005315 ASSERT_NO_FATAL_FAILURE(
5316 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005317
5318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5319 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5320 ASSERT_EQ(0, motionArgs.buttonState);
5321 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005322 ASSERT_NO_FATAL_FAILURE(
5323 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005324
Michael Wrightd02c5b62014-02-10 15:10:22 -08005325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5326 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5327 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5328
5329 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005330 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
5331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5333 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5334 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005335
Michael Wrightd02c5b62014-02-10 15:10:22 -08005336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005337 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005338 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5339 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005340 ASSERT_NO_FATAL_FAILURE(
5341 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005342
5343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5344 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5345 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5346 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005347 ASSERT_NO_FATAL_FAILURE(
5348 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005349
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005350 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
5351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005353 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005354 ASSERT_EQ(0, motionArgs.buttonState);
5355 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005356 ASSERT_NO_FATAL_FAILURE(
5357 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005358
5359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5360 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5361 ASSERT_EQ(0, motionArgs.buttonState);
5362 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005363 ASSERT_NO_FATAL_FAILURE(
5364 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005365
Michael Wrightd02c5b62014-02-10 15:10:22 -08005366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5367 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5368 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5369}
5370
5371TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005372 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005373 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374
5375 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5376 mFakePointerController->setPosition(100, 200);
5377 mFakePointerController->setButtonState(0);
5378
5379 NotifyMotionArgs args;
5380
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5382 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005385 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5386 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5387 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5388 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 +01005389 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005390}
5391
5392TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005393 addConfigurationProperty("cursor.mode", "pointer");
5394 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005395 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005396
5397 NotifyDeviceResetArgs resetArgs;
5398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5399 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5400 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5401
5402 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5403 mFakePointerController->setPosition(100, 200);
5404 mFakePointerController->setButtonState(0);
5405
5406 NotifyMotionArgs args;
5407
5408 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5411 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5413 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5414 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5416 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 +01005417 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005418
5419 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5421 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5423 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5424 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5426 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5428 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5429 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
5430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5431 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5432
5433 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005434 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5435 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5437 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5438 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
5439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5440 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5442 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5443 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
5444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5445 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5446
5447 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
5449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
5450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5452 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5453 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5454 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5455 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 +01005456 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005457
5458 // Disable pointer capture and check that the device generation got bumped
5459 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08005460 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005461 mFakePolicy->setPointerCapture(false);
5462 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08005463 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005464
5465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005466 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5467
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5469 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5470 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5472 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005473 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5475 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 +01005476 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005477}
5478
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00005479/**
5480 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
5481 * pointer acceleration or speed processing should not be applied.
5482 */
5483TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5484 addConfigurationProperty("cursor.mode", "pointer");
5485 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5486 100.f /*high threshold*/, 10.f /*acceleration*/);
5487 mFakePolicy->setVelocityControlParams(testParams);
5488 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5489
5490 NotifyDeviceResetArgs resetArgs;
5491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5492 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5493 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5494
5495 NotifyMotionArgs args;
5496
5497 // Move and verify scale is applied.
5498 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5499 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5502 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5503 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5504 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5505 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5506 ASSERT_GT(relX, 10);
5507 ASSERT_GT(relY, 20);
5508
5509 // Enable Pointer Capture
5510 mFakePolicy->setPointerCapture(true);
5511 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5512 NotifyPointerCaptureChangedArgs captureArgs;
5513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5514 ASSERT_TRUE(captureArgs.request.enable);
5515
5516 // Move and verify scale is not applied.
5517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5521 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5522 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5523 ASSERT_EQ(10, args.pointerCoords[0].getX());
5524 ASSERT_EQ(20, args.pointerCoords[0].getY());
5525}
5526
Prabir Pradhan208360b2022-06-24 18:37:04 +00005527TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5528 addConfigurationProperty("cursor.mode", "pointer");
5529 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5530
5531 NotifyDeviceResetArgs resetArgs;
5532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5533 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5534 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5535
5536 // Ensure the display is rotated.
5537 prepareDisplay(DISPLAY_ORIENTATION_90);
5538
5539 NotifyMotionArgs args;
5540
5541 // Verify that the coordinates are rotated.
5542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5546 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5547 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5548 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5549 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5550
5551 // Enable Pointer Capture.
5552 mFakePolicy->setPointerCapture(true);
5553 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5554 NotifyPointerCaptureChangedArgs captureArgs;
5555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5556 ASSERT_TRUE(captureArgs.request.enable);
5557
5558 // Move and verify rotation is not applied.
5559 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5560 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5561 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5563 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5564 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5565 ASSERT_EQ(10, args.pointerCoords[0].getX());
5566 ASSERT_EQ(20, args.pointerCoords[0].getY());
5567}
5568
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005569TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005570 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005571
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005572 // Set up the default display.
5573 prepareDisplay(DISPLAY_ORIENTATION_90);
5574
5575 // Set up the secondary display as the display on which the pointer should be shown.
5576 // The InputDevice is not associated with any display.
5577 prepareSecondaryDisplay();
5578 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005579 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5580
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005581 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005582 mFakePointerController->setPosition(100, 200);
5583 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005584
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005585 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005586 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5588 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005590 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5591 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5592 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005593 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005594}
5595
5596TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5597 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5598
5599 // Set up the default display.
5600 prepareDisplay(DISPLAY_ORIENTATION_90);
5601
5602 // Set up the secondary display as the display on which the pointer should be shown,
5603 // and associate the InputDevice with the secondary display.
5604 prepareSecondaryDisplay();
5605 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5606 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5607 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5608
5609 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5610 mFakePointerController->setPosition(100, 200);
5611 mFakePointerController->setButtonState(0);
5612
5613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5615 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005617 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5618 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5619 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005620 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5621}
5622
5623TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5624 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5625
5626 // Set up the default display as the display on which the pointer should be shown.
5627 prepareDisplay(DISPLAY_ORIENTATION_90);
5628 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5629
5630 // Associate the InputDevice with the secondary display.
5631 prepareSecondaryDisplay();
5632 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5633 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5634
5635 // The mapper should not generate any events because it is associated with a display that is
5636 // different from the pointer display.
5637 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5638 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5639 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005641}
5642
Michael Wrightd02c5b62014-02-10 15:10:22 -08005643// --- TouchInputMapperTest ---
5644
5645class TouchInputMapperTest : public InputMapperTest {
5646protected:
5647 static const int32_t RAW_X_MIN;
5648 static const int32_t RAW_X_MAX;
5649 static const int32_t RAW_Y_MIN;
5650 static const int32_t RAW_Y_MAX;
5651 static const int32_t RAW_TOUCH_MIN;
5652 static const int32_t RAW_TOUCH_MAX;
5653 static const int32_t RAW_TOOL_MIN;
5654 static const int32_t RAW_TOOL_MAX;
5655 static const int32_t RAW_PRESSURE_MIN;
5656 static const int32_t RAW_PRESSURE_MAX;
5657 static const int32_t RAW_ORIENTATION_MIN;
5658 static const int32_t RAW_ORIENTATION_MAX;
5659 static const int32_t RAW_DISTANCE_MIN;
5660 static const int32_t RAW_DISTANCE_MAX;
5661 static const int32_t RAW_TILT_MIN;
5662 static const int32_t RAW_TILT_MAX;
5663 static const int32_t RAW_ID_MIN;
5664 static const int32_t RAW_ID_MAX;
5665 static const int32_t RAW_SLOT_MIN;
5666 static const int32_t RAW_SLOT_MAX;
5667 static const float X_PRECISION;
5668 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005669 static const float X_PRECISION_VIRTUAL;
5670 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005671
5672 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005673 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005674
5675 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5676
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005677 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005678 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005679
Michael Wrightd02c5b62014-02-10 15:10:22 -08005680 enum Axes {
5681 POSITION = 1 << 0,
5682 TOUCH = 1 << 1,
5683 TOOL = 1 << 2,
5684 PRESSURE = 1 << 3,
5685 ORIENTATION = 1 << 4,
5686 MINOR = 1 << 5,
5687 ID = 1 << 6,
5688 DISTANCE = 1 << 7,
5689 TILT = 1 << 8,
5690 SLOT = 1 << 9,
5691 TOOL_TYPE = 1 << 10,
5692 };
5693
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005694 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5695 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005696 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005697 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005698 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005699 int32_t toRawX(float displayX);
5700 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005701 int32_t toRotatedRawX(float displayX);
5702 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005703 float toCookedX(float rawX, float rawY);
5704 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005705 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005706 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005707 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005708 float toDisplayY(int32_t rawY, int32_t displayHeight);
5709
Michael Wrightd02c5b62014-02-10 15:10:22 -08005710};
5711
5712const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5713const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5714const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5715const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5716const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5717const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5718const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5719const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005720const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5721const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005722const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5723const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5724const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5725const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5726const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5727const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5728const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5729const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5730const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5731const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5732const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5733const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005734const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5735 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5736const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5737 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005738const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5739 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005740
5741const float TouchInputMapperTest::GEOMETRIC_SCALE =
5742 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5743 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5744
5745const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5746 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5747 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5748};
5749
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005750void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005751 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5752 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005753}
5754
5755void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5756 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5757 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005758}
5759
Santos Cordonfa5cf462017-04-05 10:37:00 -07005760void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005761 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5762 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5763 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005764}
5765
Michael Wrightd02c5b62014-02-10 15:10:22 -08005766void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005767 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5768 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5769 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5770 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005771}
5772
Jason Gerecke489fda82012-09-07 17:19:40 -07005773void TouchInputMapperTest::prepareLocationCalibration() {
5774 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5775}
5776
Michael Wrightd02c5b62014-02-10 15:10:22 -08005777int32_t TouchInputMapperTest::toRawX(float displayX) {
5778 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5779}
5780
5781int32_t TouchInputMapperTest::toRawY(float displayY) {
5782 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5783}
5784
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005785int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5786 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5787}
5788
5789int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5790 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5791}
5792
Jason Gerecke489fda82012-09-07 17:19:40 -07005793float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5794 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5795 return rawX;
5796}
5797
5798float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5799 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5800 return rawY;
5801}
5802
Michael Wrightd02c5b62014-02-10 15:10:22 -08005803float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005804 return toDisplayX(rawX, DISPLAY_WIDTH);
5805}
5806
5807float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5808 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005809}
5810
5811float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005812 return toDisplayY(rawY, DISPLAY_HEIGHT);
5813}
5814
5815float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5816 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005817}
5818
5819
5820// --- SingleTouchInputMapperTest ---
5821
5822class SingleTouchInputMapperTest : public TouchInputMapperTest {
5823protected:
5824 void prepareButtons();
5825 void prepareAxes(int axes);
5826
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005827 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5828 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5829 void processUp(SingleTouchInputMapper& mappery);
5830 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5831 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5832 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5833 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5834 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5835 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005836};
5837
5838void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005839 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005840}
5841
5842void SingleTouchInputMapperTest::prepareAxes(int axes) {
5843 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005844 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5845 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005846 }
5847 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005848 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5849 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005850 }
5851 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005852 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5853 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005854 }
5855 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005856 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5857 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005858 }
5859 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005860 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5861 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005862 }
5863}
5864
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005865void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005866 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5867 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5868 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005869}
5870
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005871void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005872 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5873 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005874}
5875
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005876void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005877 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005878}
5879
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005880void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005881 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005882}
5883
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005884void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5885 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005886 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005887}
5888
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005889void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005890 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005891}
5892
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005893void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5894 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005895 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5896 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005897}
5898
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005899void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5900 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005901 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005902}
5903
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005904void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005905 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005906}
5907
Michael Wrightd02c5b62014-02-10 15:10:22 -08005908TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005909 prepareButtons();
5910 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005911 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005912
Harry Cutts16a24cc2022-10-26 15:22:19 +00005913 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005914}
5915
Michael Wrightd02c5b62014-02-10 15:10:22 -08005916TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005917 prepareButtons();
5918 prepareAxes(POSITION);
5919 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005920 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005921
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005922 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005923}
5924
5925TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005926 addConfigurationProperty("touch.deviceType", "touchScreen");
5927 prepareDisplay(DISPLAY_ORIENTATION_0);
5928 prepareButtons();
5929 prepareAxes(POSITION);
5930 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005931 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005932
5933 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005934 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005935
5936 // Virtual key is down.
5937 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5938 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5939 processDown(mapper, x, y);
5940 processSync(mapper);
5941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5942
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005943 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005944
5945 // Virtual key is up.
5946 processUp(mapper);
5947 processSync(mapper);
5948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5949
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005950 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005951}
5952
5953TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005954 addConfigurationProperty("touch.deviceType", "touchScreen");
5955 prepareDisplay(DISPLAY_ORIENTATION_0);
5956 prepareButtons();
5957 prepareAxes(POSITION);
5958 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005959 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005960
5961 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005962 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005963
5964 // Virtual key is down.
5965 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5966 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5967 processDown(mapper, x, y);
5968 processSync(mapper);
5969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5970
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005971 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005972
5973 // Virtual key is up.
5974 processUp(mapper);
5975 processSync(mapper);
5976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5977
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005978 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005979}
5980
5981TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005982 addConfigurationProperty("touch.deviceType", "touchScreen");
5983 prepareDisplay(DISPLAY_ORIENTATION_0);
5984 prepareButtons();
5985 prepareAxes(POSITION);
5986 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005987 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005988
Michael Wrightd02c5b62014-02-10 15:10:22 -08005989 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005990 ASSERT_TRUE(
5991 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005992 ASSERT_TRUE(flags[0]);
5993 ASSERT_FALSE(flags[1]);
5994}
5995
5996TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005997 addConfigurationProperty("touch.deviceType", "touchScreen");
5998 prepareDisplay(DISPLAY_ORIENTATION_0);
5999 prepareButtons();
6000 prepareAxes(POSITION);
6001 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006002 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006003
arthurhungdcef2dc2020-08-11 14:47:50 +08006004 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006005
6006 NotifyKeyArgs args;
6007
6008 // Press virtual key.
6009 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6010 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6011 processDown(mapper, x, y);
6012 processSync(mapper);
6013
6014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
6015 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
6016 ASSERT_EQ(DEVICE_ID, args.deviceId);
6017 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
6018 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
6019 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
6020 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
6021 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
6022 ASSERT_EQ(KEY_HOME, args.scanCode);
6023 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
6024 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
6025
6026 // Release virtual key.
6027 processUp(mapper);
6028 processSync(mapper);
6029
6030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
6031 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
6032 ASSERT_EQ(DEVICE_ID, args.deviceId);
6033 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
6034 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
6035 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
6036 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
6037 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
6038 ASSERT_EQ(KEY_HOME, args.scanCode);
6039 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
6040 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
6041
6042 // Should not have sent any motions.
6043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6044}
6045
6046TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006047 addConfigurationProperty("touch.deviceType", "touchScreen");
6048 prepareDisplay(DISPLAY_ORIENTATION_0);
6049 prepareButtons();
6050 prepareAxes(POSITION);
6051 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006052 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006053
arthurhungdcef2dc2020-08-11 14:47:50 +08006054 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006055
6056 NotifyKeyArgs keyArgs;
6057
6058 // Press virtual key.
6059 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6060 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6061 processDown(mapper, x, y);
6062 processSync(mapper);
6063
6064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6065 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
6066 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
6067 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
6068 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
6069 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6070 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
6071 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
6072 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
6073 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
6074 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
6075
6076 // Move out of bounds. This should generate a cancel and a pointer down since we moved
6077 // into the display area.
6078 y -= 100;
6079 processMove(mapper, x, y);
6080 processSync(mapper);
6081
6082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6083 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
6084 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
6085 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
6086 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
6087 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6088 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
6089 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
6090 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
6091 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
6092 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
6093 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
6094
6095 NotifyMotionArgs motionArgs;
6096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6097 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6098 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6099 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6100 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6101 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6102 ASSERT_EQ(0, motionArgs.flags);
6103 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6104 ASSERT_EQ(0, motionArgs.buttonState);
6105 ASSERT_EQ(0, motionArgs.edgeFlags);
6106 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6107 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6108 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6109 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6110 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6111 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6112 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6113 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6114
6115 // Keep moving out of bounds. Should generate a pointer move.
6116 y -= 50;
6117 processMove(mapper, x, y);
6118 processSync(mapper);
6119
6120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6121 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6122 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6123 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6124 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6125 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6126 ASSERT_EQ(0, motionArgs.flags);
6127 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6128 ASSERT_EQ(0, motionArgs.buttonState);
6129 ASSERT_EQ(0, motionArgs.edgeFlags);
6130 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6131 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6132 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6134 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6135 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6136 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6137 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6138
6139 // Release out of bounds. Should generate a pointer up.
6140 processUp(mapper);
6141 processSync(mapper);
6142
6143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6144 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6145 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6146 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6147 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6148 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6149 ASSERT_EQ(0, motionArgs.flags);
6150 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6151 ASSERT_EQ(0, motionArgs.buttonState);
6152 ASSERT_EQ(0, motionArgs.edgeFlags);
6153 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6154 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6155 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6156 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6157 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6158 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6159 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6160 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6161
6162 // Should not have sent any more keys or motions.
6163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6165}
6166
6167TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006168 addConfigurationProperty("touch.deviceType", "touchScreen");
6169 prepareDisplay(DISPLAY_ORIENTATION_0);
6170 prepareButtons();
6171 prepareAxes(POSITION);
6172 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006173 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006174
arthurhungdcef2dc2020-08-11 14:47:50 +08006175 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006176
6177 NotifyMotionArgs motionArgs;
6178
6179 // Initially go down out of bounds.
6180 int32_t x = -10;
6181 int32_t y = -10;
6182 processDown(mapper, x, y);
6183 processSync(mapper);
6184
6185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6186
6187 // Move into the display area. Should generate a pointer down.
6188 x = 50;
6189 y = 75;
6190 processMove(mapper, x, y);
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(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6197 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6198 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6199 ASSERT_EQ(0, motionArgs.flags);
6200 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6201 ASSERT_EQ(0, motionArgs.buttonState);
6202 ASSERT_EQ(0, motionArgs.edgeFlags);
6203 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6204 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6205 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6206 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6207 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6208 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6209 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6210 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6211
6212 // Release. Should generate a pointer up.
6213 processUp(mapper);
6214 processSync(mapper);
6215
6216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6217 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6218 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6219 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6220 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6221 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6222 ASSERT_EQ(0, motionArgs.flags);
6223 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6224 ASSERT_EQ(0, motionArgs.buttonState);
6225 ASSERT_EQ(0, motionArgs.edgeFlags);
6226 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6227 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6228 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6229 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6230 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6231 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6232 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6233 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6234
6235 // Should not have sent any more keys or motions.
6236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6238}
6239
Santos Cordonfa5cf462017-04-05 10:37:00 -07006240TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07006241 addConfigurationProperty("touch.deviceType", "touchScreen");
6242 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
6243
6244 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
6245 prepareButtons();
6246 prepareAxes(POSITION);
6247 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006248 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07006249
arthurhungdcef2dc2020-08-11 14:47:50 +08006250 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006251
6252 NotifyMotionArgs motionArgs;
6253
6254 // Down.
6255 int32_t x = 100;
6256 int32_t y = 125;
6257 processDown(mapper, x, y);
6258 processSync(mapper);
6259
6260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6261 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6262 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6263 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6264 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6265 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6266 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6267 ASSERT_EQ(0, motionArgs.flags);
6268 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6269 ASSERT_EQ(0, motionArgs.buttonState);
6270 ASSERT_EQ(0, motionArgs.edgeFlags);
6271 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6272 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6273 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6275 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6276 1, 0, 0, 0, 0, 0, 0, 0));
6277 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6278 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6279 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6280
6281 // Move.
6282 x += 50;
6283 y += 75;
6284 processMove(mapper, x, y);
6285 processSync(mapper);
6286
6287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6288 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6289 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6290 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6291 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6292 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6294 ASSERT_EQ(0, motionArgs.flags);
6295 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6296 ASSERT_EQ(0, motionArgs.buttonState);
6297 ASSERT_EQ(0, motionArgs.edgeFlags);
6298 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6299 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6300 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6302 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6303 1, 0, 0, 0, 0, 0, 0, 0));
6304 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6305 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6306 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6307
6308 // Up.
6309 processUp(mapper);
6310 processSync(mapper);
6311
6312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6313 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6314 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6315 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6316 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6317 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6318 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6319 ASSERT_EQ(0, motionArgs.flags);
6320 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6321 ASSERT_EQ(0, motionArgs.buttonState);
6322 ASSERT_EQ(0, motionArgs.edgeFlags);
6323 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6324 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6325 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6327 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6328 1, 0, 0, 0, 0, 0, 0, 0));
6329 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6330 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6331 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6332
6333 // Should not have sent any more keys or motions.
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6336}
6337
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006339 addConfigurationProperty("touch.deviceType", "touchScreen");
6340 prepareDisplay(DISPLAY_ORIENTATION_0);
6341 prepareButtons();
6342 prepareAxes(POSITION);
6343 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006344 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006345
arthurhungdcef2dc2020-08-11 14:47:50 +08006346 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006347
6348 NotifyMotionArgs motionArgs;
6349
6350 // Down.
6351 int32_t x = 100;
6352 int32_t y = 125;
6353 processDown(mapper, x, y);
6354 processSync(mapper);
6355
6356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6357 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6358 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6359 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6360 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6361 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6362 ASSERT_EQ(0, motionArgs.flags);
6363 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6364 ASSERT_EQ(0, motionArgs.buttonState);
6365 ASSERT_EQ(0, motionArgs.edgeFlags);
6366 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6367 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6368 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6370 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6371 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6372 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6373 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6374
6375 // Move.
6376 x += 50;
6377 y += 75;
6378 processMove(mapper, x, y);
6379 processSync(mapper);
6380
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6382 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6383 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6384 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6385 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6387 ASSERT_EQ(0, motionArgs.flags);
6388 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6389 ASSERT_EQ(0, motionArgs.buttonState);
6390 ASSERT_EQ(0, motionArgs.edgeFlags);
6391 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6392 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6395 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6396 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6397 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6398 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6399
6400 // Up.
6401 processUp(mapper);
6402 processSync(mapper);
6403
6404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6405 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6406 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6407 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6408 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6409 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6410 ASSERT_EQ(0, motionArgs.flags);
6411 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6412 ASSERT_EQ(0, motionArgs.buttonState);
6413 ASSERT_EQ(0, motionArgs.edgeFlags);
6414 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6415 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6416 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6417 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6418 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6419 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6420 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6421 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6422
6423 // Should not have sent any more keys or motions.
6424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6426}
6427
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006428TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006429 addConfigurationProperty("touch.deviceType", "touchScreen");
6430 prepareButtons();
6431 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006432 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
6433 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006434 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006435
6436 NotifyMotionArgs args;
6437
6438 // Rotation 90.
6439 prepareDisplay(DISPLAY_ORIENTATION_90);
6440 processDown(mapper, toRawX(50), toRawY(75));
6441 processSync(mapper);
6442
6443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6444 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6445 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6446
6447 processUp(mapper);
6448 processSync(mapper);
6449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6450}
6451
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006452TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006453 addConfigurationProperty("touch.deviceType", "touchScreen");
6454 prepareButtons();
6455 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006456 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6457 // orientation-aware are affected by display rotation.
6458 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006459 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006460
6461 NotifyMotionArgs args;
6462
6463 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006464 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006465 prepareDisplay(DISPLAY_ORIENTATION_0);
6466 processDown(mapper, toRawX(50), toRawY(75));
6467 processSync(mapper);
6468
6469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6470 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6471 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6472
6473 processUp(mapper);
6474 processSync(mapper);
6475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6476
6477 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006478 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006479 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006480 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006481 processSync(mapper);
6482
6483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6484 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6485 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6486
6487 processUp(mapper);
6488 processSync(mapper);
6489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6490
6491 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006492 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006493 prepareDisplay(DISPLAY_ORIENTATION_180);
6494 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6495 processSync(mapper);
6496
6497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6498 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6499 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6500
6501 processUp(mapper);
6502 processSync(mapper);
6503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6504
6505 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006506 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006507 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006508 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006509 processSync(mapper);
6510
6511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6512 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6513 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6514
6515 processUp(mapper);
6516 processSync(mapper);
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6518}
6519
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006520TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6521 addConfigurationProperty("touch.deviceType", "touchScreen");
6522 prepareButtons();
6523 prepareAxes(POSITION);
6524 addConfigurationProperty("touch.orientationAware", "1");
6525 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6526 clearViewports();
6527 prepareDisplay(DISPLAY_ORIENTATION_0);
6528 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6529 NotifyMotionArgs args;
6530
6531 // Orientation 0.
6532 processDown(mapper, toRawX(50), toRawY(75));
6533 processSync(mapper);
6534
6535 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6536 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6537 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6538
6539 processUp(mapper);
6540 processSync(mapper);
6541 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6542}
6543
6544TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6545 addConfigurationProperty("touch.deviceType", "touchScreen");
6546 prepareButtons();
6547 prepareAxes(POSITION);
6548 addConfigurationProperty("touch.orientationAware", "1");
6549 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6550 clearViewports();
6551 prepareDisplay(DISPLAY_ORIENTATION_0);
6552 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6553 NotifyMotionArgs args;
6554
6555 // Orientation 90.
6556 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6557 processSync(mapper);
6558
6559 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6560 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6561 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6562
6563 processUp(mapper);
6564 processSync(mapper);
6565 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6566}
6567
6568TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6569 addConfigurationProperty("touch.deviceType", "touchScreen");
6570 prepareButtons();
6571 prepareAxes(POSITION);
6572 addConfigurationProperty("touch.orientationAware", "1");
6573 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6574 clearViewports();
6575 prepareDisplay(DISPLAY_ORIENTATION_0);
6576 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6577 NotifyMotionArgs args;
6578
6579 // Orientation 180.
6580 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6581 processSync(mapper);
6582
6583 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6584 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6585 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6586
6587 processUp(mapper);
6588 processSync(mapper);
6589 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6590}
6591
6592TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6593 addConfigurationProperty("touch.deviceType", "touchScreen");
6594 prepareButtons();
6595 prepareAxes(POSITION);
6596 addConfigurationProperty("touch.orientationAware", "1");
6597 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6598 clearViewports();
6599 prepareDisplay(DISPLAY_ORIENTATION_0);
6600 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6601 NotifyMotionArgs args;
6602
6603 // Orientation 270.
6604 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6605 processSync(mapper);
6606
6607 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6608 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6609 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6610
6611 processUp(mapper);
6612 processSync(mapper);
6613 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6614}
6615
6616TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6617 addConfigurationProperty("touch.deviceType", "touchScreen");
6618 prepareButtons();
6619 prepareAxes(POSITION);
6620 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6621 // orientation-aware are affected by display rotation.
6622 addConfigurationProperty("touch.orientationAware", "0");
6623 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6624 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6625
6626 NotifyMotionArgs args;
6627
6628 // Orientation 90, Rotation 0.
6629 clearViewports();
6630 prepareDisplay(DISPLAY_ORIENTATION_0);
6631 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6632 processSync(mapper);
6633
6634 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6635 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6636 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6637
6638 processUp(mapper);
6639 processSync(mapper);
6640 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6641
6642 // Orientation 90, Rotation 90.
6643 clearViewports();
6644 prepareDisplay(DISPLAY_ORIENTATION_90);
6645 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6646 processSync(mapper);
6647
6648 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6649 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6650 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6651
6652 processUp(mapper);
6653 processSync(mapper);
6654 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6655
6656 // Orientation 90, Rotation 180.
6657 clearViewports();
6658 prepareDisplay(DISPLAY_ORIENTATION_180);
6659 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6660 processSync(mapper);
6661
6662 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6663 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6664 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6665
6666 processUp(mapper);
6667 processSync(mapper);
6668 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6669
6670 // Orientation 90, Rotation 270.
6671 clearViewports();
6672 prepareDisplay(DISPLAY_ORIENTATION_270);
6673 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6674 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6675 processSync(mapper);
6676
6677 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6678 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6679 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6680
6681 processUp(mapper);
6682 processSync(mapper);
6683 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6684}
6685
Michael Wrightd02c5b62014-02-10 15:10:22 -08006686TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006687 addConfigurationProperty("touch.deviceType", "touchScreen");
6688 prepareDisplay(DISPLAY_ORIENTATION_0);
6689 prepareButtons();
6690 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006691 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006692
6693 // These calculations are based on the input device calibration documentation.
6694 int32_t rawX = 100;
6695 int32_t rawY = 200;
6696 int32_t rawPressure = 10;
6697 int32_t rawToolMajor = 12;
6698 int32_t rawDistance = 2;
6699 int32_t rawTiltX = 30;
6700 int32_t rawTiltY = 110;
6701
6702 float x = toDisplayX(rawX);
6703 float y = toDisplayY(rawY);
6704 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6705 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6706 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6707 float distance = float(rawDistance);
6708
6709 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6710 float tiltScale = M_PI / 180;
6711 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6712 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6713 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6714 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6715
6716 processDown(mapper, rawX, rawY);
6717 processPressure(mapper, rawPressure);
6718 processToolMajor(mapper, rawToolMajor);
6719 processDistance(mapper, rawDistance);
6720 processTilt(mapper, rawTiltX, rawTiltY);
6721 processSync(mapper);
6722
6723 NotifyMotionArgs args;
6724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6726 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6727 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6728}
6729
Jason Gerecke489fda82012-09-07 17:19:40 -07006730TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006731 addConfigurationProperty("touch.deviceType", "touchScreen");
6732 prepareDisplay(DISPLAY_ORIENTATION_0);
6733 prepareLocationCalibration();
6734 prepareButtons();
6735 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006736 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006737
6738 int32_t rawX = 100;
6739 int32_t rawY = 200;
6740
6741 float x = toDisplayX(toCookedX(rawX, rawY));
6742 float y = toDisplayY(toCookedY(rawX, rawY));
6743
6744 processDown(mapper, rawX, rawY);
6745 processSync(mapper);
6746
6747 NotifyMotionArgs args;
6748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6750 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6751}
6752
Michael Wrightd02c5b62014-02-10 15:10:22 -08006753TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006754 addConfigurationProperty("touch.deviceType", "touchScreen");
6755 prepareDisplay(DISPLAY_ORIENTATION_0);
6756 prepareButtons();
6757 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006758 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006759
6760 NotifyMotionArgs motionArgs;
6761 NotifyKeyArgs keyArgs;
6762
6763 processDown(mapper, 100, 200);
6764 processSync(mapper);
6765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6766 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6767 ASSERT_EQ(0, motionArgs.buttonState);
6768
6769 // press BTN_LEFT, release BTN_LEFT
6770 processKey(mapper, BTN_LEFT, 1);
6771 processSync(mapper);
6772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6773 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6774 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6775
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6777 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6778 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6779
Michael Wrightd02c5b62014-02-10 15:10:22 -08006780 processKey(mapper, BTN_LEFT, 0);
6781 processSync(mapper);
6782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006783 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006784 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006785
6786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006787 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006788 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006789
6790 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6791 processKey(mapper, BTN_RIGHT, 1);
6792 processKey(mapper, BTN_MIDDLE, 1);
6793 processSync(mapper);
6794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6795 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6796 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6797 motionArgs.buttonState);
6798
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6800 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6801 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6802
6803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6804 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6805 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6806 motionArgs.buttonState);
6807
Michael Wrightd02c5b62014-02-10 15:10:22 -08006808 processKey(mapper, BTN_RIGHT, 0);
6809 processSync(mapper);
6810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006811 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006812 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006813
6814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006815 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006816 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006817
6818 processKey(mapper, BTN_MIDDLE, 0);
6819 processSync(mapper);
6820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006821 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006822 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006823
6824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006825 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006826 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006827
6828 // press BTN_BACK, release BTN_BACK
6829 processKey(mapper, BTN_BACK, 1);
6830 processSync(mapper);
6831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6832 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6833 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006834
Michael Wrightd02c5b62014-02-10 15:10:22 -08006835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006836 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006837 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6838
6839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6840 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6841 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006842
6843 processKey(mapper, BTN_BACK, 0);
6844 processSync(mapper);
6845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006846 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006847 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006848
6849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006851 ASSERT_EQ(0, motionArgs.buttonState);
6852
Michael Wrightd02c5b62014-02-10 15:10:22 -08006853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6854 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6855 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6856
6857 // press BTN_SIDE, release BTN_SIDE
6858 processKey(mapper, BTN_SIDE, 1);
6859 processSync(mapper);
6860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6861 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6862 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006863
Michael Wrightd02c5b62014-02-10 15:10:22 -08006864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006865 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006866 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6867
6868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6869 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6870 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006871
6872 processKey(mapper, BTN_SIDE, 0);
6873 processSync(mapper);
6874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006875 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006876 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006877
6878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006879 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006880 ASSERT_EQ(0, motionArgs.buttonState);
6881
Michael Wrightd02c5b62014-02-10 15:10:22 -08006882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6883 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6884 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6885
6886 // press BTN_FORWARD, release BTN_FORWARD
6887 processKey(mapper, BTN_FORWARD, 1);
6888 processSync(mapper);
6889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6890 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6891 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006892
Michael Wrightd02c5b62014-02-10 15:10:22 -08006893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006894 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006895 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6896
6897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6898 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6899 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006900
6901 processKey(mapper, BTN_FORWARD, 0);
6902 processSync(mapper);
6903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006904 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006905 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006906
6907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006908 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006909 ASSERT_EQ(0, motionArgs.buttonState);
6910
Michael Wrightd02c5b62014-02-10 15:10:22 -08006911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6912 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6913 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6914
6915 // press BTN_EXTRA, release BTN_EXTRA
6916 processKey(mapper, BTN_EXTRA, 1);
6917 processSync(mapper);
6918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6919 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6920 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006921
Michael Wrightd02c5b62014-02-10 15:10:22 -08006922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006923 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006924 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6925
6926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6927 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6928 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006929
6930 processKey(mapper, BTN_EXTRA, 0);
6931 processSync(mapper);
6932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006933 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006934 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006935
6936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006937 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006938 ASSERT_EQ(0, motionArgs.buttonState);
6939
Michael Wrightd02c5b62014-02-10 15:10:22 -08006940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6941 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6942 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6943
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6945
Michael Wrightd02c5b62014-02-10 15:10:22 -08006946 // press BTN_STYLUS, release BTN_STYLUS
6947 processKey(mapper, BTN_STYLUS, 1);
6948 processSync(mapper);
6949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006951 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6952
6953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6954 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6955 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006956
6957 processKey(mapper, BTN_STYLUS, 0);
6958 processSync(mapper);
6959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006960 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006961 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006962
6963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006964 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006965 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006966
6967 // press BTN_STYLUS2, release BTN_STYLUS2
6968 processKey(mapper, BTN_STYLUS2, 1);
6969 processSync(mapper);
6970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6971 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006972 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6973
6974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6975 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6976 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006977
6978 processKey(mapper, BTN_STYLUS2, 0);
6979 processSync(mapper);
6980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006981 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006982 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006983
6984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006985 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006986 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006987
6988 // release touch
6989 processUp(mapper);
6990 processSync(mapper);
6991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6992 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6993 ASSERT_EQ(0, motionArgs.buttonState);
6994}
6995
6996TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006997 addConfigurationProperty("touch.deviceType", "touchScreen");
6998 prepareDisplay(DISPLAY_ORIENTATION_0);
6999 prepareButtons();
7000 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007001 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007002
7003 NotifyMotionArgs motionArgs;
7004
7005 // default tool type is finger
7006 processDown(mapper, 100, 200);
7007 processSync(mapper);
7008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7009 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7010 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7011
7012 // eraser
7013 processKey(mapper, BTN_TOOL_RUBBER, 1);
7014 processSync(mapper);
7015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7016 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7017 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7018
7019 // stylus
7020 processKey(mapper, BTN_TOOL_RUBBER, 0);
7021 processKey(mapper, BTN_TOOL_PEN, 1);
7022 processSync(mapper);
7023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7025 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7026
7027 // brush
7028 processKey(mapper, BTN_TOOL_PEN, 0);
7029 processKey(mapper, BTN_TOOL_BRUSH, 1);
7030 processSync(mapper);
7031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7032 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7033 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7034
7035 // pencil
7036 processKey(mapper, BTN_TOOL_BRUSH, 0);
7037 processKey(mapper, BTN_TOOL_PENCIL, 1);
7038 processSync(mapper);
7039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7040 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7042
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007043 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007044 processKey(mapper, BTN_TOOL_PENCIL, 0);
7045 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7046 processSync(mapper);
7047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7048 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7049 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7050
7051 // mouse
7052 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7053 processKey(mapper, BTN_TOOL_MOUSE, 1);
7054 processSync(mapper);
7055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7056 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7057 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7058
7059 // lens
7060 processKey(mapper, BTN_TOOL_MOUSE, 0);
7061 processKey(mapper, BTN_TOOL_LENS, 1);
7062 processSync(mapper);
7063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7065 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7066
7067 // double-tap
7068 processKey(mapper, BTN_TOOL_LENS, 0);
7069 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7070 processSync(mapper);
7071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7073 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7074
7075 // triple-tap
7076 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7077 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7078 processSync(mapper);
7079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7080 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7081 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7082
7083 // quad-tap
7084 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7085 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7086 processSync(mapper);
7087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7088 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7089 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7090
7091 // finger
7092 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7093 processKey(mapper, BTN_TOOL_FINGER, 1);
7094 processSync(mapper);
7095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7096 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7097 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7098
7099 // stylus trumps finger
7100 processKey(mapper, BTN_TOOL_PEN, 1);
7101 processSync(mapper);
7102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7103 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7104 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7105
7106 // eraser trumps stylus
7107 processKey(mapper, BTN_TOOL_RUBBER, 1);
7108 processSync(mapper);
7109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7110 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7111 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7112
7113 // mouse trumps eraser
7114 processKey(mapper, BTN_TOOL_MOUSE, 1);
7115 processSync(mapper);
7116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7118 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7119
7120 // back to default tool type
7121 processKey(mapper, BTN_TOOL_MOUSE, 0);
7122 processKey(mapper, BTN_TOOL_RUBBER, 0);
7123 processKey(mapper, BTN_TOOL_PEN, 0);
7124 processKey(mapper, BTN_TOOL_FINGER, 0);
7125 processSync(mapper);
7126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7127 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7128 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7129}
7130
7131TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007132 addConfigurationProperty("touch.deviceType", "touchScreen");
7133 prepareDisplay(DISPLAY_ORIENTATION_0);
7134 prepareButtons();
7135 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007136 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007137 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007138
7139 NotifyMotionArgs motionArgs;
7140
7141 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7142 processKey(mapper, BTN_TOOL_FINGER, 1);
7143 processMove(mapper, 100, 200);
7144 processSync(mapper);
7145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7146 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7147 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7148 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7149
7150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7151 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7152 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7153 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7154
7155 // move a little
7156 processMove(mapper, 150, 250);
7157 processSync(mapper);
7158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7159 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7160 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7161 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7162
7163 // down when BTN_TOUCH is pressed, pressure defaults to 1
7164 processKey(mapper, BTN_TOUCH, 1);
7165 processSync(mapper);
7166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7167 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7168 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7169 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7170
7171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7172 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7173 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7174 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7175
7176 // up when BTN_TOUCH is released, hover restored
7177 processKey(mapper, BTN_TOUCH, 0);
7178 processSync(mapper);
7179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7180 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7182 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7183
7184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7185 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7186 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7187 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7188
7189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7190 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7191 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7192 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7193
7194 // exit hover when pointer goes away
7195 processKey(mapper, BTN_TOOL_FINGER, 0);
7196 processSync(mapper);
7197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7198 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7199 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7200 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7201}
7202
7203TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007204 addConfigurationProperty("touch.deviceType", "touchScreen");
7205 prepareDisplay(DISPLAY_ORIENTATION_0);
7206 prepareButtons();
7207 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007208 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007209
7210 NotifyMotionArgs motionArgs;
7211
7212 // initially hovering because pressure is 0
7213 processDown(mapper, 100, 200);
7214 processPressure(mapper, 0);
7215 processSync(mapper);
7216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7217 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7219 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7220
7221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7222 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7224 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7225
7226 // move a little
7227 processMove(mapper, 150, 250);
7228 processSync(mapper);
7229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7230 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7232 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7233
7234 // down when pressure is non-zero
7235 processPressure(mapper, RAW_PRESSURE_MAX);
7236 processSync(mapper);
7237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7238 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7239 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7240 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7241
7242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7243 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7245 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7246
7247 // up when pressure becomes 0, hover restored
7248 processPressure(mapper, 0);
7249 processSync(mapper);
7250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7251 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7252 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7253 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7254
7255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7256 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7258 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7259
7260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7261 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7263 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7264
7265 // exit hover when pointer goes away
7266 processUp(mapper);
7267 processSync(mapper);
7268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7269 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7271 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7272}
7273
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007274TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
7275 addConfigurationProperty("touch.deviceType", "touchScreen");
7276 prepareDisplay(DISPLAY_ORIENTATION_0);
7277 prepareButtons();
7278 prepareAxes(POSITION | PRESSURE);
7279 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7280
7281 // Touch down.
7282 processDown(mapper, 100, 200);
7283 processPressure(mapper, 1);
7284 processSync(mapper);
7285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7286 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
7287
7288 // Reset the mapper. This should cancel the ongoing gesture.
7289 resetMapper(mapper, ARBITRARY_TIME);
7290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7291 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
7292
7293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7294}
7295
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007296TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
7297 addConfigurationProperty("touch.deviceType", "touchScreen");
7298 prepareDisplay(DISPLAY_ORIENTATION_0);
7299 prepareButtons();
7300 prepareAxes(POSITION | PRESSURE);
7301 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7302
7303 // Set the initial state for the touch pointer.
7304 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
7305 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
7306 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
7307 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7308
7309 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007310 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
7311 // does not generate any events.
7312 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007313
7314 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
7315 // the recreated touch state to generate a down event.
7316 processSync(mapper);
7317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7318 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
7319
7320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7321}
7322
lilinnan687e58f2022-07-19 16:00:50 +08007323TEST_F(SingleTouchInputMapperTest,
7324 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
7325 addConfigurationProperty("touch.deviceType", "touchScreen");
7326 prepareDisplay(DISPLAY_ORIENTATION_0);
7327 prepareButtons();
7328 prepareAxes(POSITION);
7329 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7330 NotifyMotionArgs motionArgs;
7331
7332 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00007333 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08007334 processSync(mapper);
7335
7336 // We should receive a down event
7337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7338 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7339
7340 // Change display id
7341 clearViewports();
7342 prepareSecondaryDisplay(ViewportType::INTERNAL);
7343
7344 // We should receive a cancel event
7345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7346 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7347 // Then receive reset called
7348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7349}
7350
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007351TEST_F(SingleTouchInputMapperTest,
7352 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
7353 addConfigurationProperty("touch.deviceType", "touchScreen");
7354 prepareDisplay(DISPLAY_ORIENTATION_0);
7355 prepareButtons();
7356 prepareAxes(POSITION);
7357 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7359 NotifyMotionArgs motionArgs;
7360
7361 // Start a new gesture.
7362 processDown(mapper, 100, 200);
7363 processSync(mapper);
7364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7365 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7366
7367 // Make the viewport inactive. This will put the device in disabled mode.
7368 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7369 viewport->isActive = false;
7370 mFakePolicy->updateViewport(*viewport);
7371 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7372
7373 // We should receive a cancel event for the ongoing gesture.
7374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7375 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7376 // Then we should be notified that the device was reset.
7377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7378
7379 // No events are generated while the viewport is inactive.
7380 processMove(mapper, 101, 201);
7381 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007382 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007383 processSync(mapper);
7384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7385
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007386 // Start a new gesture while the viewport is still inactive.
7387 processDown(mapper, 300, 400);
7388 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
7389 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
7390 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7391 processSync(mapper);
7392
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007393 // Make the viewport active again. The device should resume processing events.
7394 viewport->isActive = true;
7395 mFakePolicy->updateViewport(*viewport);
7396 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7397
7398 // The device is reset because it changes back to direct mode, without generating any events.
7399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7401
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007402 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007403 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7405 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007406
7407 // No more events.
7408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
7410}
7411
Prabir Pradhan211ba622022-10-31 21:09:21 +00007412TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
7413 addConfigurationProperty("touch.deviceType", "touchScreen");
7414 prepareDisplay(DISPLAY_ORIENTATION_0);
7415 prepareButtons();
7416 prepareAxes(POSITION);
7417 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7419
7420 // Press a stylus button.
7421 processKey(mapper, BTN_STYLUS, 1);
7422 processSync(mapper);
7423
7424 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
7425 processDown(mapper, 100, 200);
7426 processSync(mapper);
7427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7428 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7429 WithCoords(toDisplayX(100), toDisplayY(200)),
7430 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7432 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7433 WithCoords(toDisplayX(100), toDisplayY(200)),
7434 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7435
7436 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
7437 // the button has not actually been released, since there will be no pointers through which the
7438 // button state can be reported. The event is generated at the location of the pointer before
7439 // it went up.
7440 processUp(mapper);
7441 processSync(mapper);
7442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7443 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7444 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7446 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7447 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7448}
7449
Prabir Pradhan5632d622021-09-06 07:57:20 -07007450// --- TouchDisplayProjectionTest ---
7451
7452class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7453public:
7454 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7455 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7456 // rotated equivalent of the given un-rotated physical display bounds.
7457 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
7458 uint32_t inverseRotationFlags;
7459 auto width = DISPLAY_WIDTH;
7460 auto height = DISPLAY_HEIGHT;
7461 switch (orientation) {
7462 case DISPLAY_ORIENTATION_90:
7463 inverseRotationFlags = ui::Transform::ROT_270;
7464 std::swap(width, height);
7465 break;
7466 case DISPLAY_ORIENTATION_180:
7467 inverseRotationFlags = ui::Transform::ROT_180;
7468 break;
7469 case DISPLAY_ORIENTATION_270:
7470 inverseRotationFlags = ui::Transform::ROT_90;
7471 std::swap(width, height);
7472 break;
7473 case DISPLAY_ORIENTATION_0:
7474 inverseRotationFlags = ui::Transform::ROT_0;
7475 break;
7476 default:
7477 FAIL() << "Invalid orientation: " << orientation;
7478 }
7479
7480 const ui::Transform rotation(inverseRotationFlags, width, height);
7481 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7482
7483 std::optional<DisplayViewport> internalViewport =
7484 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7485 DisplayViewport& v = *internalViewport;
7486 v.displayId = DISPLAY_ID;
7487 v.orientation = orientation;
7488
7489 v.logicalLeft = 0;
7490 v.logicalTop = 0;
7491 v.logicalRight = 100;
7492 v.logicalBottom = 100;
7493
7494 v.physicalLeft = rotatedPhysicalDisplay.left;
7495 v.physicalTop = rotatedPhysicalDisplay.top;
7496 v.physicalRight = rotatedPhysicalDisplay.right;
7497 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7498
7499 v.deviceWidth = width;
7500 v.deviceHeight = height;
7501
7502 v.isActive = true;
7503 v.uniqueId = UNIQUE_ID;
7504 v.type = ViewportType::INTERNAL;
7505 mFakePolicy->updateViewport(v);
7506 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7507 }
7508
7509 void assertReceivedMove(const Point& point) {
7510 NotifyMotionArgs motionArgs;
7511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7513 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7515 1, 0, 0, 0, 0, 0, 0, 0));
7516 }
7517};
7518
7519TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7520 addConfigurationProperty("touch.deviceType", "touchScreen");
7521 prepareDisplay(DISPLAY_ORIENTATION_0);
7522
7523 prepareButtons();
7524 prepareAxes(POSITION);
7525 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7526
7527 NotifyMotionArgs motionArgs;
7528
7529 // Configure the DisplayViewport such that the logical display maps to a subsection of
7530 // the display panel called the physical display. Here, the physical display is bounded by the
7531 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7532 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7533 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7534 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7535
7536 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7537 DISPLAY_ORIENTATION_270}) {
7538 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7539
7540 // Touches outside the physical display should be ignored, and should not generate any
7541 // events. Ensure touches at the following points that lie outside of the physical display
7542 // area do not generate any events.
7543 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7544 processDown(mapper, toRawX(point.x), toRawY(point.y));
7545 processSync(mapper);
7546 processUp(mapper);
7547 processSync(mapper);
7548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7549 << "Unexpected event generated for touch outside physical display at point: "
7550 << point.x << ", " << point.y;
7551 }
7552 }
7553}
7554
7555TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7556 addConfigurationProperty("touch.deviceType", "touchScreen");
7557 prepareDisplay(DISPLAY_ORIENTATION_0);
7558
7559 prepareButtons();
7560 prepareAxes(POSITION);
7561 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7562
7563 NotifyMotionArgs motionArgs;
7564
7565 // Configure the DisplayViewport such that the logical display maps to a subsection of
7566 // the display panel called the physical display. Here, the physical display is bounded by the
7567 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7568 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7569
7570 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7571 DISPLAY_ORIENTATION_270}) {
7572 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7573
7574 // Touches that start outside the physical display should be ignored until it enters the
7575 // physical display bounds, at which point it should generate a down event. Start a touch at
7576 // the point (5, 100), which is outside the physical display bounds.
7577 static const Point kOutsidePoint{5, 100};
7578 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7579 processSync(mapper);
7580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7581
7582 // Move the touch into the physical display area. This should generate a pointer down.
7583 processMove(mapper, toRawX(11), toRawY(21));
7584 processSync(mapper);
7585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7586 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7587 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7588 ASSERT_NO_FATAL_FAILURE(
7589 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7590
7591 // Move the touch inside the physical display area. This should generate a pointer move.
7592 processMove(mapper, toRawX(69), toRawY(159));
7593 processSync(mapper);
7594 assertReceivedMove({69, 159});
7595
7596 // Move outside the physical display area. Since the pointer is already down, this should
7597 // now continue generating events.
7598 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7599 processSync(mapper);
7600 assertReceivedMove(kOutsidePoint);
7601
7602 // Release. This should generate a pointer up.
7603 processUp(mapper);
7604 processSync(mapper);
7605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7606 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7608 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7609
7610 // Ensure no more events were generated.
7611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7613 }
7614}
7615
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007616// --- ExternalStylusFusionTest ---
7617
7618class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7619public:
7620 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7621 addConfigurationProperty("touch.deviceType", "touchScreen");
7622 prepareDisplay(DISPLAY_ORIENTATION_0);
7623 prepareButtons();
7624 prepareAxes(POSITION);
7625 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7626
7627 mStylusState.when = ARBITRARY_TIME;
7628 mStylusState.pressure = 0.f;
7629 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7630 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
7631 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
7632 processExternalStylusState(mapper);
7633 return mapper;
7634 }
7635
7636 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7637 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7638 for (const NotifyArgs& args : generatedArgs) {
7639 mFakeListener->notify(args);
7640 }
7641 // Loop the reader to flush the input listener queue.
7642 mReader->loopOnce();
7643 return generatedArgs;
7644 }
7645
7646protected:
7647 StylusState mStylusState{};
7648 static constexpr uint32_t EXPECTED_SOURCE =
7649 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7650
7651 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7652 auto toolTypeSource =
7653 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7654
7655 // The first pointer is withheld.
7656 processDown(mapper, 100, 200);
7657 processSync(mapper);
7658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7659 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7660 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7661
7662 // The external stylus reports pressure. The withheld finger pointer is released as a
7663 // stylus.
7664 mStylusState.pressure = 1.f;
7665 processExternalStylusState(mapper);
7666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7667 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7668 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7669
7670 // Subsequent pointer events are not withheld.
7671 processMove(mapper, 101, 201);
7672 processSync(mapper);
7673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7674 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7675
7676 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7678 }
7679
7680 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7681 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7682
7683 // Releasing the touch pointer ends the gesture.
7684 processUp(mapper);
7685 processSync(mapper);
7686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7687 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7688 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7689
7690 mStylusState.pressure = 0.f;
7691 processExternalStylusState(mapper);
7692 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7694 }
7695
7696 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7697 auto toolTypeSource =
7698 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER));
7699
7700 // The first pointer is withheld when an external stylus is connected,
7701 // and a timeout is requested.
7702 processDown(mapper, 100, 200);
7703 processSync(mapper);
7704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7705 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7706 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7707
7708 // If the timeout expires early, it is requested again.
7709 handleTimeout(mapper, ARBITRARY_TIME + 1);
7710 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7711 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7712
7713 // When the timeout expires, the withheld touch is released as a finger pointer.
7714 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7716 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7717
7718 // Subsequent pointer events are not withheld.
7719 processMove(mapper, 101, 201);
7720 processSync(mapper);
7721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7722 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7723 processUp(mapper);
7724 processSync(mapper);
7725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7726 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7727
7728 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7730 }
7731
7732private:
7733 InputDeviceInfo mExternalStylusDeviceInfo{};
7734};
7735
7736TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7737 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7738 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7739}
7740
7741TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7742 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7743 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7744}
7745
7746TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7747 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7748 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7749}
7750
7751// Test a successful stylus fusion gesture where the pressure is reported by the external
7752// before the touch is reported by the touchscreen.
7753TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7754 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7755 auto toolTypeSource =
7756 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7757
7758 // The external stylus reports pressure first. It is ignored for now.
7759 mStylusState.pressure = 1.f;
7760 processExternalStylusState(mapper);
7761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7762 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7763
7764 // When the touch goes down afterwards, it is reported as a stylus pointer.
7765 processDown(mapper, 100, 200);
7766 processSync(mapper);
7767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7768 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7769 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7770
7771 processMove(mapper, 101, 201);
7772 processSync(mapper);
7773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7774 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7775 processUp(mapper);
7776 processSync(mapper);
7777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7778 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7779
7780 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7782}
7783
7784TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7785 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7786
7787 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7788 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7789
7790 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7791 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7792 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7793 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7794}
7795
7796TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7797 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7798 auto toolTypeSource =
7799 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7800
7801 mStylusState.pressure = 0.8f;
7802 processExternalStylusState(mapper);
7803 processDown(mapper, 100, 200);
7804 processSync(mapper);
7805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7806 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7807 WithPressure(0.8f))));
7808 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7809
7810 // The external stylus reports a pressure change. We wait for some time for a touch event.
7811 mStylusState.pressure = 0.6f;
7812 processExternalStylusState(mapper);
7813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7814 ASSERT_NO_FATAL_FAILURE(
7815 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7816
7817 // If a touch is reported within the timeout, it reports the updated pressure.
7818 processMove(mapper, 101, 201);
7819 processSync(mapper);
7820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7821 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7822 WithPressure(0.6f))));
7823 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7824
7825 // There is another pressure change.
7826 mStylusState.pressure = 0.5f;
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 pressure.
7834 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7836 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7837 WithPressure(0.5f))));
7838
7839 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7840 // repeated indefinitely.
7841 mStylusState.pressure = 0.0f;
7842 processExternalStylusState(mapper);
7843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7844 ASSERT_NO_FATAL_FAILURE(
7845 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7846 processMove(mapper, 102, 202);
7847 processSync(mapper);
7848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7849 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7850 WithPressure(0.5f))));
7851 processMove(mapper, 103, 203);
7852 processSync(mapper);
7853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7854 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7855 WithPressure(0.5f))));
7856
7857 processUp(mapper);
7858 processSync(mapper);
7859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7860 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7861 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7862
7863 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7865}
7866
7867TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7868 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7869 auto source = WithSource(EXPECTED_SOURCE);
7870
7871 mStylusState.pressure = 1.f;
7872 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_ERASER;
7873 processExternalStylusState(mapper);
7874 processDown(mapper, 100, 200);
7875 processSync(mapper);
7876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7877 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7878 WithToolType(AMOTION_EVENT_TOOL_TYPE_ERASER))));
7879 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7880
7881 // The external stylus reports a tool change. We wait for some time for a touch event.
7882 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7883 processExternalStylusState(mapper);
7884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7885 ASSERT_NO_FATAL_FAILURE(
7886 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7887
7888 // If a touch is reported within the timeout, it reports the updated pressure.
7889 processMove(mapper, 101, 201);
7890 processSync(mapper);
7891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7892 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7893 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7894 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7895
7896 // There is another tool type change.
7897 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
7898 processExternalStylusState(mapper);
7899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7900 ASSERT_NO_FATAL_FAILURE(
7901 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7902
7903 // If a touch is not reported within the timeout, a move event is generated to report
7904 // the new tool type.
7905 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7907 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7908 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7909
7910 processUp(mapper);
7911 processSync(mapper);
7912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7913 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
7914 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7915
7916 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7918}
7919
7920TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7921 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7922 auto toolTypeSource =
7923 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7924
7925 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7926
7927 // The external stylus reports a button change. We wait for some time for a touch event.
7928 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7929 processExternalStylusState(mapper);
7930 ASSERT_NO_FATAL_FAILURE(
7931 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7932
7933 // If a touch is reported within the timeout, it reports the updated button state.
7934 processMove(mapper, 101, 201);
7935 processSync(mapper);
7936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7937 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7938 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7940 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7941 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7942 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7943
7944 // The button is now released.
7945 mStylusState.buttons = 0;
7946 processExternalStylusState(mapper);
7947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7948 ASSERT_NO_FATAL_FAILURE(
7949 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7950
7951 // If a touch is not reported within the timeout, a move event is generated to report
7952 // the new button state.
7953 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7954 // TODO(prabirmsp): Fix fused stylus button releases being handled inconsistently.
7955 // The button release event should be sent here, but isn't.
7956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7957 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7958 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7959
7960 processUp(mapper);
7961 processSync(mapper);
7962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7963 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7964 WithButtonState(0))));
7965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7966 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7967
7968 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7970}
7971
Michael Wrightd02c5b62014-02-10 15:10:22 -08007972// --- MultiTouchInputMapperTest ---
7973
7974class MultiTouchInputMapperTest : public TouchInputMapperTest {
7975protected:
7976 void prepareAxes(int axes);
7977
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007978 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7979 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7980 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7981 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7982 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7983 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7984 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7985 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7986 void processId(MultiTouchInputMapper& mapper, int32_t id);
7987 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7988 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7989 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007990 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007991 void processMTSync(MultiTouchInputMapper& mapper);
7992 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007993};
7994
7995void MultiTouchInputMapperTest::prepareAxes(int axes) {
7996 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007997 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7998 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007999 }
8000 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008001 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
8002 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008003 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008004 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
8005 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008006 }
8007 }
8008 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008009 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8010 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008011 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008012 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008013 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008014 }
8015 }
8016 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008017 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
8018 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008019 }
8020 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008021 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
8022 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008023 }
8024 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008025 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
8026 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008027 }
8028 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008029 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
8030 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008031 }
8032 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008033 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
8034 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008035 }
8036 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008037 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008038 }
8039}
8040
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008041void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
8042 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
8044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008045}
8046
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008047void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
8048 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008049 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008050}
8051
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008052void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
8053 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008054 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008055}
8056
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008057void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008058 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008059}
8060
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008061void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008062 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008063}
8064
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008065void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
8066 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008068}
8069
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008070void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072}
8073
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008074void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008075 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008076}
8077
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008078void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008080}
8081
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008082void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008084}
8085
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008086void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008087 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008088}
8089
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008090void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
8091 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008093}
8094
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008095void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
8096 int32_t value) {
8097 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
8098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
8099}
8100
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008101void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008102 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008103}
8104
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008105void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008107}
8108
Michael Wrightd02c5b62014-02-10 15:10:22 -08008109TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008110 addConfigurationProperty("touch.deviceType", "touchScreen");
8111 prepareDisplay(DISPLAY_ORIENTATION_0);
8112 prepareAxes(POSITION);
8113 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008114 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008115
arthurhungdcef2dc2020-08-11 14:47:50 +08008116 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008117
8118 NotifyMotionArgs motionArgs;
8119
8120 // Two fingers down at once.
8121 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8122 processPosition(mapper, x1, y1);
8123 processMTSync(mapper);
8124 processPosition(mapper, x2, y2);
8125 processMTSync(mapper);
8126 processSync(mapper);
8127
8128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8129 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8130 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8131 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8132 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8133 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8134 ASSERT_EQ(0, motionArgs.flags);
8135 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8136 ASSERT_EQ(0, motionArgs.buttonState);
8137 ASSERT_EQ(0, motionArgs.edgeFlags);
8138 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8139 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8140 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8141 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8142 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8143 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8144 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8145 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8146
8147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8148 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8149 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8150 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8151 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008152 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008153 ASSERT_EQ(0, motionArgs.flags);
8154 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8155 ASSERT_EQ(0, motionArgs.buttonState);
8156 ASSERT_EQ(0, motionArgs.edgeFlags);
8157 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8158 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8159 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8160 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8161 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8162 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8163 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8164 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8165 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8166 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8167 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8168 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8169
8170 // Move.
8171 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8172 processPosition(mapper, x1, y1);
8173 processMTSync(mapper);
8174 processPosition(mapper, x2, y2);
8175 processMTSync(mapper);
8176 processSync(mapper);
8177
8178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8179 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8180 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8181 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8182 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8183 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8184 ASSERT_EQ(0, motionArgs.flags);
8185 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8186 ASSERT_EQ(0, motionArgs.buttonState);
8187 ASSERT_EQ(0, motionArgs.edgeFlags);
8188 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8189 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8190 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8191 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8192 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8194 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8195 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8196 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8197 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8198 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8199 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8200
8201 // First finger up.
8202 x2 += 15; y2 -= 20;
8203 processPosition(mapper, x2, y2);
8204 processMTSync(mapper);
8205 processSync(mapper);
8206
8207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8208 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8209 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8210 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8211 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008212 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008213 ASSERT_EQ(0, motionArgs.flags);
8214 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8215 ASSERT_EQ(0, motionArgs.buttonState);
8216 ASSERT_EQ(0, motionArgs.edgeFlags);
8217 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8218 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8219 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8220 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8221 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8222 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8223 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8225 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8226 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8227 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8228 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8229
8230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8231 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8232 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8233 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8234 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8236 ASSERT_EQ(0, motionArgs.flags);
8237 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8238 ASSERT_EQ(0, motionArgs.buttonState);
8239 ASSERT_EQ(0, motionArgs.edgeFlags);
8240 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8241 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8242 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8243 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8244 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8245 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8246 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8247 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8248
8249 // Move.
8250 x2 += 20; y2 -= 25;
8251 processPosition(mapper, x2, y2);
8252 processMTSync(mapper);
8253 processSync(mapper);
8254
8255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8256 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8257 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8258 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8259 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8261 ASSERT_EQ(0, motionArgs.flags);
8262 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8263 ASSERT_EQ(0, motionArgs.buttonState);
8264 ASSERT_EQ(0, motionArgs.edgeFlags);
8265 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8266 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8267 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8269 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8270 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8271 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8272 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8273
8274 // New finger down.
8275 int32_t x3 = 700, y3 = 300;
8276 processPosition(mapper, x2, y2);
8277 processMTSync(mapper);
8278 processPosition(mapper, x3, y3);
8279 processMTSync(mapper);
8280 processSync(mapper);
8281
8282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8283 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8284 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8285 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8286 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008287 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008288 ASSERT_EQ(0, motionArgs.flags);
8289 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8290 ASSERT_EQ(0, motionArgs.buttonState);
8291 ASSERT_EQ(0, motionArgs.edgeFlags);
8292 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8293 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8294 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8295 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8296 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8298 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8300 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8301 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8302 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8303 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8304
8305 // Second finger up.
8306 x3 += 30; y3 -= 20;
8307 processPosition(mapper, x3, y3);
8308 processMTSync(mapper);
8309 processSync(mapper);
8310
8311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8312 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8313 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8314 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8315 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008316 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008317 ASSERT_EQ(0, motionArgs.flags);
8318 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8319 ASSERT_EQ(0, motionArgs.buttonState);
8320 ASSERT_EQ(0, motionArgs.edgeFlags);
8321 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8322 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8323 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8324 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8325 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8327 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8328 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8329 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8330 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8331 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8332 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8333
8334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8335 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8336 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8337 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8338 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8340 ASSERT_EQ(0, motionArgs.flags);
8341 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8342 ASSERT_EQ(0, motionArgs.buttonState);
8343 ASSERT_EQ(0, motionArgs.edgeFlags);
8344 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8345 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8346 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8347 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8348 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8349 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8350 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8351 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8352
8353 // Last finger up.
8354 processMTSync(mapper);
8355 processSync(mapper);
8356
8357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8358 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8359 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8360 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8361 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8362 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8363 ASSERT_EQ(0, motionArgs.flags);
8364 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8365 ASSERT_EQ(0, motionArgs.buttonState);
8366 ASSERT_EQ(0, motionArgs.edgeFlags);
8367 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8368 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8369 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8371 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8372 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8373 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8374 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8375
8376 // Should not have sent any more keys or motions.
8377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8379}
8380
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008381TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8382 addConfigurationProperty("touch.deviceType", "touchScreen");
8383 prepareDisplay(DISPLAY_ORIENTATION_0);
8384
8385 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8386 /*fuzz*/ 0, /*resolution*/ 10);
8387 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8388 /*fuzz*/ 0, /*resolution*/ 11);
8389 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8390 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8391 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8392 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8393 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8394 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8395 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8396 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8397
8398 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8399
8400 // X and Y axes
8401 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8402 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8403 // Touch major and minor
8404 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8405 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8406 // Tool major and minor
8407 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8408 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8409}
8410
8411TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8412 addConfigurationProperty("touch.deviceType", "touchScreen");
8413 prepareDisplay(DISPLAY_ORIENTATION_0);
8414
8415 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8416 /*fuzz*/ 0, /*resolution*/ 10);
8417 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8418 /*fuzz*/ 0, /*resolution*/ 11);
8419
8420 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8421
8422 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8423
8424 // Touch major and minor
8425 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8426 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8427 // Tool major and minor
8428 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8429 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8430}
8431
Michael Wrightd02c5b62014-02-10 15:10:22 -08008432TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008433 addConfigurationProperty("touch.deviceType", "touchScreen");
8434 prepareDisplay(DISPLAY_ORIENTATION_0);
8435 prepareAxes(POSITION | ID);
8436 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008437 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008438
arthurhungdcef2dc2020-08-11 14:47:50 +08008439 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008440
8441 NotifyMotionArgs motionArgs;
8442
8443 // Two fingers down at once.
8444 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8445 processPosition(mapper, x1, y1);
8446 processId(mapper, 1);
8447 processMTSync(mapper);
8448 processPosition(mapper, x2, y2);
8449 processId(mapper, 2);
8450 processMTSync(mapper);
8451 processSync(mapper);
8452
8453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8454 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8455 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8456 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8457 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8458 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8459 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8460
8461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008462 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008463 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8464 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8465 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8466 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8467 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8469 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8471 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8472
8473 // Move.
8474 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8475 processPosition(mapper, x1, y1);
8476 processId(mapper, 1);
8477 processMTSync(mapper);
8478 processPosition(mapper, x2, y2);
8479 processId(mapper, 2);
8480 processMTSync(mapper);
8481 processSync(mapper);
8482
8483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8485 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8486 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8487 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8488 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8489 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8491 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8493 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8494
8495 // First finger up.
8496 x2 += 15; y2 -= 20;
8497 processPosition(mapper, x2, y2);
8498 processId(mapper, 2);
8499 processMTSync(mapper);
8500 processSync(mapper);
8501
8502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008503 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008504 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8505 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8506 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8507 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8508 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8510 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8512 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8513
8514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8515 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8516 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8517 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8518 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8520 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8521
8522 // Move.
8523 x2 += 20; y2 -= 25;
8524 processPosition(mapper, x2, y2);
8525 processId(mapper, 2);
8526 processMTSync(mapper);
8527 processSync(mapper);
8528
8529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8530 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8531 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8532 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8533 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8535 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8536
8537 // New finger down.
8538 int32_t x3 = 700, y3 = 300;
8539 processPosition(mapper, x2, y2);
8540 processId(mapper, 2);
8541 processMTSync(mapper);
8542 processPosition(mapper, x3, y3);
8543 processId(mapper, 3);
8544 processMTSync(mapper);
8545 processSync(mapper);
8546
8547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008548 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008549 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8550 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8551 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8552 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8555 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8557 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8558
8559 // Second finger up.
8560 x3 += 30; y3 -= 20;
8561 processPosition(mapper, x3, y3);
8562 processId(mapper, 3);
8563 processMTSync(mapper);
8564 processSync(mapper);
8565
8566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008567 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008568 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8569 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8571 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8574 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8576 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8577
8578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8580 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8581 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8584 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8585
8586 // Last finger up.
8587 processMTSync(mapper);
8588 processSync(mapper);
8589
8590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8591 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8592 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8593 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8594 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8596 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8597
8598 // Should not have sent any more keys or motions.
8599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8601}
8602
8603TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008604 addConfigurationProperty("touch.deviceType", "touchScreen");
8605 prepareDisplay(DISPLAY_ORIENTATION_0);
8606 prepareAxes(POSITION | ID | SLOT);
8607 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008608 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008609
arthurhungdcef2dc2020-08-11 14:47:50 +08008610 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008611
8612 NotifyMotionArgs motionArgs;
8613
8614 // Two fingers down at once.
8615 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8616 processPosition(mapper, x1, y1);
8617 processId(mapper, 1);
8618 processSlot(mapper, 1);
8619 processPosition(mapper, x2, y2);
8620 processId(mapper, 2);
8621 processSync(mapper);
8622
8623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8624 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8625 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8626 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8627 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8629 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8630
8631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008632 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008633 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8634 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8635 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8636 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8637 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8638 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8639 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8641 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8642
8643 // Move.
8644 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8645 processSlot(mapper, 0);
8646 processPosition(mapper, x1, y1);
8647 processSlot(mapper, 1);
8648 processPosition(mapper, x2, y2);
8649 processSync(mapper);
8650
8651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8652 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8653 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8654 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8655 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8656 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8657 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8659 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8661 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8662
8663 // First finger up.
8664 x2 += 15; y2 -= 20;
8665 processSlot(mapper, 0);
8666 processId(mapper, -1);
8667 processSlot(mapper, 1);
8668 processPosition(mapper, x2, y2);
8669 processSync(mapper);
8670
8671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008672 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008673 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8674 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8676 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8677 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8679 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8680 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8681 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8682
8683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8684 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8685 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8686 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8687 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8688 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8689 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8690
8691 // Move.
8692 x2 += 20; y2 -= 25;
8693 processPosition(mapper, x2, y2);
8694 processSync(mapper);
8695
8696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8697 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8698 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8699 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8700 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8702 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8703
8704 // New finger down.
8705 int32_t x3 = 700, y3 = 300;
8706 processPosition(mapper, x2, y2);
8707 processSlot(mapper, 0);
8708 processId(mapper, 3);
8709 processPosition(mapper, x3, y3);
8710 processSync(mapper);
8711
8712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008713 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008714 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8715 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8716 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8717 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8718 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8719 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8720 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8722 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8723
8724 // Second finger up.
8725 x3 += 30; y3 -= 20;
8726 processSlot(mapper, 1);
8727 processId(mapper, -1);
8728 processSlot(mapper, 0);
8729 processPosition(mapper, x3, y3);
8730 processSync(mapper);
8731
8732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008733 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008734 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8735 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8736 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8737 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8738 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8740 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8742 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8743
8744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8745 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8746 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8747 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8748 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8750 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8751
8752 // Last finger up.
8753 processId(mapper, -1);
8754 processSync(mapper);
8755
8756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8757 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8758 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8759 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8760 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8762 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8763
8764 // Should not have sent any more keys or motions.
8765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8767}
8768
8769TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008770 addConfigurationProperty("touch.deviceType", "touchScreen");
8771 prepareDisplay(DISPLAY_ORIENTATION_0);
8772 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008773 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008774
8775 // These calculations are based on the input device calibration documentation.
8776 int32_t rawX = 100;
8777 int32_t rawY = 200;
8778 int32_t rawTouchMajor = 7;
8779 int32_t rawTouchMinor = 6;
8780 int32_t rawToolMajor = 9;
8781 int32_t rawToolMinor = 8;
8782 int32_t rawPressure = 11;
8783 int32_t rawDistance = 0;
8784 int32_t rawOrientation = 3;
8785 int32_t id = 5;
8786
8787 float x = toDisplayX(rawX);
8788 float y = toDisplayY(rawY);
8789 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8790 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8791 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8792 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8793 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8794 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8795 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8796 float distance = float(rawDistance);
8797
8798 processPosition(mapper, rawX, rawY);
8799 processTouchMajor(mapper, rawTouchMajor);
8800 processTouchMinor(mapper, rawTouchMinor);
8801 processToolMajor(mapper, rawToolMajor);
8802 processToolMinor(mapper, rawToolMinor);
8803 processPressure(mapper, rawPressure);
8804 processOrientation(mapper, rawOrientation);
8805 processDistance(mapper, rawDistance);
8806 processId(mapper, id);
8807 processMTSync(mapper);
8808 processSync(mapper);
8809
8810 NotifyMotionArgs args;
8811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8812 ASSERT_EQ(0, args.pointerProperties[0].id);
8813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8814 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8815 orientation, distance));
8816}
8817
8818TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008819 addConfigurationProperty("touch.deviceType", "touchScreen");
8820 prepareDisplay(DISPLAY_ORIENTATION_0);
8821 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8822 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008823 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008824
8825 // These calculations are based on the input device calibration documentation.
8826 int32_t rawX = 100;
8827 int32_t rawY = 200;
8828 int32_t rawTouchMajor = 140;
8829 int32_t rawTouchMinor = 120;
8830 int32_t rawToolMajor = 180;
8831 int32_t rawToolMinor = 160;
8832
8833 float x = toDisplayX(rawX);
8834 float y = toDisplayY(rawY);
8835 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8836 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8837 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8838 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8839 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8840
8841 processPosition(mapper, rawX, rawY);
8842 processTouchMajor(mapper, rawTouchMajor);
8843 processTouchMinor(mapper, rawTouchMinor);
8844 processToolMajor(mapper, rawToolMajor);
8845 processToolMinor(mapper, rawToolMinor);
8846 processMTSync(mapper);
8847 processSync(mapper);
8848
8849 NotifyMotionArgs args;
8850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8852 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8853}
8854
8855TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008856 addConfigurationProperty("touch.deviceType", "touchScreen");
8857 prepareDisplay(DISPLAY_ORIENTATION_0);
8858 prepareAxes(POSITION | TOUCH | TOOL);
8859 addConfigurationProperty("touch.size.calibration", "diameter");
8860 addConfigurationProperty("touch.size.scale", "10");
8861 addConfigurationProperty("touch.size.bias", "160");
8862 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008863 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008864
8865 // These calculations are based on the input device calibration documentation.
8866 // Note: We only provide a single common touch/tool value because the device is assumed
8867 // not to emit separate values for each pointer (isSummed = 1).
8868 int32_t rawX = 100;
8869 int32_t rawY = 200;
8870 int32_t rawX2 = 150;
8871 int32_t rawY2 = 250;
8872 int32_t rawTouchMajor = 5;
8873 int32_t rawToolMajor = 8;
8874
8875 float x = toDisplayX(rawX);
8876 float y = toDisplayY(rawY);
8877 float x2 = toDisplayX(rawX2);
8878 float y2 = toDisplayY(rawY2);
8879 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8880 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8881 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8882
8883 processPosition(mapper, rawX, rawY);
8884 processTouchMajor(mapper, rawTouchMajor);
8885 processToolMajor(mapper, rawToolMajor);
8886 processMTSync(mapper);
8887 processPosition(mapper, rawX2, rawY2);
8888 processTouchMajor(mapper, rawTouchMajor);
8889 processToolMajor(mapper, rawToolMajor);
8890 processMTSync(mapper);
8891 processSync(mapper);
8892
8893 NotifyMotionArgs args;
8894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8895 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8896
8897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008898 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008899 ASSERT_EQ(size_t(2), args.pointerCount);
8900 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8901 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8902 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8903 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8904}
8905
8906TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008907 addConfigurationProperty("touch.deviceType", "touchScreen");
8908 prepareDisplay(DISPLAY_ORIENTATION_0);
8909 prepareAxes(POSITION | TOUCH | TOOL);
8910 addConfigurationProperty("touch.size.calibration", "area");
8911 addConfigurationProperty("touch.size.scale", "43");
8912 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008913 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008914
8915 // These calculations are based on the input device calibration documentation.
8916 int32_t rawX = 100;
8917 int32_t rawY = 200;
8918 int32_t rawTouchMajor = 5;
8919 int32_t rawToolMajor = 8;
8920
8921 float x = toDisplayX(rawX);
8922 float y = toDisplayY(rawY);
8923 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8924 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8925 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8926
8927 processPosition(mapper, rawX, rawY);
8928 processTouchMajor(mapper, rawTouchMajor);
8929 processToolMajor(mapper, rawToolMajor);
8930 processMTSync(mapper);
8931 processSync(mapper);
8932
8933 NotifyMotionArgs args;
8934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8935 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8936 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8937}
8938
8939TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008940 addConfigurationProperty("touch.deviceType", "touchScreen");
8941 prepareDisplay(DISPLAY_ORIENTATION_0);
8942 prepareAxes(POSITION | PRESSURE);
8943 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8944 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008945 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008946
Michael Wrightaa449c92017-12-13 21:21:43 +00008947 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008948 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008949 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8950 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8951 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8952
Michael Wrightd02c5b62014-02-10 15:10:22 -08008953 // These calculations are based on the input device calibration documentation.
8954 int32_t rawX = 100;
8955 int32_t rawY = 200;
8956 int32_t rawPressure = 60;
8957
8958 float x = toDisplayX(rawX);
8959 float y = toDisplayY(rawY);
8960 float pressure = float(rawPressure) * 0.01f;
8961
8962 processPosition(mapper, rawX, rawY);
8963 processPressure(mapper, rawPressure);
8964 processMTSync(mapper);
8965 processSync(mapper);
8966
8967 NotifyMotionArgs args;
8968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8970 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8971}
8972
8973TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008974 addConfigurationProperty("touch.deviceType", "touchScreen");
8975 prepareDisplay(DISPLAY_ORIENTATION_0);
8976 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008977 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008978
8979 NotifyMotionArgs motionArgs;
8980 NotifyKeyArgs keyArgs;
8981
8982 processId(mapper, 1);
8983 processPosition(mapper, 100, 200);
8984 processSync(mapper);
8985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8986 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8987 ASSERT_EQ(0, motionArgs.buttonState);
8988
8989 // press BTN_LEFT, release BTN_LEFT
8990 processKey(mapper, BTN_LEFT, 1);
8991 processSync(mapper);
8992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8993 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8994 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8995
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8997 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8998 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8999
Michael Wrightd02c5b62014-02-10 15:10:22 -08009000 processKey(mapper, BTN_LEFT, 0);
9001 processSync(mapper);
9002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009003 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009004 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009005
9006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009007 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009008 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009009
9010 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
9011 processKey(mapper, BTN_RIGHT, 1);
9012 processKey(mapper, BTN_MIDDLE, 1);
9013 processSync(mapper);
9014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9015 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9016 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
9017 motionArgs.buttonState);
9018
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9020 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9021 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
9022
9023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9024 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9025 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
9026 motionArgs.buttonState);
9027
Michael Wrightd02c5b62014-02-10 15:10:22 -08009028 processKey(mapper, BTN_RIGHT, 0);
9029 processSync(mapper);
9030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009031 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009032 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009033
9034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009035 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009036 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009037
9038 processKey(mapper, BTN_MIDDLE, 0);
9039 processSync(mapper);
9040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009041 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009042 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009043
9044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009046 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009047
9048 // press BTN_BACK, release BTN_BACK
9049 processKey(mapper, BTN_BACK, 1);
9050 processSync(mapper);
9051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9052 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9053 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009054
Michael Wrightd02c5b62014-02-10 15:10:22 -08009055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009056 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009057 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
9058
9059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9060 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9061 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009062
9063 processKey(mapper, BTN_BACK, 0);
9064 processSync(mapper);
9065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009066 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009067 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009068
9069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009070 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009071 ASSERT_EQ(0, motionArgs.buttonState);
9072
Michael Wrightd02c5b62014-02-10 15:10:22 -08009073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9074 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9075 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
9076
9077 // press BTN_SIDE, release BTN_SIDE
9078 processKey(mapper, BTN_SIDE, 1);
9079 processSync(mapper);
9080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9081 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9082 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009083
Michael Wrightd02c5b62014-02-10 15:10:22 -08009084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009085 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009086 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
9087
9088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9089 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9090 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009091
9092 processKey(mapper, BTN_SIDE, 0);
9093 processSync(mapper);
9094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009095 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009096 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009097
9098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009099 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009100 ASSERT_EQ(0, motionArgs.buttonState);
9101
Michael Wrightd02c5b62014-02-10 15:10:22 -08009102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9103 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9104 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
9105
9106 // press BTN_FORWARD, release BTN_FORWARD
9107 processKey(mapper, BTN_FORWARD, 1);
9108 processSync(mapper);
9109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9110 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9111 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009112
Michael Wrightd02c5b62014-02-10 15:10:22 -08009113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009114 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009115 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9116
9117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9118 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9119 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009120
9121 processKey(mapper, BTN_FORWARD, 0);
9122 processSync(mapper);
9123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009124 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009125 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009126
9127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009129 ASSERT_EQ(0, motionArgs.buttonState);
9130
Michael Wrightd02c5b62014-02-10 15:10:22 -08009131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9132 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9133 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9134
9135 // press BTN_EXTRA, release BTN_EXTRA
9136 processKey(mapper, BTN_EXTRA, 1);
9137 processSync(mapper);
9138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9139 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9140 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009141
Michael Wrightd02c5b62014-02-10 15:10:22 -08009142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009143 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009144 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9145
9146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9147 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9148 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009149
9150 processKey(mapper, BTN_EXTRA, 0);
9151 processSync(mapper);
9152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009153 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009154 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009155
9156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009157 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009158 ASSERT_EQ(0, motionArgs.buttonState);
9159
Michael Wrightd02c5b62014-02-10 15:10:22 -08009160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9161 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9162 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9163
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9165
Michael Wrightd02c5b62014-02-10 15:10:22 -08009166 // press BTN_STYLUS, release BTN_STYLUS
9167 processKey(mapper, BTN_STYLUS, 1);
9168 processSync(mapper);
9169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9170 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009171 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
9172
9173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9174 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9175 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009176
9177 processKey(mapper, BTN_STYLUS, 0);
9178 processSync(mapper);
9179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009180 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009181 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009182
9183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009184 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009185 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009186
9187 // press BTN_STYLUS2, release BTN_STYLUS2
9188 processKey(mapper, BTN_STYLUS2, 1);
9189 processSync(mapper);
9190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9191 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009192 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
9193
9194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9195 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9196 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009197
9198 processKey(mapper, BTN_STYLUS2, 0);
9199 processSync(mapper);
9200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009201 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009202 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009203
9204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009205 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009206 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009207
9208 // release touch
9209 processId(mapper, -1);
9210 processSync(mapper);
9211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9212 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9213 ASSERT_EQ(0, motionArgs.buttonState);
9214}
9215
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009216TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
9217 addConfigurationProperty("touch.deviceType", "touchScreen");
9218 prepareDisplay(DISPLAY_ORIENTATION_0);
9219 prepareAxes(POSITION | ID | SLOT);
9220 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9221
9222 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
9223 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
9224
9225 // Touch down.
9226 processId(mapper, 1);
9227 processPosition(mapper, 100, 200);
9228 processSync(mapper);
9229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9230 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
9231
9232 // Press and release button mapped to the primary stylus button.
9233 processKey(mapper, BTN_A, 1);
9234 processSync(mapper);
9235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9236 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9237 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9239 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9240 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9241
9242 processKey(mapper, BTN_A, 0);
9243 processSync(mapper);
9244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9245 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9247 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9248
9249 // Press and release the HID usage mapped to the secondary stylus button.
9250 processHidUsage(mapper, 0xabcd, 1);
9251 processSync(mapper);
9252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9253 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9254 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9256 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9257 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9258
9259 processHidUsage(mapper, 0xabcd, 0);
9260 processSync(mapper);
9261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9262 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9264 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9265
9266 // Release touch.
9267 processId(mapper, -1);
9268 processSync(mapper);
9269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9270 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
9271}
9272
Michael Wrightd02c5b62014-02-10 15:10:22 -08009273TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009274 addConfigurationProperty("touch.deviceType", "touchScreen");
9275 prepareDisplay(DISPLAY_ORIENTATION_0);
9276 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009277 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009278
9279 NotifyMotionArgs motionArgs;
9280
9281 // default tool type is finger
9282 processId(mapper, 1);
9283 processPosition(mapper, 100, 200);
9284 processSync(mapper);
9285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9286 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9287 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9288
9289 // eraser
9290 processKey(mapper, BTN_TOOL_RUBBER, 1);
9291 processSync(mapper);
9292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9294 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
9295
9296 // stylus
9297 processKey(mapper, BTN_TOOL_RUBBER, 0);
9298 processKey(mapper, BTN_TOOL_PEN, 1);
9299 processSync(mapper);
9300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9301 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9302 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9303
9304 // brush
9305 processKey(mapper, BTN_TOOL_PEN, 0);
9306 processKey(mapper, BTN_TOOL_BRUSH, 1);
9307 processSync(mapper);
9308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9310 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9311
9312 // pencil
9313 processKey(mapper, BTN_TOOL_BRUSH, 0);
9314 processKey(mapper, BTN_TOOL_PENCIL, 1);
9315 processSync(mapper);
9316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9318 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9319
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009320 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009321 processKey(mapper, BTN_TOOL_PENCIL, 0);
9322 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
9323 processSync(mapper);
9324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9326 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9327
9328 // mouse
9329 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9330 processKey(mapper, BTN_TOOL_MOUSE, 1);
9331 processSync(mapper);
9332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9334 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
9335
9336 // lens
9337 processKey(mapper, BTN_TOOL_MOUSE, 0);
9338 processKey(mapper, BTN_TOOL_LENS, 1);
9339 processSync(mapper);
9340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9342 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
9343
9344 // double-tap
9345 processKey(mapper, BTN_TOOL_LENS, 0);
9346 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
9347 processSync(mapper);
9348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9350 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9351
9352 // triple-tap
9353 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9354 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
9355 processSync(mapper);
9356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9359
9360 // quad-tap
9361 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9362 processKey(mapper, BTN_TOOL_QUADTAP, 1);
9363 processSync(mapper);
9364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9365 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9366 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9367
9368 // finger
9369 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9370 processKey(mapper, BTN_TOOL_FINGER, 1);
9371 processSync(mapper);
9372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9373 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9374 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9375
9376 // stylus trumps finger
9377 processKey(mapper, BTN_TOOL_PEN, 1);
9378 processSync(mapper);
9379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9380 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9381 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9382
9383 // eraser trumps stylus
9384 processKey(mapper, BTN_TOOL_RUBBER, 1);
9385 processSync(mapper);
9386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9387 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9388 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
9389
9390 // mouse trumps eraser
9391 processKey(mapper, BTN_TOOL_MOUSE, 1);
9392 processSync(mapper);
9393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9394 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9395 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
9396
9397 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9398 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9399 processSync(mapper);
9400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9402 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9403
9404 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9405 processToolType(mapper, MT_TOOL_PEN);
9406 processSync(mapper);
9407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9408 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9409 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9410
9411 // back to default tool type
9412 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9413 processKey(mapper, BTN_TOOL_MOUSE, 0);
9414 processKey(mapper, BTN_TOOL_RUBBER, 0);
9415 processKey(mapper, BTN_TOOL_PEN, 0);
9416 processKey(mapper, BTN_TOOL_FINGER, 0);
9417 processSync(mapper);
9418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9420 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9421}
9422
9423TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009424 addConfigurationProperty("touch.deviceType", "touchScreen");
9425 prepareDisplay(DISPLAY_ORIENTATION_0);
9426 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009427 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009428 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009429
9430 NotifyMotionArgs motionArgs;
9431
9432 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9433 processId(mapper, 1);
9434 processPosition(mapper, 100, 200);
9435 processSync(mapper);
9436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9437 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9439 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9440
9441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9442 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9444 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9445
9446 // move a little
9447 processPosition(mapper, 150, 250);
9448 processSync(mapper);
9449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9450 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9452 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9453
9454 // down when BTN_TOUCH is pressed, pressure defaults to 1
9455 processKey(mapper, BTN_TOUCH, 1);
9456 processSync(mapper);
9457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9458 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9459 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9460 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9461
9462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9463 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9465 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9466
9467 // up when BTN_TOUCH is released, hover restored
9468 processKey(mapper, BTN_TOUCH, 0);
9469 processSync(mapper);
9470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9471 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9473 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9474
9475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9476 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9477 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9478 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9479
9480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9481 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9482 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9483 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9484
9485 // exit hover when pointer goes away
9486 processId(mapper, -1);
9487 processSync(mapper);
9488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9489 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9491 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9492}
9493
9494TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009495 addConfigurationProperty("touch.deviceType", "touchScreen");
9496 prepareDisplay(DISPLAY_ORIENTATION_0);
9497 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009498 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009499
9500 NotifyMotionArgs motionArgs;
9501
9502 // initially hovering because pressure is 0
9503 processId(mapper, 1);
9504 processPosition(mapper, 100, 200);
9505 processPressure(mapper, 0);
9506 processSync(mapper);
9507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9508 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9510 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9511
9512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9513 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9515 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9516
9517 // move a little
9518 processPosition(mapper, 150, 250);
9519 processSync(mapper);
9520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9521 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9523 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9524
9525 // down when pressure becomes non-zero
9526 processPressure(mapper, RAW_PRESSURE_MAX);
9527 processSync(mapper);
9528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9529 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9531 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9532
9533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9534 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9536 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9537
9538 // up when pressure becomes 0, hover restored
9539 processPressure(mapper, 0);
9540 processSync(mapper);
9541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9542 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9544 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9545
9546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9547 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9549 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9550
9551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9552 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9554 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9555
9556 // exit hover when pointer goes away
9557 processId(mapper, -1);
9558 processSync(mapper);
9559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9560 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9562 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9563}
9564
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009565/**
9566 * Set the input device port <--> display port associations, and check that the
9567 * events are routed to the display that matches the display port.
9568 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9569 */
9570TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009571 const std::string usb2 = "USB2";
9572 const uint8_t hdmi1 = 0;
9573 const uint8_t hdmi2 = 1;
9574 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009575 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009576
9577 addConfigurationProperty("touch.deviceType", "touchScreen");
9578 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009579 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009580
9581 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9582 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9583
9584 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9585 // for this input device is specified, and the matching viewport is not present,
9586 // the input device should be disabled (at the mapper level).
9587
9588 // Add viewport for display 2 on hdmi2
9589 prepareSecondaryDisplay(type, hdmi2);
9590 // Send a touch event
9591 processPosition(mapper, 100, 100);
9592 processSync(mapper);
9593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9594
9595 // Add viewport for display 1 on hdmi1
9596 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
9597 // Send a touch event again
9598 processPosition(mapper, 100, 100);
9599 processSync(mapper);
9600
9601 NotifyMotionArgs args;
9602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9603 ASSERT_EQ(DISPLAY_ID, args.displayId);
9604}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009605
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009606TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9607 addConfigurationProperty("touch.deviceType", "touchScreen");
9608 prepareAxes(POSITION);
9609 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9610
9611 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9612
9613 prepareDisplay(DISPLAY_ORIENTATION_0);
9614 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9615
9616 // Send a touch event
9617 processPosition(mapper, 100, 100);
9618 processSync(mapper);
9619
9620 NotifyMotionArgs args;
9621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9622 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9623}
9624
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009625TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009626 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009627 std::shared_ptr<FakePointerController> fakePointerController =
9628 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009629 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009630 fakePointerController->setPosition(100, 200);
9631 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009632 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009633
Garfield Tan888a6a42020-01-09 11:39:16 -08009634 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009635 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009636
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009637 prepareDisplay(DISPLAY_ORIENTATION_0);
9638 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009639 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009640
Harry Cutts16a24cc2022-10-26 15:22:19 +00009641 // Check source is a touchpad that would obtain the PointerController.
9642 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009643
9644 NotifyMotionArgs motionArgs;
9645 processPosition(mapper, 100, 100);
9646 processSync(mapper);
9647
9648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9649 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9650 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9651}
9652
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009653/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009654 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9655 */
9656TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9657 addConfigurationProperty("touch.deviceType", "touchScreen");
9658 prepareAxes(POSITION);
9659 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9660
9661 prepareDisplay(DISPLAY_ORIENTATION_0);
9662 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
9663 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
9664 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
9665 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9666
9667 NotifyMotionArgs args;
9668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9669 ASSERT_EQ(26, args.readTime);
9670
9671 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
9672 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
9673 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9674
9675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9676 ASSERT_EQ(33, args.readTime);
9677}
9678
9679/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009680 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9681 * events should not be delivered to the listener.
9682 */
9683TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9684 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009685 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009686 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9687 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
9688 ViewportType::INTERNAL);
9689 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9690 prepareAxes(POSITION);
9691 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9692
9693 NotifyMotionArgs motionArgs;
9694 processPosition(mapper, 100, 100);
9695 processSync(mapper);
9696
9697 mFakeListener->assertNotifyMotionWasNotCalled();
9698}
9699
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009700/**
9701 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9702 * the touch mapper can process the events and the events can be delivered to the listener.
9703 */
9704TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9705 addConfigurationProperty("touch.deviceType", "touchScreen");
9706 addConfigurationProperty("touch.enableForInactiveViewport", "1");
9707 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9708 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
9709 ViewportType::INTERNAL);
9710 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9711 prepareAxes(POSITION);
9712 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9713
9714 NotifyMotionArgs motionArgs;
9715 processPosition(mapper, 100, 100);
9716 processSync(mapper);
9717
9718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9719 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9720}
9721
Garfield Tanc734e4f2021-01-15 20:01:39 -08009722TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9723 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009724 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Garfield Tanc734e4f2021-01-15 20:01:39 -08009725 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9726 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
9727 ViewportType::INTERNAL);
9728 std::optional<DisplayViewport> optionalDisplayViewport =
9729 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9730 ASSERT_TRUE(optionalDisplayViewport.has_value());
9731 DisplayViewport displayViewport = *optionalDisplayViewport;
9732
9733 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9734 prepareAxes(POSITION);
9735 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9736
9737 // Finger down
9738 int32_t x = 100, y = 100;
9739 processPosition(mapper, x, y);
9740 processSync(mapper);
9741
9742 NotifyMotionArgs motionArgs;
9743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9744 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9745
9746 // Deactivate display viewport
9747 displayViewport.isActive = false;
9748 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9749 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9750
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009751 // The ongoing touch should be canceled immediately
9752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9753 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9754
9755 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009756 x += 10, y += 10;
9757 processPosition(mapper, x, y);
9758 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009760
9761 // Reactivate display viewport
9762 displayViewport.isActive = true;
9763 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9764 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9765
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009766 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009767 x += 10, y += 10;
9768 processPosition(mapper, x, y);
9769 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9771 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009772}
9773
Arthur Hung7c645402019-01-25 17:45:42 +08009774TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9775 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009776 prepareAxes(POSITION | ID | SLOT);
9777 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009778 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009779
9780 // Create the second touch screen device, and enable multi fingers.
9781 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009782 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009783 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009784 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009785 std::shared_ptr<InputDevice> device2 =
9786 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009787 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009788
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009789 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9790 0 /*flat*/, 0 /*fuzz*/);
9791 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9792 0 /*flat*/, 0 /*fuzz*/);
9793 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
9794 0 /*flat*/, 0 /*fuzz*/);
9795 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
9796 0 /*flat*/, 0 /*fuzz*/);
9797 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
9798 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9799 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009800
9801 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009802 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009803 std::list<NotifyArgs> unused =
9804 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9805 0 /*changes*/);
9806 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009807
9808 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009809 std::shared_ptr<FakePointerController> fakePointerController =
9810 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009811 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009812
9813 // Setup policy for associated displays and show touches.
9814 const uint8_t hdmi1 = 0;
9815 const uint8_t hdmi2 = 1;
9816 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9817 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9818 mFakePolicy->setShowTouches(true);
9819
9820 // Create displays.
9821 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009822 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009823
9824 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009825 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9826 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9827 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009828
9829 // Two fingers down at default display.
9830 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9831 processPosition(mapper, x1, y1);
9832 processId(mapper, 1);
9833 processSlot(mapper, 1);
9834 processPosition(mapper, x2, y2);
9835 processId(mapper, 2);
9836 processSync(mapper);
9837
9838 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9839 fakePointerController->getSpots().find(DISPLAY_ID);
9840 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9841 ASSERT_EQ(size_t(2), iter->second.size());
9842
9843 // Two fingers down at second display.
9844 processPosition(mapper2, x1, y1);
9845 processId(mapper2, 1);
9846 processSlot(mapper2, 1);
9847 processPosition(mapper2, x2, y2);
9848 processId(mapper2, 2);
9849 processSync(mapper2);
9850
9851 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9852 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9853 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009854
9855 // Disable the show touches configuration and ensure the spots are cleared.
9856 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009857 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9858 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009859
9860 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009861}
9862
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009863TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009864 prepareAxes(POSITION);
9865 addConfigurationProperty("touch.deviceType", "touchScreen");
9866 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009867 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009868
9869 NotifyMotionArgs motionArgs;
9870 // Unrotated video frame
9871 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9872 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009873 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009874 processPosition(mapper, 100, 200);
9875 processSync(mapper);
9876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9877 ASSERT_EQ(frames, motionArgs.videoFrames);
9878
9879 // Subsequent touch events should not have any videoframes
9880 // This is implemented separately in FakeEventHub,
9881 // but that should match the behaviour of TouchVideoDevice.
9882 processPosition(mapper, 200, 200);
9883 processSync(mapper);
9884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9885 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9886}
9887
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009888TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009889 prepareAxes(POSITION);
9890 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009891 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009892 // Unrotated video frame
9893 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9894 NotifyMotionArgs motionArgs;
9895
9896 // Test all 4 orientations
9897 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009898 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9899 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9900 clearViewports();
9901 prepareDisplay(orientation);
9902 std::vector<TouchVideoFrame> frames{frame};
9903 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9904 processPosition(mapper, 100, 200);
9905 processSync(mapper);
9906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9907 ASSERT_EQ(frames, motionArgs.videoFrames);
9908 }
9909}
9910
9911TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9912 prepareAxes(POSITION);
9913 addConfigurationProperty("touch.deviceType", "touchScreen");
9914 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9915 // orientation-aware are affected by display rotation.
9916 addConfigurationProperty("touch.orientationAware", "0");
9917 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9918 // Unrotated video frame
9919 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9920 NotifyMotionArgs motionArgs;
9921
9922 // Test all 4 orientations
9923 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009924 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9925 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9926 clearViewports();
9927 prepareDisplay(orientation);
9928 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009929 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009930 processPosition(mapper, 100, 200);
9931 processSync(mapper);
9932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009933 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9934 // compared to the display. This is so that when the window transform (which contains the
9935 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9936 // window's coordinate space.
9937 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009938 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009939
9940 // Release finger.
9941 processSync(mapper);
9942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009943 }
9944}
9945
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009946TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009947 prepareAxes(POSITION);
9948 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009949 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009950 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9951 // so mix these.
9952 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9953 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9954 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9955 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9956 NotifyMotionArgs motionArgs;
9957
9958 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009959 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009960 processPosition(mapper, 100, 200);
9961 processSync(mapper);
9962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009963 ASSERT_EQ(frames, motionArgs.videoFrames);
9964}
9965
9966TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9967 prepareAxes(POSITION);
9968 addConfigurationProperty("touch.deviceType", "touchScreen");
9969 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9970 // orientation-aware are affected by display rotation.
9971 addConfigurationProperty("touch.orientationAware", "0");
9972 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9973 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9974 // so mix these.
9975 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9976 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9977 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9978 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9979 NotifyMotionArgs motionArgs;
9980
9981 prepareDisplay(DISPLAY_ORIENTATION_90);
9982 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9983 processPosition(mapper, 100, 200);
9984 processSync(mapper);
9985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9986 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9987 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9988 // compared to the display. This is so that when the window transform (which contains the
9989 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9990 // window's coordinate space.
9991 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
9992 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009993 ASSERT_EQ(frames, motionArgs.videoFrames);
9994}
9995
Arthur Hung9da14732019-09-02 16:16:58 +08009996/**
9997 * If we had defined port associations, but the viewport is not ready, the touch device would be
9998 * expected to be disabled, and it should be enabled after the viewport has found.
9999 */
10000TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +080010001 constexpr uint8_t hdmi2 = 1;
10002 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010003 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +080010004
10005 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
10006
10007 addConfigurationProperty("touch.deviceType", "touchScreen");
10008 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010009 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +080010010
10011 ASSERT_EQ(mDevice->isEnabled(), false);
10012
10013 // Add display on hdmi2, the device should be enabled and can receive touch event.
10014 prepareSecondaryDisplay(type, hdmi2);
10015 ASSERT_EQ(mDevice->isEnabled(), true);
10016
10017 // Send a touch event.
10018 processPosition(mapper, 100, 100);
10019 processSync(mapper);
10020
10021 NotifyMotionArgs args;
10022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10023 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
10024}
10025
Arthur Hung421eb1c2020-01-16 00:09:42 +080010026TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010027 addConfigurationProperty("touch.deviceType", "touchScreen");
10028 prepareDisplay(DISPLAY_ORIENTATION_0);
10029 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010030 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010031
10032 NotifyMotionArgs motionArgs;
10033
10034 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10035 // finger down
10036 processId(mapper, 1);
10037 processPosition(mapper, x1, y1);
10038 processSync(mapper);
10039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10040 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10042
10043 // finger move
10044 processId(mapper, 1);
10045 processPosition(mapper, x2, y2);
10046 processSync(mapper);
10047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10048 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10049 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10050
10051 // finger up.
10052 processId(mapper, -1);
10053 processSync(mapper);
10054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10055 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10056 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10057
10058 // new finger down
10059 processId(mapper, 1);
10060 processPosition(mapper, x3, y3);
10061 processSync(mapper);
10062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10063 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10064 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10065}
10066
10067/**
arthurhungcc7f9802020-04-30 17:55:40 +080010068 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
10069 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +080010070 */
arthurhungcc7f9802020-04-30 17:55:40 +080010071TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010072 addConfigurationProperty("touch.deviceType", "touchScreen");
10073 prepareDisplay(DISPLAY_ORIENTATION_0);
10074 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010075 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010076
10077 NotifyMotionArgs motionArgs;
10078
10079 // default tool type is finger
10080 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +080010081 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010082 processPosition(mapper, x1, y1);
10083 processSync(mapper);
10084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10085 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10086 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10087
10088 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
10089 processToolType(mapper, MT_TOOL_PALM);
10090 processSync(mapper);
10091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10092 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10093
10094 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +080010095 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010096 processPosition(mapper, x2, y2);
10097 processSync(mapper);
10098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10099
10100 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +080010101 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010102 processSync(mapper);
10103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10104
10105 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +080010106 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010107 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010108 processPosition(mapper, x3, y3);
10109 processSync(mapper);
10110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10111 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10112 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10113}
10114
arthurhungbf89a482020-04-17 17:37:55 +080010115/**
arthurhungcc7f9802020-04-30 17:55:40 +080010116 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10117 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +080010118 */
arthurhungcc7f9802020-04-30 17:55:40 +080010119TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +080010120 addConfigurationProperty("touch.deviceType", "touchScreen");
10121 prepareDisplay(DISPLAY_ORIENTATION_0);
10122 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
10123 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10124
10125 NotifyMotionArgs motionArgs;
10126
10127 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +080010128 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10129 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010130 processPosition(mapper, x1, y1);
10131 processSync(mapper);
10132 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);
10135
10136 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +080010137 processSlot(mapper, SECOND_SLOT);
10138 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010139 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +080010140 processSync(mapper);
10141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010142 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010143 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
10144
10145 // If the tool type of the first finger changes to MT_TOOL_PALM,
10146 // we expect to receive ACTION_POINTER_UP with cancel flag.
10147 processSlot(mapper, FIRST_SLOT);
10148 processId(mapper, FIRST_TRACKING_ID);
10149 processToolType(mapper, MT_TOOL_PALM);
10150 processSync(mapper);
10151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010152 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010153 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10154
10155 // The following MOVE events of second finger should be processed.
10156 processSlot(mapper, SECOND_SLOT);
10157 processId(mapper, SECOND_TRACKING_ID);
10158 processPosition(mapper, x2 + 1, y2 + 1);
10159 processSync(mapper);
10160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10161 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10162 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10163
10164 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
10165 // it. Second finger receive move.
10166 processSlot(mapper, FIRST_SLOT);
10167 processId(mapper, INVALID_TRACKING_ID);
10168 processSync(mapper);
10169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10170 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10171 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10172
10173 // Second finger keeps moving.
10174 processSlot(mapper, SECOND_SLOT);
10175 processId(mapper, SECOND_TRACKING_ID);
10176 processPosition(mapper, x2 + 2, y2 + 2);
10177 processSync(mapper);
10178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10180 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10181
10182 // Second finger up.
10183 processId(mapper, INVALID_TRACKING_ID);
10184 processSync(mapper);
10185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10186 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10187 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10188}
10189
10190/**
10191 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
10192 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
10193 */
10194TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
10195 addConfigurationProperty("touch.deviceType", "touchScreen");
10196 prepareDisplay(DISPLAY_ORIENTATION_0);
10197 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
10198 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10199
10200 NotifyMotionArgs motionArgs;
10201
10202 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10203 // First finger down.
10204 processId(mapper, FIRST_TRACKING_ID);
10205 processPosition(mapper, x1, y1);
10206 processSync(mapper);
10207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10208 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10209 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10210
10211 // Second finger down.
10212 processSlot(mapper, SECOND_SLOT);
10213 processId(mapper, SECOND_TRACKING_ID);
10214 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +080010215 processSync(mapper);
10216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010217 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +080010218 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10219
arthurhungcc7f9802020-04-30 17:55:40 +080010220 // If the tool type of the first finger changes to MT_TOOL_PALM,
10221 // we expect to receive ACTION_POINTER_UP with cancel flag.
10222 processSlot(mapper, FIRST_SLOT);
10223 processId(mapper, FIRST_TRACKING_ID);
10224 processToolType(mapper, MT_TOOL_PALM);
10225 processSync(mapper);
10226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010227 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010228 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10229
10230 // Second finger keeps moving.
10231 processSlot(mapper, SECOND_SLOT);
10232 processId(mapper, SECOND_TRACKING_ID);
10233 processPosition(mapper, x2 + 1, y2 + 1);
10234 processSync(mapper);
10235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10237
10238 // second finger becomes palm, receive cancel due to only 1 finger is active.
10239 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010240 processToolType(mapper, MT_TOOL_PALM);
10241 processSync(mapper);
10242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10243 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10244
arthurhungcc7f9802020-04-30 17:55:40 +080010245 // third finger down.
10246 processSlot(mapper, THIRD_SLOT);
10247 processId(mapper, THIRD_TRACKING_ID);
10248 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +080010249 processPosition(mapper, x3, y3);
10250 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +080010251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10252 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10253 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010254 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10255
10256 // third finger move
10257 processId(mapper, THIRD_TRACKING_ID);
10258 processPosition(mapper, x3 + 1, y3 + 1);
10259 processSync(mapper);
10260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10262
10263 // first finger up, third finger receive move.
10264 processSlot(mapper, FIRST_SLOT);
10265 processId(mapper, INVALID_TRACKING_ID);
10266 processSync(mapper);
10267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10269 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10270
10271 // second finger up, third finger receive move.
10272 processSlot(mapper, SECOND_SLOT);
10273 processId(mapper, INVALID_TRACKING_ID);
10274 processSync(mapper);
10275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10277 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10278
10279 // third finger up.
10280 processSlot(mapper, THIRD_SLOT);
10281 processId(mapper, INVALID_TRACKING_ID);
10282 processSync(mapper);
10283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10284 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10285 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10286}
10287
10288/**
10289 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10290 * and the active finger could still be allowed to receive the events
10291 */
10292TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
10293 addConfigurationProperty("touch.deviceType", "touchScreen");
10294 prepareDisplay(DISPLAY_ORIENTATION_0);
10295 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
10296 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10297
10298 NotifyMotionArgs motionArgs;
10299
10300 // default tool type is finger
10301 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10302 processId(mapper, FIRST_TRACKING_ID);
10303 processPosition(mapper, x1, y1);
10304 processSync(mapper);
10305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10306 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10307 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10308
10309 // Second finger down.
10310 processSlot(mapper, SECOND_SLOT);
10311 processId(mapper, SECOND_TRACKING_ID);
10312 processPosition(mapper, x2, y2);
10313 processSync(mapper);
10314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010315 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010316 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10317
10318 // If the tool type of the second finger changes to MT_TOOL_PALM,
10319 // we expect to receive ACTION_POINTER_UP with cancel flag.
10320 processId(mapper, SECOND_TRACKING_ID);
10321 processToolType(mapper, MT_TOOL_PALM);
10322 processSync(mapper);
10323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010324 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010325 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10326
10327 // The following MOVE event should be processed.
10328 processSlot(mapper, FIRST_SLOT);
10329 processId(mapper, FIRST_TRACKING_ID);
10330 processPosition(mapper, x1 + 1, y1 + 1);
10331 processSync(mapper);
10332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10334 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10335
10336 // second finger up.
10337 processSlot(mapper, SECOND_SLOT);
10338 processId(mapper, INVALID_TRACKING_ID);
10339 processSync(mapper);
10340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10342
10343 // first finger keep moving
10344 processSlot(mapper, FIRST_SLOT);
10345 processId(mapper, FIRST_TRACKING_ID);
10346 processPosition(mapper, x1 + 2, y1 + 2);
10347 processSync(mapper);
10348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10350
10351 // first finger up.
10352 processId(mapper, INVALID_TRACKING_ID);
10353 processSync(mapper);
10354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10355 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10356 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010357}
10358
Arthur Hung9ad18942021-06-19 02:04:46 +000010359/**
10360 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10361 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10362 * cause slot be valid again.
10363 */
10364TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10365 addConfigurationProperty("touch.deviceType", "touchScreen");
10366 prepareDisplay(DISPLAY_ORIENTATION_0);
10367 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10368 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10369
10370 NotifyMotionArgs motionArgs;
10371
10372 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10373 // First finger down.
10374 processId(mapper, FIRST_TRACKING_ID);
10375 processPosition(mapper, x1, y1);
10376 processPressure(mapper, RAW_PRESSURE_MAX);
10377 processSync(mapper);
10378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10379 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10380 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10381
10382 // First finger move.
10383 processId(mapper, FIRST_TRACKING_ID);
10384 processPosition(mapper, x1 + 1, y1 + 1);
10385 processPressure(mapper, RAW_PRESSURE_MAX);
10386 processSync(mapper);
10387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10388 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10389 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10390
10391 // Second finger down.
10392 processSlot(mapper, SECOND_SLOT);
10393 processId(mapper, SECOND_TRACKING_ID);
10394 processPosition(mapper, x2, y2);
10395 processPressure(mapper, RAW_PRESSURE_MAX);
10396 processSync(mapper);
10397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010398 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010399 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10400
10401 // second finger up with some unexpected data.
10402 processSlot(mapper, SECOND_SLOT);
10403 processId(mapper, INVALID_TRACKING_ID);
10404 processPosition(mapper, x2, y2);
10405 processSync(mapper);
10406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010407 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010408 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10409
10410 // first finger up with some unexpected data.
10411 processSlot(mapper, FIRST_SLOT);
10412 processId(mapper, INVALID_TRACKING_ID);
10413 processPosition(mapper, x2, y2);
10414 processPressure(mapper, RAW_PRESSURE_MAX);
10415 processSync(mapper);
10416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10417 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10418 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10419}
10420
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010421TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10422 addConfigurationProperty("touch.deviceType", "touchScreen");
10423 prepareDisplay(DISPLAY_ORIENTATION_0);
10424 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10425 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10426
10427 // First finger down.
10428 processId(mapper, FIRST_TRACKING_ID);
10429 processPosition(mapper, 100, 200);
10430 processPressure(mapper, RAW_PRESSURE_MAX);
10431 processSync(mapper);
10432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10433 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10434
10435 // Second finger down.
10436 processSlot(mapper, SECOND_SLOT);
10437 processId(mapper, SECOND_TRACKING_ID);
10438 processPosition(mapper, 300, 400);
10439 processPressure(mapper, RAW_PRESSURE_MAX);
10440 processSync(mapper);
10441 ASSERT_NO_FATAL_FAILURE(
10442 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10443
10444 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010445 // preserved. Resetting should cancel the ongoing gesture.
10446 resetMapper(mapper, ARBITRARY_TIME);
10447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10448 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010449
10450 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10451 // the existing touch state to generate a down event.
10452 processPosition(mapper, 301, 302);
10453 processSync(mapper);
10454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10455 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10457 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10458
10459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10460}
10461
10462TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10463 addConfigurationProperty("touch.deviceType", "touchScreen");
10464 prepareDisplay(DISPLAY_ORIENTATION_0);
10465 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10466 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10467
10468 // First finger touches down and releases.
10469 processId(mapper, FIRST_TRACKING_ID);
10470 processPosition(mapper, 100, 200);
10471 processPressure(mapper, RAW_PRESSURE_MAX);
10472 processSync(mapper);
10473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10474 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10475 processId(mapper, INVALID_TRACKING_ID);
10476 processSync(mapper);
10477 ASSERT_NO_FATAL_FAILURE(
10478 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10479
10480 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10481 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010482 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10484
10485 // Send an empty sync frame. Since there are no pointers, no events are generated.
10486 processSync(mapper);
10487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10488}
10489
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010490// --- MultiTouchInputMapperTest_ExternalDevice ---
10491
10492class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10493protected:
Chris Yea52ade12020-08-27 16:49:20 -070010494 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010495};
10496
10497/**
10498 * Expect fallback to internal viewport if device is external and external viewport is not present.
10499 */
10500TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10501 prepareAxes(POSITION);
10502 addConfigurationProperty("touch.deviceType", "touchScreen");
10503 prepareDisplay(DISPLAY_ORIENTATION_0);
10504 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10505
10506 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10507
10508 NotifyMotionArgs motionArgs;
10509
10510 // Expect the event to be sent to the internal viewport,
10511 // because an external viewport is not present.
10512 processPosition(mapper, 100, 100);
10513 processSync(mapper);
10514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10515 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10516
10517 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010518 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010519 processPosition(mapper, 100, 100);
10520 processSync(mapper);
10521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10522 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10523}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010524
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010525TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10526 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10527 std::shared_ptr<FakePointerController> fakePointerController =
10528 std::make_shared<FakePointerController>();
10529 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10530 fakePointerController->setPosition(0, 0);
10531 fakePointerController->setButtonState(0);
10532
10533 // prepare device and capture
10534 prepareDisplay(DISPLAY_ORIENTATION_0);
10535 prepareAxes(POSITION | ID | SLOT);
10536 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10537 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10538 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010539 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010540 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10541
10542 // captured touchpad should be a touchpad source
10543 NotifyDeviceResetArgs resetArgs;
10544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10545 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10546
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010547 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010548
10549 const InputDeviceInfo::MotionRange* relRangeX =
10550 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10551 ASSERT_NE(relRangeX, nullptr);
10552 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10553 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10554 const InputDeviceInfo::MotionRange* relRangeY =
10555 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10556 ASSERT_NE(relRangeY, nullptr);
10557 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10558 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10559
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010560 // run captured pointer tests - note that this is unscaled, so input listener events should be
10561 // identical to what the hardware sends (accounting for any
10562 // calibration).
10563 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010564 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010565 processId(mapper, 1);
10566 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10567 processKey(mapper, BTN_TOUCH, 1);
10568 processSync(mapper);
10569
10570 // expect coord[0] to contain initial location of touch 0
10571 NotifyMotionArgs args;
10572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10573 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10574 ASSERT_EQ(1U, args.pointerCount);
10575 ASSERT_EQ(0, args.pointerProperties[0].id);
10576 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10577 ASSERT_NO_FATAL_FAILURE(
10578 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10579
10580 // FINGER 1 DOWN
10581 processSlot(mapper, 1);
10582 processId(mapper, 2);
10583 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10584 processSync(mapper);
10585
10586 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010588 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010589 ASSERT_EQ(2U, args.pointerCount);
10590 ASSERT_EQ(0, args.pointerProperties[0].id);
10591 ASSERT_EQ(1, args.pointerProperties[1].id);
10592 ASSERT_NO_FATAL_FAILURE(
10593 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10594 ASSERT_NO_FATAL_FAILURE(
10595 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10596
10597 // FINGER 1 MOVE
10598 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10599 processSync(mapper);
10600
10601 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10602 // from move
10603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10605 ASSERT_NO_FATAL_FAILURE(
10606 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10607 ASSERT_NO_FATAL_FAILURE(
10608 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10609
10610 // FINGER 0 MOVE
10611 processSlot(mapper, 0);
10612 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10613 processSync(mapper);
10614
10615 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10617 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10618 ASSERT_NO_FATAL_FAILURE(
10619 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10620 ASSERT_NO_FATAL_FAILURE(
10621 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10622
10623 // BUTTON DOWN
10624 processKey(mapper, BTN_LEFT, 1);
10625 processSync(mapper);
10626
10627 // touchinputmapper design sends a move before button press
10628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10629 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10631 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10632
10633 // BUTTON UP
10634 processKey(mapper, BTN_LEFT, 0);
10635 processSync(mapper);
10636
10637 // touchinputmapper design sends a move after button release
10638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10639 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10641 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10642
10643 // FINGER 0 UP
10644 processId(mapper, -1);
10645 processSync(mapper);
10646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10647 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10648
10649 // FINGER 1 MOVE
10650 processSlot(mapper, 1);
10651 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10652 processSync(mapper);
10653
10654 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10656 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10657 ASSERT_EQ(1U, args.pointerCount);
10658 ASSERT_EQ(1, args.pointerProperties[0].id);
10659 ASSERT_NO_FATAL_FAILURE(
10660 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10661
10662 // FINGER 1 UP
10663 processId(mapper, -1);
10664 processKey(mapper, BTN_TOUCH, 0);
10665 processSync(mapper);
10666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10667 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10668
Harry Cutts16a24cc2022-10-26 15:22:19 +000010669 // A non captured touchpad should have a mouse and touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010670 mFakePolicy->setPointerCapture(false);
10671 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Harry Cutts16a24cc2022-10-26 15:22:19 +000010673 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010674}
10675
10676TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10677 std::shared_ptr<FakePointerController> fakePointerController =
10678 std::make_shared<FakePointerController>();
10679 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10680 fakePointerController->setPosition(0, 0);
10681 fakePointerController->setButtonState(0);
10682
10683 // prepare device and capture
10684 prepareDisplay(DISPLAY_ORIENTATION_0);
10685 prepareAxes(POSITION | ID | SLOT);
10686 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10687 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010688 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010689 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10690 // run uncaptured pointer tests - pushes out generic events
10691 // FINGER 0 DOWN
10692 processId(mapper, 3);
10693 processPosition(mapper, 100, 100);
10694 processKey(mapper, BTN_TOUCH, 1);
10695 processSync(mapper);
10696
10697 // start at (100,100), cursor should be at (0,0) * scale
10698 NotifyMotionArgs args;
10699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10700 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10701 ASSERT_NO_FATAL_FAILURE(
10702 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10703
10704 // FINGER 0 MOVE
10705 processPosition(mapper, 200, 200);
10706 processSync(mapper);
10707
10708 // compute scaling to help with touch position checking
10709 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10710 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10711 float scale =
10712 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10713
10714 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10716 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10718 0, 0, 0, 0, 0, 0, 0));
10719}
10720
10721TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10722 std::shared_ptr<FakePointerController> fakePointerController =
10723 std::make_shared<FakePointerController>();
10724
10725 prepareDisplay(DISPLAY_ORIENTATION_0);
10726 prepareAxes(POSITION | ID | SLOT);
10727 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010728 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010729 mFakePolicy->setPointerCapture(false);
10730 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10731
Harry Cutts16a24cc2022-10-26 15:22:19 +000010732 // An uncaptured touchpad should be a pointer device, with additional touchpad source.
10733 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010734
Harry Cutts16a24cc2022-10-26 15:22:19 +000010735 // A captured touchpad should just have a touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010736 mFakePolicy->setPointerCapture(true);
10737 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10738 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10739}
10740
HQ Liue6983c72022-04-19 22:14:56 +000010741class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10742protected:
10743 float mPointerMovementScale;
10744 float mPointerXZoomScale;
10745 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10746 addConfigurationProperty("touch.deviceType", "pointer");
10747 std::shared_ptr<FakePointerController> fakePointerController =
10748 std::make_shared<FakePointerController>();
10749 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10750 fakePointerController->setPosition(0, 0);
10751 fakePointerController->setButtonState(0);
10752 prepareDisplay(DISPLAY_ORIENTATION_0);
10753
10754 prepareAxes(POSITION);
10755 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10756 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10757 // needs to be disabled, and the pointer gesture needs to be enabled.
10758 mFakePolicy->setPointerCapture(false);
10759 mFakePolicy->setPointerGestureEnabled(true);
10760 mFakePolicy->setPointerController(fakePointerController);
10761
10762 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10763 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10764 mPointerMovementScale =
10765 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10766 mPointerXZoomScale =
10767 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10768 }
10769
10770 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10771 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10772 /*flat*/ 0,
10773 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10774 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10775 /*flat*/ 0,
10776 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10777 }
10778};
10779
10780/**
10781 * Two fingers down on a pointer mode touch pad. The width
10782 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10783 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10784 * be greater than the both value to be freeform gesture, so that after two
10785 * fingers start to move downwards, the gesture should be swipe.
10786 */
10787TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10788 // The min freeform gesture width is 25units/mm x 30mm = 750
10789 // which is greater than fraction of the diagnal length of the touchpad (349).
10790 // Thus, MaxSwipWidth is 750.
10791 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10792 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10793 NotifyMotionArgs motionArgs;
10794
10795 // Two fingers down at once.
10796 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10797 // Pointer's initial position is used the [0,0] coordinate.
10798 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10799
10800 processId(mapper, FIRST_TRACKING_ID);
10801 processPosition(mapper, x1, y1);
10802 processMTSync(mapper);
10803 processId(mapper, SECOND_TRACKING_ID);
10804 processPosition(mapper, x2, y2);
10805 processMTSync(mapper);
10806 processSync(mapper);
10807
10808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10809 ASSERT_EQ(1U, motionArgs.pointerCount);
10810 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10811 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010812 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010813 ASSERT_NO_FATAL_FAILURE(
10814 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10815
10816 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10817 // that there should be 1 pointer.
10818 int32_t movingDistance = 200;
10819 y1 += movingDistance;
10820 y2 += movingDistance;
10821
10822 processId(mapper, FIRST_TRACKING_ID);
10823 processPosition(mapper, x1, y1);
10824 processMTSync(mapper);
10825 processId(mapper, SECOND_TRACKING_ID);
10826 processPosition(mapper, x2, y2);
10827 processMTSync(mapper);
10828 processSync(mapper);
10829
10830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10831 ASSERT_EQ(1U, motionArgs.pointerCount);
10832 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10833 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010834 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10836 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10837 0, 0, 0, 0));
10838}
10839
10840/**
10841 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10842 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10843 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10844 * value to be freeform gesture, so that after two fingers start to move downwards,
10845 * the gesture should be swipe.
10846 */
10847TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10848 // The min freeform gesture width is 5units/mm x 30mm = 150
10849 // which is greater than fraction of the diagnal length of the touchpad (349).
10850 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
10851 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
10852 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10853 NotifyMotionArgs motionArgs;
10854
10855 // Two fingers down at once.
10856 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10857 // Pointer's initial position is used the [0,0] coordinate.
10858 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10859
10860 processId(mapper, FIRST_TRACKING_ID);
10861 processPosition(mapper, x1, y1);
10862 processMTSync(mapper);
10863 processId(mapper, SECOND_TRACKING_ID);
10864 processPosition(mapper, x2, y2);
10865 processMTSync(mapper);
10866 processSync(mapper);
10867
10868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10869 ASSERT_EQ(1U, motionArgs.pointerCount);
10870 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10871 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010872 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010873 ASSERT_NO_FATAL_FAILURE(
10874 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10875
10876 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10877 // and there should be 1 pointer.
10878 int32_t movingDistance = 200;
10879 y1 += movingDistance;
10880 y2 += movingDistance;
10881
10882 processId(mapper, FIRST_TRACKING_ID);
10883 processPosition(mapper, x1, y1);
10884 processMTSync(mapper);
10885 processId(mapper, SECOND_TRACKING_ID);
10886 processPosition(mapper, x2, y2);
10887 processMTSync(mapper);
10888 processSync(mapper);
10889
10890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10891 ASSERT_EQ(1U, motionArgs.pointerCount);
10892 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10893 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010894 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010895 // New coordinate is the scaled relative coordinate from the initial coordinate.
10896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10897 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10898 0, 0, 0, 0));
10899}
10900
10901/**
10902 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10903 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10904 * freeform gestures after two fingers start to move downwards.
10905 */
10906TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
10907 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10908 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10909
10910 NotifyMotionArgs motionArgs;
10911
10912 // Two fingers down at once. Wider than the max swipe width.
10913 // The gesture is expected to be PRESS, then transformed to FREEFORM
10914 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10915
10916 processId(mapper, FIRST_TRACKING_ID);
10917 processPosition(mapper, x1, y1);
10918 processMTSync(mapper);
10919 processId(mapper, SECOND_TRACKING_ID);
10920 processPosition(mapper, x2, y2);
10921 processMTSync(mapper);
10922 processSync(mapper);
10923
10924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10925 ASSERT_EQ(1U, motionArgs.pointerCount);
10926 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10927 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010928 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010929 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10930 ASSERT_NO_FATAL_FAILURE(
10931 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10932
10933 int32_t movingDistance = 200;
10934
10935 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10936 // then two down events for two pointers.
10937 y1 += movingDistance;
10938 y2 += movingDistance;
10939
10940 processId(mapper, FIRST_TRACKING_ID);
10941 processPosition(mapper, x1, y1);
10942 processMTSync(mapper);
10943 processId(mapper, SECOND_TRACKING_ID);
10944 processPosition(mapper, x2, y2);
10945 processMTSync(mapper);
10946 processSync(mapper);
10947
10948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10949 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10950 ASSERT_EQ(1U, motionArgs.pointerCount);
10951 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10953 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10954 ASSERT_EQ(1U, motionArgs.pointerCount);
10955 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10957 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010958 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010959 ASSERT_EQ(2U, motionArgs.pointerCount);
10960 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10961 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010962 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010963 // Two pointers' scaled relative coordinates from their initial centroid.
10964 // Initial y coordinates are 0 as y1 and y2 have the same value.
10965 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10966 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10967 // When pointers move, the new coordinates equal to the initial coordinates plus
10968 // scaled moving distance.
10969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10970 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10971 0, 0, 0, 0));
10972 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10973 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10974 0, 0, 0, 0));
10975
10976 // Move two fingers down again, expect one MOVE motion event.
10977 y1 += movingDistance;
10978 y2 += movingDistance;
10979
10980 processId(mapper, FIRST_TRACKING_ID);
10981 processPosition(mapper, x1, y1);
10982 processMTSync(mapper);
10983 processId(mapper, SECOND_TRACKING_ID);
10984 processPosition(mapper, x2, y2);
10985 processMTSync(mapper);
10986 processSync(mapper);
10987
10988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10989 ASSERT_EQ(2U, motionArgs.pointerCount);
10990 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10991 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010992 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10994 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10995 0, 0, 0, 0, 0));
10996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10997 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10998 0, 0, 0, 0, 0));
10999}
11000
Harry Cutts39b7ca22022-10-05 15:55:48 +000011001TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
11002 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
11003 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11004 NotifyMotionArgs motionArgs;
11005
11006 // Place two fingers down.
11007 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
11008
11009 processId(mapper, FIRST_TRACKING_ID);
11010 processPosition(mapper, x1, y1);
11011 processMTSync(mapper);
11012 processId(mapper, SECOND_TRACKING_ID);
11013 processPosition(mapper, x2, y2);
11014 processMTSync(mapper);
11015 processSync(mapper);
11016
11017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11018 ASSERT_EQ(1U, motionArgs.pointerCount);
11019 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11020 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
11021 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
11022 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
11023
11024 // Move the two fingers down and to the left.
11025 int32_t movingDistance = 200;
11026 x1 -= movingDistance;
11027 y1 += movingDistance;
11028 x2 -= movingDistance;
11029 y2 += movingDistance;
11030
11031 processId(mapper, FIRST_TRACKING_ID);
11032 processPosition(mapper, x1, y1);
11033 processMTSync(mapper);
11034 processId(mapper, SECOND_TRACKING_ID);
11035 processPosition(mapper, x2, y2);
11036 processMTSync(mapper);
11037 processSync(mapper);
11038
11039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11040 ASSERT_EQ(1U, motionArgs.pointerCount);
11041 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11042 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
11043 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
11044 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
11045}
11046
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011047TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
11048 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
11049 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
11050 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
11052
11053 // Start a stylus gesture.
11054 processKey(mapper, BTN_TOOL_PEN, 1);
11055 processId(mapper, FIRST_TRACKING_ID);
11056 processPosition(mapper, 100, 200);
11057 processSync(mapper);
11058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11059 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
11060 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11061 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11062 // TODO(b/257078296): Pointer mode generates extra event.
11063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11064 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11065 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11066 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11068
11069 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
11070 // gesture should be disabled.
11071 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
11072 viewport->isActive = false;
11073 mFakePolicy->updateViewport(*viewport);
11074 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
11075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11076 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11077 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11078 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11079 // TODO(b/257078296): Pointer mode generates extra event.
11080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11081 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11082 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11083 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11085}
11086
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011087// --- JoystickInputMapperTest ---
11088
11089class JoystickInputMapperTest : public InputMapperTest {
11090protected:
11091 static const int32_t RAW_X_MIN;
11092 static const int32_t RAW_X_MAX;
11093 static const int32_t RAW_Y_MIN;
11094 static const int32_t RAW_Y_MAX;
11095
11096 void SetUp() override {
11097 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
11098 }
11099 void prepareAxes() {
11100 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
11101 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
11102 }
11103
11104 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
11105 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
11106 }
11107
11108 void processSync(JoystickInputMapper& mapper) {
11109 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
11110 }
11111
11112 void prepareVirtualDisplay(int32_t orientation) {
11113 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
11114 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
11115 NO_PORT, ViewportType::VIRTUAL);
11116 }
11117};
11118
11119const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
11120const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
11121const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
11122const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
11123
11124TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
11125 prepareAxes();
11126 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
11127
11128 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
11129
11130 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
11131
11132 // Send an axis event
11133 processAxis(mapper, ABS_X, 100);
11134 processSync(mapper);
11135
11136 NotifyMotionArgs args;
11137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11138 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11139
11140 // Send another axis event
11141 processAxis(mapper, ABS_Y, 100);
11142 processSync(mapper);
11143
11144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11145 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11146}
11147
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011148// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011149
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011150class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011151protected:
11152 static const char* DEVICE_NAME;
11153 static const char* DEVICE_LOCATION;
11154 static const int32_t DEVICE_ID;
11155 static const int32_t DEVICE_GENERATION;
11156 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011157 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011158 static const int32_t EVENTHUB_ID;
11159
11160 std::shared_ptr<FakeEventHub> mFakeEventHub;
11161 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011162 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011163 std::unique_ptr<InstrumentedInputReader> mReader;
11164 std::shared_ptr<InputDevice> mDevice;
11165
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011166 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011167 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011168 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011169 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011170 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011171 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011172 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11173 }
11174
11175 void SetUp() override { SetUp(DEVICE_CLASSES); }
11176
11177 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011178 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011179 mFakePolicy.clear();
11180 }
11181
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070011182 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011183 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
11184 mReader->requestRefreshConfiguration(changes);
11185 mReader->loopOnce();
11186 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070011187 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011188 }
11189
11190 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11191 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011192 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011193 InputDeviceIdentifier identifier;
11194 identifier.name = name;
11195 identifier.location = location;
11196 std::shared_ptr<InputDevice> device =
11197 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11198 identifier);
11199 mReader->pushNextDevice(device);
11200 mFakeEventHub->addDevice(eventHubId, name, classes);
11201 mReader->loopOnce();
11202 return device;
11203 }
11204
11205 template <class T, typename... Args>
11206 T& addControllerAndConfigure(Args... args) {
11207 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11208
11209 return controller;
11210 }
11211};
11212
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011213const char* PeripheralControllerTest::DEVICE_NAME = "device";
11214const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11215const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11216const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11217const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011218const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11219 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011220const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011221
11222// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011223class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011224protected:
11225 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011226 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011227 }
11228};
11229
11230TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011231 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011232
11233 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
11234 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
11235}
11236
11237TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011238 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011239
11240 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
11241 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
11242}
11243
11244// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011245class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011246protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011247 void SetUp() override {
11248 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11249 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011250};
11251
Chris Ye85758332021-05-16 23:05:17 -070011252TEST_F(LightControllerTest, MonoLight) {
11253 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011254 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011255 .maxBrightness = 255,
11256 .flags = InputLightClass::BRIGHTNESS,
11257 .path = ""};
11258 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011259
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011260 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011261 InputDeviceInfo info;
11262 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011263 std::vector<InputDeviceLightInfo> lights = info.getLights();
11264 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011265 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11266 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11267
11268 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11269 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11270}
11271
11272TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11273 RawLightInfo infoMono = {.id = 1,
11274 .name = "mono_keyboard_backlight",
11275 .maxBrightness = 255,
11276 .flags = InputLightClass::BRIGHTNESS |
11277 InputLightClass::KEYBOARD_BACKLIGHT,
11278 .path = ""};
11279 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11280
11281 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11282 InputDeviceInfo info;
11283 controller.populateDeviceInfo(&info);
11284 std::vector<InputDeviceLightInfo> lights = info.getLights();
11285 ASSERT_EQ(1U, lights.size());
11286 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11287 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011288
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011289 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11290 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011291}
11292
11293TEST_F(LightControllerTest, RGBLight) {
11294 RawLightInfo infoRed = {.id = 1,
11295 .name = "red",
11296 .maxBrightness = 255,
11297 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11298 .path = ""};
11299 RawLightInfo infoGreen = {.id = 2,
11300 .name = "green",
11301 .maxBrightness = 255,
11302 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11303 .path = ""};
11304 RawLightInfo infoBlue = {.id = 3,
11305 .name = "blue",
11306 .maxBrightness = 255,
11307 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11308 .path = ""};
11309 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11310 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11311 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11312
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011313 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011314 InputDeviceInfo info;
11315 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011316 std::vector<InputDeviceLightInfo> lights = info.getLights();
11317 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011318 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11319 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11320 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11321
11322 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11323 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11324}
11325
11326TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11327 RawLightInfo infoRed = {.id = 1,
11328 .name = "red_keyboard_backlight",
11329 .maxBrightness = 255,
11330 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11331 InputLightClass::KEYBOARD_BACKLIGHT,
11332 .path = ""};
11333 RawLightInfo infoGreen = {.id = 2,
11334 .name = "green_keyboard_backlight",
11335 .maxBrightness = 255,
11336 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11337 InputLightClass::KEYBOARD_BACKLIGHT,
11338 .path = ""};
11339 RawLightInfo infoBlue = {.id = 3,
11340 .name = "blue_keyboard_backlight",
11341 .maxBrightness = 255,
11342 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11343 InputLightClass::KEYBOARD_BACKLIGHT,
11344 .path = ""};
11345 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11346 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11347 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11348
11349 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11350 InputDeviceInfo info;
11351 controller.populateDeviceInfo(&info);
11352 std::vector<InputDeviceLightInfo> lights = info.getLights();
11353 ASSERT_EQ(1U, lights.size());
11354 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11355 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11356 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11357
11358 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11359 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11360}
11361
11362TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11363 RawLightInfo infoRed = {.id = 1,
11364 .name = "red",
11365 .maxBrightness = 255,
11366 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11367 .path = ""};
11368 RawLightInfo infoGreen = {.id = 2,
11369 .name = "green",
11370 .maxBrightness = 255,
11371 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11372 .path = ""};
11373 RawLightInfo infoBlue = {.id = 3,
11374 .name = "blue",
11375 .maxBrightness = 255,
11376 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11377 .path = ""};
11378 RawLightInfo infoGlobal = {.id = 3,
11379 .name = "global_keyboard_backlight",
11380 .maxBrightness = 255,
11381 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11382 InputLightClass::KEYBOARD_BACKLIGHT,
11383 .path = ""};
11384 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11385 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11386 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11387 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11388
11389 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11390 InputDeviceInfo info;
11391 controller.populateDeviceInfo(&info);
11392 std::vector<InputDeviceLightInfo> lights = info.getLights();
11393 ASSERT_EQ(1U, lights.size());
11394 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11395 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11396 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011397
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011398 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11399 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011400}
11401
11402TEST_F(LightControllerTest, MultiColorRGBLight) {
11403 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011404 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011405 .maxBrightness = 255,
11406 .flags = InputLightClass::BRIGHTNESS |
11407 InputLightClass::MULTI_INTENSITY |
11408 InputLightClass::MULTI_INDEX,
11409 .path = ""};
11410
11411 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11412
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011413 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011414 InputDeviceInfo info;
11415 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011416 std::vector<InputDeviceLightInfo> lights = info.getLights();
11417 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011418 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11419 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11420 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11421
11422 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11423 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11424}
11425
11426TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11427 RawLightInfo infoColor = {.id = 1,
11428 .name = "multi_color_keyboard_backlight",
11429 .maxBrightness = 255,
11430 .flags = InputLightClass::BRIGHTNESS |
11431 InputLightClass::MULTI_INTENSITY |
11432 InputLightClass::MULTI_INDEX |
11433 InputLightClass::KEYBOARD_BACKLIGHT,
11434 .path = ""};
11435
11436 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11437
11438 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11439 InputDeviceInfo info;
11440 controller.populateDeviceInfo(&info);
11441 std::vector<InputDeviceLightInfo> lights = info.getLights();
11442 ASSERT_EQ(1U, lights.size());
11443 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11444 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11445 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011446
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011447 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11448 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011449}
11450
11451TEST_F(LightControllerTest, PlayerIdLight) {
11452 RawLightInfo info1 = {.id = 1,
11453 .name = "player1",
11454 .maxBrightness = 255,
11455 .flags = InputLightClass::BRIGHTNESS,
11456 .path = ""};
11457 RawLightInfo info2 = {.id = 2,
11458 .name = "player2",
11459 .maxBrightness = 255,
11460 .flags = InputLightClass::BRIGHTNESS,
11461 .path = ""};
11462 RawLightInfo info3 = {.id = 3,
11463 .name = "player3",
11464 .maxBrightness = 255,
11465 .flags = InputLightClass::BRIGHTNESS,
11466 .path = ""};
11467 RawLightInfo info4 = {.id = 4,
11468 .name = "player4",
11469 .maxBrightness = 255,
11470 .flags = InputLightClass::BRIGHTNESS,
11471 .path = ""};
11472 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11473 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11474 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11475 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11476
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011477 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011478 InputDeviceInfo info;
11479 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011480 std::vector<InputDeviceLightInfo> lights = info.getLights();
11481 ASSERT_EQ(1U, lights.size());
11482 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011483 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11484 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011485
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011486 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11487 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11488 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011489}
11490
Michael Wrightd02c5b62014-02-10 15:10:22 -080011491} // namespace android