blob: eabb18cb32dc7cbf5382a10dab687294b6b32f42 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
19
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <CursorInputMapper.h>
21#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080024#include <InputReaderBase.h>
25#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000026#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070029#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070030#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
32#include <SwitchInputMapper.h>
33#include <TestInputListener.h>
Prabir Pradhan739dca42022-09-09 20:12:01 +000034#include <TestInputListenerMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070035#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080036#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000037#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070038#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080039#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050040#include <gui/constants.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070042#include <thread>
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000043#include "android/hardware/input/InputDeviceCountryCode.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000044#include "input/DisplayViewport.h"
45#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010046
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000047using android::hardware::input::InputDeviceCountryCode;
48
Michael Wrightd02c5b62014-02-10 15:10:22 -080049namespace android {
50
Dominik Laskowski2f01d772022-03-23 16:01:29 -070051using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000052using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070053using std::chrono_literals::operator""ms;
54
55// Timeout for waiting for an expected event
56static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
57
Michael Wrightd02c5b62014-02-10 15:10:22 -080058// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000059static constexpr nsecs_t ARBITRARY_TIME = 1234;
60static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080061
62// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080063static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000064static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080065static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000066static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080067static constexpr int32_t DISPLAY_WIDTH = 480;
68static constexpr int32_t DISPLAY_HEIGHT = 800;
69static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
70static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
71static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070072static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070073static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080074
arthurhungcc7f9802020-04-30 17:55:40 +080075static constexpr int32_t FIRST_SLOT = 0;
76static constexpr int32_t SECOND_SLOT = 1;
77static constexpr int32_t THIRD_SLOT = 2;
78static constexpr int32_t INVALID_TRACKING_ID = -1;
79static constexpr int32_t FIRST_TRACKING_ID = 0;
80static constexpr int32_t SECOND_TRACKING_ID = 1;
81static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080082static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080083static constexpr int32_t BATTERY_STATUS = 4;
84static constexpr int32_t BATTERY_CAPACITY = 66;
Prabir Pradhane287ecd2022-09-07 21:18:05 +000085static const std::string BATTERY_DEVPATH = "/sys/devices/mydevice/power_supply/mybattery";
Chris Ye3fdbfef2021-01-06 18:45:18 -080086static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
87static constexpr int32_t LIGHT_COLOR = 0x7F448866;
88static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080089
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080090static constexpr int32_t ACTION_POINTER_0_DOWN =
91 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
92static constexpr int32_t ACTION_POINTER_0_UP =
93 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
94static constexpr int32_t ACTION_POINTER_1_DOWN =
95 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
96static constexpr int32_t ACTION_POINTER_1_UP =
97 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
98
Michael Wrightd02c5b62014-02-10 15:10:22 -080099// Error tolerance for floating point assertions.
100static const float EPSILON = 0.001f;
101
102template<typename T>
103static inline T min(T a, T b) {
104 return a < b ? a : b;
105}
106
107static inline float avg(float x, float y) {
108 return (x + y) / 2;
109}
110
Chris Ye3fdbfef2021-01-06 18:45:18 -0800111// Mapping for light color name and the light color
112const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
113 {"green", LightColor::GREEN},
114 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800115
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700116static int32_t getInverseRotation(int32_t orientation) {
117 switch (orientation) {
118 case DISPLAY_ORIENTATION_90:
119 return DISPLAY_ORIENTATION_270;
120 case DISPLAY_ORIENTATION_270:
121 return DISPLAY_ORIENTATION_90;
122 default:
123 return orientation;
124 }
125}
126
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800127static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
128 InputDeviceInfo info;
129 mapper.populateDeviceInfo(&info);
130
131 const InputDeviceInfo::MotionRange* motionRange =
132 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
133 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
134}
135
136static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
137 InputDeviceInfo info;
138 mapper.populateDeviceInfo(&info);
139
140 const InputDeviceInfo::MotionRange* motionRange =
141 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
142 ASSERT_EQ(nullptr, motionRange);
143}
144
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700145[[maybe_unused]] static void dumpReader(InputReader& reader) {
146 std::string dump;
147 reader.dump(dump);
148 std::istringstream iss(dump);
149 for (std::string line; std::getline(iss, line);) {
150 ALOGE("%s", line.c_str());
151 std::this_thread::sleep_for(std::chrono::milliseconds(1));
152 }
153}
154
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155// --- FakePointerController ---
156
157class FakePointerController : public PointerControllerInterface {
158 bool mHaveBounds;
159 float mMinX, mMinY, mMaxX, mMaxY;
160 float mX, mY;
161 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800162 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164public:
165 FakePointerController() :
166 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800167 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168 }
169
Michael Wright17db18e2020-06-26 20:51:44 +0100170 virtual ~FakePointerController() {}
171
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172 void setBounds(float minX, float minY, float maxX, float maxY) {
173 mHaveBounds = true;
174 mMinX = minX;
175 mMinY = minY;
176 mMaxX = maxX;
177 mMaxY = maxY;
178 }
179
Chris Yea52ade12020-08-27 16:49:20 -0700180 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181 mX = x;
182 mY = y;
183 }
184
Chris Yea52ade12020-08-27 16:49:20 -0700185 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186
Chris Yea52ade12020-08-27 16:49:20 -0700187 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188
Chris Yea52ade12020-08-27 16:49:20 -0700189 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190 *outX = mX;
191 *outY = mY;
192 }
193
Chris Yea52ade12020-08-27 16:49:20 -0700194 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800195
Chris Yea52ade12020-08-27 16:49:20 -0700196 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800197 mDisplayId = viewport.displayId;
198 }
199
Arthur Hung7c645402019-01-25 17:45:42 +0800200 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
201 return mSpotsByDisplay;
202 }
203
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204private:
Chris Yea52ade12020-08-27 16:49:20 -0700205 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206 *outMinX = mMinX;
207 *outMinY = mMinY;
208 *outMaxX = mMaxX;
209 *outMaxY = mMaxY;
210 return mHaveBounds;
211 }
212
Chris Yea52ade12020-08-27 16:49:20 -0700213 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800214 mX += deltaX;
215 if (mX < mMinX) mX = mMinX;
216 if (mX > mMaxX) mX = mMaxX;
217 mY += deltaY;
218 if (mY < mMinY) mY = mMinY;
219 if (mY > mMaxY) mY = mMaxY;
220 }
221
Chris Yea52ade12020-08-27 16:49:20 -0700222 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800223
Chris Yea52ade12020-08-27 16:49:20 -0700224 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225
Chris Yea52ade12020-08-27 16:49:20 -0700226 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800227
Chris Yea52ade12020-08-27 16:49:20 -0700228 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
229 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800230 std::vector<int32_t> newSpots;
231 // Add spots for fingers that are down.
232 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
233 uint32_t id = idBits.clearFirstMarkedBit();
234 newSpots.push_back(id);
235 }
236
237 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800238 }
239
Prabir Pradhan197e0862022-07-01 14:28:00 +0000240 void clearSpots() override { mSpotsByDisplay.clear(); }
Arthur Hung7c645402019-01-25 17:45:42 +0800241
242 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243};
244
245
246// --- FakeInputReaderPolicy ---
247
248class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700249 std::mutex mLock;
250 std::condition_variable mDevicesChangedCondition;
251
Michael Wrightd02c5b62014-02-10 15:10:22 -0800252 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000253 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700254 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
255 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100256 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700257 TouchAffineTransformation transform;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000258 std::optional<int32_t /*deviceId*/> mStylusGestureNotified GUARDED_BY(mLock){};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259
260protected:
Chris Yea52ade12020-08-27 16:49:20 -0700261 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800262
263public:
264 FakeInputReaderPolicy() {
265 }
266
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700267 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800268 waitForInputDevices([](bool devicesChanged) {
269 if (!devicesChanged) {
270 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
271 }
272 });
273 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700274
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800275 void assertInputDevicesNotChanged() {
276 waitForInputDevices([](bool devicesChanged) {
277 if (devicesChanged) {
278 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
279 }
280 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700281 }
282
Prabir Pradhanda20b172022-09-26 17:01:18 +0000283 void assertStylusGestureNotified(int32_t deviceId) {
284 std::scoped_lock lock(mLock);
285 ASSERT_TRUE(mStylusGestureNotified);
286 ASSERT_EQ(deviceId, *mStylusGestureNotified);
287 mStylusGestureNotified.reset();
288 }
289
290 void assertStylusGestureNotNotified() {
291 std::scoped_lock lock(mLock);
292 ASSERT_FALSE(mStylusGestureNotified);
293 }
294
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700295 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100296 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100297 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700298 }
299
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700300 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
301 return mConfig.getDisplayViewportByUniqueId(uniqueId);
302 }
303 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
304 return mConfig.getDisplayViewportByType(type);
305 }
306
307 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
308 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700309 }
310
Prabir Pradhan5632d622021-09-06 07:57:20 -0700311 void addDisplayViewport(DisplayViewport viewport) {
312 mViewports.push_back(std::move(viewport));
313 mConfig.setDisplayViewports(mViewports);
314 }
315
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700316 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000317 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700318 std::optional<uint8_t> physicalPort, ViewportType type) {
319 const bool isRotated =
320 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
321 DisplayViewport v;
322 v.displayId = displayId;
323 v.orientation = orientation;
324 v.logicalLeft = 0;
325 v.logicalTop = 0;
326 v.logicalRight = isRotated ? height : width;
327 v.logicalBottom = isRotated ? width : height;
328 v.physicalLeft = 0;
329 v.physicalTop = 0;
330 v.physicalRight = isRotated ? height : width;
331 v.physicalBottom = isRotated ? width : height;
332 v.deviceWidth = isRotated ? height : width;
333 v.deviceHeight = isRotated ? width : height;
334 v.isActive = isActive;
335 v.uniqueId = uniqueId;
336 v.physicalPort = physicalPort;
337 v.type = type;
338
339 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800340 }
341
Arthur Hung6cd19a42019-08-30 19:04:12 +0800342 bool updateViewport(const DisplayViewport& viewport) {
343 size_t count = mViewports.size();
344 for (size_t i = 0; i < count; i++) {
345 const DisplayViewport& currentViewport = mViewports[i];
346 if (currentViewport.displayId == viewport.displayId) {
347 mViewports[i] = viewport;
348 mConfig.setDisplayViewports(mViewports);
349 return true;
350 }
351 }
352 // no viewport found.
353 return false;
354 }
355
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100356 void addExcludedDeviceName(const std::string& deviceName) {
357 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800358 }
359
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700360 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
361 mConfig.portAssociations.insert({inputPort, displayPort});
362 }
363
Christine Franks1ba71cc2021-04-07 14:37:42 -0700364 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
365 const std::string& displayUniqueId) {
366 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
367 }
368
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000369 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700370
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000371 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700372
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000373 void setPointerController(std::shared_ptr<FakePointerController> controller) {
374 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800375 }
376
377 const InputReaderConfiguration* getReaderConfiguration() const {
378 return &mConfig;
379 }
380
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800381 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382 return mInputDevices;
383 }
384
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100385 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700386 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700387 return transform;
388 }
389
390 void setTouchAffineTransformation(const TouchAffineTransformation t) {
391 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800392 }
393
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000394 PointerCaptureRequest setPointerCapture(bool enabled) {
395 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
396 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800397 }
398
Arthur Hung7c645402019-01-25 17:45:42 +0800399 void setShowTouches(bool enabled) {
400 mConfig.showTouches = enabled;
401 }
402
Garfield Tan888a6a42020-01-09 11:39:16 -0800403 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
404 mConfig.defaultPointerDisplayId = pointerDisplayId;
405 }
406
HQ Liue6983c72022-04-19 22:14:56 +0000407 void setPointerGestureEnabled(bool enabled) { mConfig.pointerGesturesEnabled = enabled; }
408
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800409 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
410
HQ Liue6983c72022-04-19 22:14:56 +0000411 float getPointerGestureZoomSpeedRatio() { return mConfig.pointerGestureZoomSpeedRatio; }
412
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000413 void setVelocityControlParams(const VelocityControlParameters& params) {
414 mConfig.pointerVelocityControlParameters = params;
415 mConfig.wheelVelocityControlParameters = params;
416 }
417
Michael Wrightd02c5b62014-02-10 15:10:22 -0800418private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000419 uint32_t mNextPointerCaptureSequenceNumber = 0;
420
Chris Yea52ade12020-08-27 16:49:20 -0700421 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800422 *outConfig = mConfig;
423 }
424
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000425 std::shared_ptr<PointerControllerInterface> obtainPointerController(
426 int32_t /*deviceId*/) override {
427 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800428 }
429
Chris Yea52ade12020-08-27 16:49:20 -0700430 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700431 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800432 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700433 mInputDevicesChanged = true;
434 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800435 }
436
Chris Yea52ade12020-08-27 16:49:20 -0700437 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
438 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700439 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800440 }
441
Chris Yea52ade12020-08-27 16:49:20 -0700442 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800443
444 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
445 std::unique_lock<std::mutex> lock(mLock);
446 base::ScopedLockAssertion assumeLocked(mLock);
447
448 const bool devicesChanged =
449 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
450 return mInputDevicesChanged;
451 });
452 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
453 mInputDevicesChanged = false;
454 }
Prabir Pradhanda20b172022-09-26 17:01:18 +0000455
456 void notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) override {
457 std::scoped_lock<std::mutex> lock(mLock);
458 mStylusGestureNotified = deviceId;
459 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800460};
461
Michael Wrightd02c5b62014-02-10 15:10:22 -0800462// --- FakeEventHub ---
463
464class FakeEventHub : public EventHubInterface {
465 struct KeyInfo {
466 int32_t keyCode;
467 uint32_t flags;
468 };
469
Chris Yef59a2f42020-10-16 12:55:26 -0700470 struct SensorInfo {
471 InputDeviceSensorType sensorType;
472 int32_t sensorDataIndex;
473 };
474
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475 struct Device {
476 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700477 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800478 PropertyMap configuration;
479 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
480 KeyedVector<int, bool> relativeAxes;
481 KeyedVector<int32_t, int32_t> keyCodeStates;
482 KeyedVector<int32_t, int32_t> scanCodeStates;
483 KeyedVector<int32_t, int32_t> switchStates;
484 KeyedVector<int32_t, int32_t> absoluteAxisValue;
485 KeyedVector<int32_t, KeyInfo> keysByScanCode;
486 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
487 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100488 // fake mapping which would normally come from keyCharacterMap
489 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700490 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
491 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800492 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700493 bool enabled;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000494 InputDeviceCountryCode countryCode;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700495
496 status_t enable() {
497 enabled = true;
498 return OK;
499 }
500
501 status_t disable() {
502 enabled = false;
503 return OK;
504 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800505
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700506 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800507 };
508
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700509 std::mutex mLock;
510 std::condition_variable mEventsCondition;
511
Michael Wrightd02c5b62014-02-10 15:10:22 -0800512 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100513 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000514 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600515 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000516 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800517 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
518 // Simulates a device light brightness, from light id to light brightness.
519 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
520 // Simulates a device light intensities, from light id to light intensities map.
521 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
522 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800523
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700524public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800525 virtual ~FakeEventHub() {
526 for (size_t i = 0; i < mDevices.size(); i++) {
527 delete mDevices.valueAt(i);
528 }
529 }
530
Michael Wrightd02c5b62014-02-10 15:10:22 -0800531 FakeEventHub() { }
532
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700533 void addDevice(int32_t deviceId, const std::string& name,
534 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800535 Device* device = new Device(classes);
536 device->identifier.name = name;
537 mDevices.add(deviceId, device);
538
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000539 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800540 }
541
542 void removeDevice(int32_t deviceId) {
543 delete mDevices.valueFor(deviceId);
544 mDevices.removeItem(deviceId);
545
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000546 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800547 }
548
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000549 bool isDeviceEnabled(int32_t deviceId) const override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700550 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700551 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700552 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
553 return false;
554 }
555 return device->enabled;
556 }
557
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000558 status_t enableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700559 status_t result;
560 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700561 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700562 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
563 return BAD_VALUE;
564 }
565 if (device->enabled) {
566 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
567 return OK;
568 }
569 result = device->enable();
570 return result;
571 }
572
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000573 status_t disableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700574 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700575 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700576 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
577 return BAD_VALUE;
578 }
579 if (!device->enabled) {
580 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
581 return OK;
582 }
583 return device->disable();
584 }
585
Michael Wrightd02c5b62014-02-10 15:10:22 -0800586 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000587 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588 }
589
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700590 void addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800591 Device* device = getDevice(deviceId);
592 device->configuration.addProperty(key, value);
593 }
594
595 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
596 Device* device = getDevice(deviceId);
597 device->configuration.addAll(configuration);
598 }
599
600 void addAbsoluteAxis(int32_t deviceId, int axis,
601 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
602 Device* device = getDevice(deviceId);
603
604 RawAbsoluteAxisInfo info;
605 info.valid = true;
606 info.minValue = minValue;
607 info.maxValue = maxValue;
608 info.flat = flat;
609 info.fuzz = fuzz;
610 info.resolution = resolution;
611 device->absoluteAxes.add(axis, info);
612 }
613
614 void addRelativeAxis(int32_t deviceId, int32_t axis) {
615 Device* device = getDevice(deviceId);
616 device->relativeAxes.add(axis, true);
617 }
618
619 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
620 Device* device = getDevice(deviceId);
621 device->keyCodeStates.replaceValueFor(keyCode, state);
622 }
623
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000624 void setCountryCode(int32_t deviceId, InputDeviceCountryCode countryCode) {
625 Device* device = getDevice(deviceId);
626 device->countryCode = countryCode;
627 }
628
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
630 Device* device = getDevice(deviceId);
631 device->scanCodeStates.replaceValueFor(scanCode, state);
632 }
633
634 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
635 Device* device = getDevice(deviceId);
636 device->switchStates.replaceValueFor(switchCode, state);
637 }
638
639 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
640 Device* device = getDevice(deviceId);
641 device->absoluteAxisValue.replaceValueFor(axis, value);
642 }
643
644 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
645 int32_t keyCode, uint32_t flags) {
646 Device* device = getDevice(deviceId);
647 KeyInfo info;
648 info.keyCode = keyCode;
649 info.flags = flags;
650 if (scanCode) {
651 device->keysByScanCode.add(scanCode, info);
652 }
653 if (usageCode) {
654 device->keysByUsageCode.add(usageCode, info);
655 }
656 }
657
Philip Junker4af3b3d2021-12-14 10:36:55 +0100658 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
659 Device* device = getDevice(deviceId);
660 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
661 }
662
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 void addLed(int32_t deviceId, int32_t led, bool initialState) {
664 Device* device = getDevice(deviceId);
665 device->leds.add(led, initialState);
666 }
667
Chris Yef59a2f42020-10-16 12:55:26 -0700668 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
669 int32_t sensorDataIndex) {
670 Device* device = getDevice(deviceId);
671 SensorInfo info;
672 info.sensorType = sensorType;
673 info.sensorDataIndex = sensorDataIndex;
674 device->sensorsByAbsCode.emplace(absCode, info);
675 }
676
677 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
678 Device* device = getDevice(deviceId);
679 typename BitArray<MSC_MAX>::Buffer buffer;
680 buffer[mscEvent / 32] = 1 << mscEvent % 32;
681 device->mscBitmask.loadFromBuffer(buffer);
682 }
683
Chris Ye3fdbfef2021-01-06 18:45:18 -0800684 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
685 mRawLightInfos.emplace(rawId, std::move(info));
686 }
687
688 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
689 mLightBrightness.emplace(rawId, brightness);
690 }
691
692 void fakeLightIntensities(int32_t rawId,
693 const std::unordered_map<LightColor, int32_t> intensities) {
694 mLightIntensities.emplace(rawId, std::move(intensities));
695 }
696
Michael Wrightd02c5b62014-02-10 15:10:22 -0800697 bool getLedState(int32_t deviceId, int32_t led) {
698 Device* device = getDevice(deviceId);
699 return device->leds.valueFor(led);
700 }
701
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100702 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703 return mExcludedDevices;
704 }
705
706 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
707 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800708 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800709 }
710
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000711 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
712 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700713 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800714 RawEvent event;
715 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000716 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800717 event.deviceId = deviceId;
718 event.type = type;
719 event.code = code;
720 event.value = value;
721 mEvents.push_back(event);
722
723 if (type == EV_ABS) {
724 setAbsoluteAxisValue(deviceId, code, value);
725 }
726 }
727
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600728 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
729 std::vector<TouchVideoFrame>> videoFrames) {
730 mVideoFrames = std::move(videoFrames);
731 }
732
Michael Wrightd02c5b62014-02-10 15:10:22 -0800733 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700734 std::unique_lock<std::mutex> lock(mLock);
735 base::ScopedLockAssertion assumeLocked(mLock);
736 const bool queueIsEmpty =
737 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
738 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
739 if (!queueIsEmpty) {
740 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
741 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 }
743
744private:
745 Device* getDevice(int32_t deviceId) const {
746 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100747 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800748 }
749
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700750 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800751 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700752 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800753 }
754
Chris Yea52ade12020-08-27 16:49:20 -0700755 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800756 Device* device = getDevice(deviceId);
757 return device ? device->identifier : InputDeviceIdentifier();
758 }
759
Chris Yea52ade12020-08-27 16:49:20 -0700760 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800761
Chris Yea52ade12020-08-27 16:49:20 -0700762 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800763 Device* device = getDevice(deviceId);
764 if (device) {
765 *outConfiguration = device->configuration;
766 }
767 }
768
Chris Yea52ade12020-08-27 16:49:20 -0700769 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
770 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800771 Device* device = getDevice(deviceId);
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700772 if (device) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800773 ssize_t index = device->absoluteAxes.indexOfKey(axis);
774 if (index >= 0) {
775 *outAxisInfo = device->absoluteAxes.valueAt(index);
776 return OK;
777 }
778 }
779 outAxisInfo->clear();
780 return -1;
781 }
782
Chris Yea52ade12020-08-27 16:49:20 -0700783 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800784 Device* device = getDevice(deviceId);
785 if (device) {
786 return device->relativeAxes.indexOfKey(axis) >= 0;
787 }
788 return false;
789 }
790
Chris Yea52ade12020-08-27 16:49:20 -0700791 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800792
Chris Yef59a2f42020-10-16 12:55:26 -0700793 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
794 Device* device = getDevice(deviceId);
795 if (device) {
796 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
797 }
798 return false;
799 }
800
Chris Yea52ade12020-08-27 16:49:20 -0700801 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
802 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800803 Device* device = getDevice(deviceId);
804 if (device) {
805 const KeyInfo* key = getKey(device, scanCode, usageCode);
806 if (key) {
807 if (outKeycode) {
808 *outKeycode = key->keyCode;
809 }
810 if (outFlags) {
811 *outFlags = key->flags;
812 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700813 if (outMetaState) {
814 *outMetaState = metaState;
815 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800816 return OK;
817 }
818 }
819 return NAME_NOT_FOUND;
820 }
821
822 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
823 if (usageCode) {
824 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
825 if (index >= 0) {
826 return &device->keysByUsageCode.valueAt(index);
827 }
828 }
829 if (scanCode) {
830 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
831 if (index >= 0) {
832 return &device->keysByScanCode.valueAt(index);
833 }
834 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700835 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836 }
837
Chris Yea52ade12020-08-27 16:49:20 -0700838 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800839
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000840 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
841 int32_t deviceId, int32_t absCode) const override {
Chris Yef59a2f42020-10-16 12:55:26 -0700842 Device* device = getDevice(deviceId);
843 if (!device) {
844 return Errorf("Sensor device not found.");
845 }
846 auto it = device->sensorsByAbsCode.find(absCode);
847 if (it == device->sensorsByAbsCode.end()) {
848 return Errorf("Sensor map not found.");
849 }
850 const SensorInfo& info = it->second;
851 return std::make_pair(info.sensorType, info.sensorDataIndex);
852 }
853
Chris Yea52ade12020-08-27 16:49:20 -0700854 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800855 mExcludedDevices = devices;
856 }
857
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700858 std::vector<RawEvent> getEvents(int) override {
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000859 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700861 std::vector<RawEvent> buffer;
862 std::swap(buffer, mEvents);
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000863
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700864 mEventsCondition.notify_all();
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700865 return buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800866 }
867
Chris Yea52ade12020-08-27 16:49:20 -0700868 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600869 auto it = mVideoFrames.find(deviceId);
870 if (it != mVideoFrames.end()) {
871 std::vector<TouchVideoFrame> frames = std::move(it->second);
872 mVideoFrames.erase(deviceId);
873 return frames;
874 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800875 return {};
876 }
877
Chris Yea52ade12020-08-27 16:49:20 -0700878 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800879 Device* device = getDevice(deviceId);
880 if (device) {
881 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
882 if (index >= 0) {
883 return device->scanCodeStates.valueAt(index);
884 }
885 }
886 return AKEY_STATE_UNKNOWN;
887 }
888
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000889 InputDeviceCountryCode getCountryCode(int32_t deviceId) const override {
890 Device* device = getDevice(deviceId);
891 if (device) {
892 return device->countryCode;
893 }
894 return InputDeviceCountryCode::INVALID;
895 }
896
Chris Yea52ade12020-08-27 16:49:20 -0700897 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898 Device* device = getDevice(deviceId);
899 if (device) {
900 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
901 if (index >= 0) {
902 return device->keyCodeStates.valueAt(index);
903 }
904 }
905 return AKEY_STATE_UNKNOWN;
906 }
907
Chris Yea52ade12020-08-27 16:49:20 -0700908 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909 Device* device = getDevice(deviceId);
910 if (device) {
911 ssize_t index = device->switchStates.indexOfKey(sw);
912 if (index >= 0) {
913 return device->switchStates.valueAt(index);
914 }
915 }
916 return AKEY_STATE_UNKNOWN;
917 }
918
Chris Yea52ade12020-08-27 16:49:20 -0700919 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
920 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921 Device* device = getDevice(deviceId);
922 if (device) {
923 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
924 if (index >= 0) {
925 *outValue = device->absoluteAxisValue.valueAt(index);
926 return OK;
927 }
928 }
929 *outValue = 0;
930 return -1;
931 }
932
Philip Junker4af3b3d2021-12-14 10:36:55 +0100933 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
934 Device* device = getDevice(deviceId);
935 if (!device) {
936 return AKEYCODE_UNKNOWN;
937 }
938 auto it = device->keyCodeMapping.find(locationKeyCode);
939 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
940 }
941
Chris Yea52ade12020-08-27 16:49:20 -0700942 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700943 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700944 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945 bool result = false;
946 Device* device = getDevice(deviceId);
947 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700948 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700949 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800950 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
951 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
952 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953 }
954 }
955 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
956 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
957 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958 }
959 }
960 }
961 }
962 return result;
963 }
964
Chris Yea52ade12020-08-27 16:49:20 -0700965 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800966 Device* device = getDevice(deviceId);
967 if (device) {
968 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
969 return index >= 0;
970 }
971 return false;
972 }
973
Arthur Hungcb40a002021-08-03 14:31:01 +0000974 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
975 Device* device = getDevice(deviceId);
976 if (!device) {
977 return false;
978 }
979 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
980 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
981 return true;
982 }
983 }
984 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
985 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
986 return true;
987 }
988 }
989 return false;
990 }
991
Chris Yea52ade12020-08-27 16:49:20 -0700992 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800993 Device* device = getDevice(deviceId);
994 return device && device->leds.indexOfKey(led) >= 0;
995 }
996
Chris Yea52ade12020-08-27 16:49:20 -0700997 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800998 Device* device = getDevice(deviceId);
999 if (device) {
1000 ssize_t index = device->leds.indexOfKey(led);
1001 if (index >= 0) {
1002 device->leds.replaceValueAt(led, on);
1003 } else {
1004 ADD_FAILURE()
1005 << "Attempted to set the state of an LED that the EventHub declared "
1006 "was not present. led=" << led;
1007 }
1008 }
1009 }
1010
Chris Yea52ade12020-08-27 16:49:20 -07001011 void getVirtualKeyDefinitions(
1012 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001013 outVirtualKeys.clear();
1014
1015 Device* device = getDevice(deviceId);
1016 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001017 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001018 }
1019 }
1020
Chris Yea52ade12020-08-27 16:49:20 -07001021 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -07001022 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001023 }
1024
Chris Yea52ade12020-08-27 16:49:20 -07001025 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001026 return false;
1027 }
1028
Chris Yea52ade12020-08-27 16:49:20 -07001029 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001030
Chris Yea52ade12020-08-27 16:49:20 -07001031 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001032
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001033 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return mVibrators; };
Chris Ye87143712020-11-10 05:05:58 +00001034
Chris Yee2b1e5c2021-03-10 22:45:12 -08001035 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
1036 return BATTERY_CAPACITY;
1037 }
Kim Low03ea0352020-11-06 12:45:07 -08001038
Chris Yee2b1e5c2021-03-10 22:45:12 -08001039 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
1040 return BATTERY_STATUS;
1041 }
1042
Andy Chenf9f1a022022-08-29 20:07:10 -04001043 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override {
1044 return {DEFAULT_BATTERY};
1045 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001046
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001047 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
1048 int32_t batteryId) const override {
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001049 if (batteryId != DEFAULT_BATTERY) return {};
1050 static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY,
1051 .name = "default battery",
1052 .flags = InputBatteryClass::CAPACITY,
1053 .path = BATTERY_DEVPATH};
1054 return BATTERY_INFO;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001055 }
Kim Low03ea0352020-11-06 12:45:07 -08001056
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001057 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001058 std::vector<int32_t> ids;
1059 for (const auto& [rawId, info] : mRawLightInfos) {
1060 ids.push_back(rawId);
1061 }
1062 return ids;
1063 }
1064
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001065 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001066 auto it = mRawLightInfos.find(lightId);
1067 if (it == mRawLightInfos.end()) {
1068 return std::nullopt;
1069 }
1070 return it->second;
1071 }
1072
1073 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1074 mLightBrightness.emplace(lightId, brightness);
1075 }
1076
1077 void setLightIntensities(int32_t deviceId, int32_t lightId,
1078 std::unordered_map<LightColor, int32_t> intensities) override {
1079 mLightIntensities.emplace(lightId, intensities);
1080 };
1081
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001082 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001083 auto lightIt = mLightBrightness.find(lightId);
1084 if (lightIt == mLightBrightness.end()) {
1085 return std::nullopt;
1086 }
1087 return lightIt->second;
1088 }
1089
1090 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001091 int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001092 auto lightIt = mLightIntensities.find(lightId);
1093 if (lightIt == mLightIntensities.end()) {
1094 return std::nullopt;
1095 }
1096 return lightIt->second;
1097 };
1098
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001099 void dump(std::string&) const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001100
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001101 void monitor() const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102
Chris Yea52ade12020-08-27 16:49:20 -07001103 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001104
Chris Yea52ade12020-08-27 16:49:20 -07001105 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001106};
1107
Michael Wrightd02c5b62014-02-10 15:10:22 -08001108// --- FakeInputMapper ---
1109
1110class FakeInputMapper : public InputMapper {
1111 uint32_t mSources;
1112 int32_t mKeyboardType;
1113 int32_t mMetaState;
1114 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1115 KeyedVector<int32_t, int32_t> mScanCodeStates;
1116 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001117 // fake mapping which would normally come from keyCharacterMap
1118 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001119 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001120
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001121 std::mutex mLock;
1122 std::condition_variable mStateChangedCondition;
1123 bool mConfigureWasCalled GUARDED_BY(mLock);
1124 bool mResetWasCalled GUARDED_BY(mLock);
1125 bool mProcessWasCalled GUARDED_BY(mLock);
1126 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001127
Arthur Hungc23540e2018-11-29 20:42:11 +08001128 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001129public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001130 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1131 : InputMapper(deviceContext),
1132 mSources(sources),
1133 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001134 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001135 mConfigureWasCalled(false),
1136 mResetWasCalled(false),
1137 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001138
Chris Yea52ade12020-08-27 16:49:20 -07001139 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001140
1141 void setKeyboardType(int32_t keyboardType) {
1142 mKeyboardType = keyboardType;
1143 }
1144
1145 void setMetaState(int32_t metaState) {
1146 mMetaState = metaState;
1147 }
1148
1149 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001150 std::unique_lock<std::mutex> lock(mLock);
1151 base::ScopedLockAssertion assumeLocked(mLock);
1152 const bool configureCalled =
1153 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1154 return mConfigureWasCalled;
1155 });
1156 if (!configureCalled) {
1157 FAIL() << "Expected configure() to have been called.";
1158 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001159 mConfigureWasCalled = false;
1160 }
1161
1162 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001163 std::unique_lock<std::mutex> lock(mLock);
1164 base::ScopedLockAssertion assumeLocked(mLock);
1165 const bool resetCalled =
1166 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1167 return mResetWasCalled;
1168 });
1169 if (!resetCalled) {
1170 FAIL() << "Expected reset() to have been called.";
1171 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001172 mResetWasCalled = false;
1173 }
1174
Yi Kong9b14ac62018-07-17 13:48:38 -07001175 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001176 std::unique_lock<std::mutex> lock(mLock);
1177 base::ScopedLockAssertion assumeLocked(mLock);
1178 const bool processCalled =
1179 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1180 return mProcessWasCalled;
1181 });
1182 if (!processCalled) {
1183 FAIL() << "Expected process() to have been called.";
1184 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001185 if (outLastEvent) {
1186 *outLastEvent = mLastEvent;
1187 }
1188 mProcessWasCalled = false;
1189 }
1190
1191 void setKeyCodeState(int32_t keyCode, int32_t state) {
1192 mKeyCodeStates.replaceValueFor(keyCode, state);
1193 }
1194
1195 void setScanCodeState(int32_t scanCode, int32_t state) {
1196 mScanCodeStates.replaceValueFor(scanCode, state);
1197 }
1198
1199 void setSwitchState(int32_t switchCode, int32_t state) {
1200 mSwitchStates.replaceValueFor(switchCode, state);
1201 }
1202
1203 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001204 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001205 }
1206
Philip Junker4af3b3d2021-12-14 10:36:55 +01001207 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1208 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1209 }
1210
Michael Wrightd02c5b62014-02-10 15:10:22 -08001211private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001212 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001213
Chris Yea52ade12020-08-27 16:49:20 -07001214 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001215 InputMapper::populateDeviceInfo(deviceInfo);
1216
1217 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1218 deviceInfo->setKeyboardType(mKeyboardType);
1219 }
1220 }
1221
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001222 std::list<NotifyArgs> configure(nsecs_t, const InputReaderConfiguration* config,
1223 uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001224 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001225 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001226
1227 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001228 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001229 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1230 mViewport = config->getDisplayViewportByPort(*displayPort);
1231 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001232
1233 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001234 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001235 }
1236
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001237 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001238 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001239 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001240 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001241 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001242 }
1243
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001244 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001245 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001246 mLastEvent = *rawEvent;
1247 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001248 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001249 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001250 }
1251
Chris Yea52ade12020-08-27 16:49:20 -07001252 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001253 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1254 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1255 }
1256
Philip Junker4af3b3d2021-12-14 10:36:55 +01001257 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1258 auto it = mKeyCodeMapping.find(locationKeyCode);
1259 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1260 }
1261
Chris Yea52ade12020-08-27 16:49:20 -07001262 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001263 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1264 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1265 }
1266
Chris Yea52ade12020-08-27 16:49:20 -07001267 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001268 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1269 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1270 }
1271
Chris Yea52ade12020-08-27 16:49:20 -07001272 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001273 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001274 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001275 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001276 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1277 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1278 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001279 }
1280 }
1281 }
Chris Yea52ade12020-08-27 16:49:20 -07001282 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001283 return result;
1284 }
1285
1286 virtual int32_t getMetaState() {
1287 return mMetaState;
1288 }
1289
1290 virtual void fadePointer() {
1291 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001292
1293 virtual std::optional<int32_t> getAssociatedDisplay() {
1294 if (mViewport) {
1295 return std::make_optional(mViewport->displayId);
1296 }
1297 return std::nullopt;
1298 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001299};
1300
1301
1302// --- InstrumentedInputReader ---
1303
1304class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001305 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001306
1307public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001308 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1309 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001310 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001311 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001312
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001313 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001314
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001315 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001316
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001317 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001318 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001319 InputDeviceIdentifier identifier;
1320 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001321 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001322 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001323 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001324 }
1325
Prabir Pradhan28efc192019-11-05 01:10:04 +00001326 // Make the protected loopOnce method accessible to tests.
1327 using InputReader::loopOnce;
1328
Michael Wrightd02c5b62014-02-10 15:10:22 -08001329protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001330 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1331 const InputDeviceIdentifier& identifier)
1332 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001333 if (!mNextDevices.empty()) {
1334 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1335 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001336 return device;
1337 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001338 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001339 }
1340
arthurhungdcef2dc2020-08-11 14:47:50 +08001341 // --- FakeInputReaderContext ---
1342 class FakeInputReaderContext : public ContextImpl {
1343 int32_t mGlobalMetaState;
1344 bool mUpdateGlobalMetaStateWasCalled;
1345 int32_t mGeneration;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00001346 std::optional<nsecs_t> mRequestedTimeout;
1347 std::vector<InputDeviceInfo> mExternalStylusDevices;
arthurhungdcef2dc2020-08-11 14:47:50 +08001348
1349 public:
1350 FakeInputReaderContext(InputReader* reader)
1351 : ContextImpl(reader),
1352 mGlobalMetaState(0),
1353 mUpdateGlobalMetaStateWasCalled(false),
1354 mGeneration(1) {}
1355
1356 virtual ~FakeInputReaderContext() {}
1357
1358 void assertUpdateGlobalMetaStateWasCalled() {
1359 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1360 << "Expected updateGlobalMetaState() to have been called.";
1361 mUpdateGlobalMetaStateWasCalled = false;
1362 }
1363
1364 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1365
1366 uint32_t getGeneration() { return mGeneration; }
1367
1368 void updateGlobalMetaState() override {
1369 mUpdateGlobalMetaStateWasCalled = true;
1370 ContextImpl::updateGlobalMetaState();
1371 }
1372
1373 int32_t getGlobalMetaState() override {
1374 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1375 }
1376
1377 int32_t bumpGeneration() override {
1378 mGeneration = ContextImpl::bumpGeneration();
1379 return mGeneration;
1380 }
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00001381
1382 void requestTimeoutAtTime(nsecs_t when) override { mRequestedTimeout = when; }
1383
1384 void assertTimeoutWasRequested(nsecs_t when) {
1385 ASSERT_TRUE(mRequestedTimeout) << "Expected timeout at time " << when
1386 << " but there was no timeout requested.";
1387 ASSERT_EQ(when, *mRequestedTimeout);
1388 mRequestedTimeout.reset();
1389 }
1390
1391 void assertTimeoutWasNotRequested() {
1392 ASSERT_FALSE(mRequestedTimeout) << "Expected no timeout to have been requested,"
1393 " but one was requested at time "
1394 << *mRequestedTimeout;
1395 }
1396
1397 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override {
1398 outDevices = mExternalStylusDevices;
1399 }
1400
1401 void setExternalStylusDevices(std::vector<InputDeviceInfo>&& devices) {
1402 mExternalStylusDevices = devices;
1403 }
arthurhungdcef2dc2020-08-11 14:47:50 +08001404 } mFakeContext;
1405
Michael Wrightd02c5b62014-02-10 15:10:22 -08001406 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001407
1408public:
1409 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001410};
1411
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001412// --- InputReaderPolicyTest ---
1413class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001414protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001415 sp<FakeInputReaderPolicy> mFakePolicy;
1416
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001417 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001418 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001419};
1420
1421/**
1422 * Check that empty set of viewports is an acceptable configuration.
1423 * Also try to get internal viewport two different ways - by type and by uniqueId.
1424 *
1425 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1426 * Such configuration is not currently allowed.
1427 */
1428TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001429 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001430
1431 // We didn't add any viewports yet, so there shouldn't be any.
1432 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001433 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001434 ASSERT_FALSE(internalViewport);
1435
1436 // Add an internal viewport, then clear it
1437 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001438 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001439 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001440
1441 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001442 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001443 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001444 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001445
1446 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001447 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001448 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001449 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001450
1451 mFakePolicy->clearViewports();
1452 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001453 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001454 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001455 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001456 ASSERT_FALSE(internalViewport);
1457}
1458
1459TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1460 const std::string internalUniqueId = "local:0";
1461 const std::string externalUniqueId = "local:1";
1462 const std::string virtualUniqueId1 = "virtual:2";
1463 const std::string virtualUniqueId2 = "virtual:3";
1464 constexpr int32_t virtualDisplayId1 = 2;
1465 constexpr int32_t virtualDisplayId2 = 3;
1466
1467 // Add an internal viewport
1468 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001469 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1470 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001471 // Add an external viewport
1472 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001473 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1474 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001475 // Add an virtual viewport
1476 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001477 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1478 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001479 // Add another virtual viewport
1480 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001481 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1482 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001483
1484 // Check matching by type for internal
1485 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001486 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001487 ASSERT_TRUE(internalViewport);
1488 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1489
1490 // Check matching by type for external
1491 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001492 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001493 ASSERT_TRUE(externalViewport);
1494 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1495
1496 // Check matching by uniqueId for virtual viewport #1
1497 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001498 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001499 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001500 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001501 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1502 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1503
1504 // Check matching by uniqueId for virtual viewport #2
1505 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001506 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001507 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001508 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001509 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1510 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1511}
1512
1513
1514/**
1515 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1516 * that lookup works by checking display id.
1517 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1518 */
1519TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1520 const std::string uniqueId1 = "uniqueId1";
1521 const std::string uniqueId2 = "uniqueId2";
1522 constexpr int32_t displayId1 = 2;
1523 constexpr int32_t displayId2 = 3;
1524
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001525 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1526 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001527 for (const ViewportType& type : types) {
1528 mFakePolicy->clearViewports();
1529 // Add a viewport
1530 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001531 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1532 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001533 // Add another viewport
1534 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001535 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1536 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001537
1538 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001539 std::optional<DisplayViewport> viewport1 =
1540 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001541 ASSERT_TRUE(viewport1);
1542 ASSERT_EQ(displayId1, viewport1->displayId);
1543 ASSERT_EQ(type, viewport1->type);
1544
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001545 std::optional<DisplayViewport> viewport2 =
1546 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001547 ASSERT_TRUE(viewport2);
1548 ASSERT_EQ(displayId2, viewport2->displayId);
1549 ASSERT_EQ(type, viewport2->type);
1550
1551 // When there are multiple viewports of the same kind, and uniqueId is not specified
1552 // in the call to getDisplayViewport, then that situation is not supported.
1553 // The viewports can be stored in any order, so we cannot rely on the order, since that
1554 // is just implementation detail.
1555 // However, we can check that it still returns *a* viewport, we just cannot assert
1556 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001557 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001558 ASSERT_TRUE(someViewport);
1559 }
1560}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001561
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001562/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001563 * When we have multiple internal displays make sure we always return the default display when
1564 * querying by type.
1565 */
1566TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1567 const std::string uniqueId1 = "uniqueId1";
1568 const std::string uniqueId2 = "uniqueId2";
1569 constexpr int32_t nonDefaultDisplayId = 2;
1570 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1571 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1572
1573 // Add the default display first and ensure it gets returned.
1574 mFakePolicy->clearViewports();
1575 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001576 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001577 ViewportType::INTERNAL);
1578 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001579 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001580 ViewportType::INTERNAL);
1581
1582 std::optional<DisplayViewport> viewport =
1583 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1584 ASSERT_TRUE(viewport);
1585 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1586 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1587
1588 // Add the default display second to make sure order doesn't matter.
1589 mFakePolicy->clearViewports();
1590 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001591 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001592 ViewportType::INTERNAL);
1593 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001594 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001595 ViewportType::INTERNAL);
1596
1597 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1598 ASSERT_TRUE(viewport);
1599 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1600 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1601}
1602
1603/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001604 * Check getDisplayViewportByPort
1605 */
1606TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001607 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001608 const std::string uniqueId1 = "uniqueId1";
1609 const std::string uniqueId2 = "uniqueId2";
1610 constexpr int32_t displayId1 = 1;
1611 constexpr int32_t displayId2 = 2;
1612 const uint8_t hdmi1 = 0;
1613 const uint8_t hdmi2 = 1;
1614 const uint8_t hdmi3 = 2;
1615
1616 mFakePolicy->clearViewports();
1617 // Add a viewport that's associated with some display port that's not of interest.
1618 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001619 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1620 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001621 // Add another viewport, connected to HDMI1 port
1622 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001623 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1624 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001625
1626 // Check that correct display viewport was returned by comparing the display ports.
1627 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1628 ASSERT_TRUE(hdmi1Viewport);
1629 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1630 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1631
1632 // Check that we can still get the same viewport using the uniqueId
1633 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1634 ASSERT_TRUE(hdmi1Viewport);
1635 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1636 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1637 ASSERT_EQ(type, hdmi1Viewport->type);
1638
1639 // Check that we cannot find a port with "HDMI2", because we never added one
1640 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1641 ASSERT_FALSE(hdmi2Viewport);
1642}
1643
Michael Wrightd02c5b62014-02-10 15:10:22 -08001644// --- InputReaderTest ---
1645
1646class InputReaderTest : public testing::Test {
1647protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001648 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001649 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001650 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001651 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001652
Chris Yea52ade12020-08-27 16:49:20 -07001653 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001654 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001655 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001656 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001657
Prabir Pradhan28efc192019-11-05 01:10:04 +00001658 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001659 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001660 }
1661
Chris Yea52ade12020-08-27 16:49:20 -07001662 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001663 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001664 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001665 }
1666
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001667 void addDevice(int32_t eventHubId, const std::string& name,
1668 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001669 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001670
1671 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001672 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001673 }
1674 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001675 mReader->loopOnce();
1676 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001677 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1678 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001679 }
1680
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001681 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001682 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001683 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001684 }
1685
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001686 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001687 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001688 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001689 }
1690
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001691 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001692 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001693 ftl::Flags<InputDeviceClass> classes,
1694 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001695 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001696 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1697 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001698 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001699 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001700 return mapper;
1701 }
1702};
1703
Chris Ye98d3f532020-10-01 21:48:59 -07001704TEST_F(InputReaderTest, PolicyGetInputDevices) {
1705 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001706 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001707 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001708
1709 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001710 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001711 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001712 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001713 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001714 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1715 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001716 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001717}
1718
Chris Yee7310032020-09-22 15:36:28 -07001719TEST_F(InputReaderTest, GetMergedInputDevices) {
1720 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1721 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1722 // Add two subdevices to device
1723 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1724 // Must add at least one mapper or the device will be ignored!
1725 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1726 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1727
1728 // Push same device instance for next device to be added, so they'll have same identifier.
1729 mReader->pushNextDevice(device);
1730 mReader->pushNextDevice(device);
1731 ASSERT_NO_FATAL_FAILURE(
1732 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1733 ASSERT_NO_FATAL_FAILURE(
1734 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1735
1736 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001737 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001738}
1739
Chris Yee14523a2020-12-19 13:46:00 -08001740TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1741 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1742 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1743 // Add two subdevices to device
1744 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1745 // Must add at least one mapper or the device will be ignored!
1746 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1747 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1748
1749 // Push same device instance for next device to be added, so they'll have same identifier.
1750 mReader->pushNextDevice(device);
1751 mReader->pushNextDevice(device);
1752 // Sensor device is initially disabled
1753 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1754 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1755 nullptr));
1756 // Device is disabled because the only sub device is a sensor device and disabled initially.
1757 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1758 ASSERT_FALSE(device->isEnabled());
1759 ASSERT_NO_FATAL_FAILURE(
1760 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1761 // The merged device is enabled if any sub device is enabled
1762 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1763 ASSERT_TRUE(device->isEnabled());
1764}
1765
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001766TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001767 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001768 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001769 constexpr int32_t eventHubId = 1;
1770 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001771 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001772 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001773 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001774 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001775
Yi Kong9b14ac62018-07-17 13:48:38 -07001776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001777
1778 NotifyDeviceResetArgs resetArgs;
1779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001780 ASSERT_EQ(deviceId, resetArgs.deviceId);
1781
1782 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001783 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001784 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001785
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001787 ASSERT_EQ(deviceId, resetArgs.deviceId);
1788 ASSERT_EQ(device->isEnabled(), false);
1789
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001790 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001791 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001794 ASSERT_EQ(device->isEnabled(), false);
1795
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001796 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001797 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001799 ASSERT_EQ(deviceId, resetArgs.deviceId);
1800 ASSERT_EQ(device->isEnabled(), true);
1801}
1802
Michael Wrightd02c5b62014-02-10 15:10:22 -08001803TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001804 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001805 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001806 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001807 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001808 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001809 AINPUT_SOURCE_KEYBOARD, nullptr);
1810 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001811
1812 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1813 AINPUT_SOURCE_ANY, AKEYCODE_A))
1814 << "Should return unknown when the device id is >= 0 but unknown.";
1815
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001816 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1817 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1818 << "Should return unknown when the device id is valid but the sources are not "
1819 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001820
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001821 ASSERT_EQ(AKEY_STATE_DOWN,
1822 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1823 AKEYCODE_A))
1824 << "Should return value provided by mapper when device id is valid and the device "
1825 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001826
1827 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1828 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1829 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1830
1831 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1832 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1833 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1834}
1835
Philip Junker4af3b3d2021-12-14 10:36:55 +01001836TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1837 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1838 constexpr int32_t eventHubId = 1;
1839 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1840 InputDeviceClass::KEYBOARD,
1841 AINPUT_SOURCE_KEYBOARD, nullptr);
1842 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1843
1844 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1845 << "Should return unknown when the device with the specified id is not found.";
1846
1847 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1848 << "Should return correct mapping when device id is valid and mapping exists.";
1849
1850 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1851 << "Should return the location key code when device id is valid and there's no "
1852 "mapping.";
1853}
1854
1855TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1856 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1857 constexpr int32_t eventHubId = 1;
1858 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1859 InputDeviceClass::JOYSTICK,
1860 AINPUT_SOURCE_GAMEPAD, nullptr);
1861 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1862
1863 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1864 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1865}
1866
Michael Wrightd02c5b62014-02-10 15:10:22 -08001867TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001868 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001869 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001870 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001871 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001872 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001873 AINPUT_SOURCE_KEYBOARD, nullptr);
1874 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001875
1876 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1877 AINPUT_SOURCE_ANY, KEY_A))
1878 << "Should return unknown when the device id is >= 0 but unknown.";
1879
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001880 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1881 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1882 << "Should return unknown when the device id is valid but the sources are not "
1883 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001884
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001885 ASSERT_EQ(AKEY_STATE_DOWN,
1886 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1887 KEY_A))
1888 << "Should return value provided by mapper when device id is valid and the device "
1889 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001890
1891 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1892 AINPUT_SOURCE_TRACKBALL, KEY_A))
1893 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1894
1895 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1896 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1897 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1898}
1899
1900TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001901 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001902 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001903 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001904 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001905 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001906 AINPUT_SOURCE_KEYBOARD, nullptr);
1907 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001908
1909 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1910 AINPUT_SOURCE_ANY, SW_LID))
1911 << "Should return unknown when the device id is >= 0 but unknown.";
1912
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001913 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1914 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1915 << "Should return unknown when the device id is valid but the sources are not "
1916 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001917
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001918 ASSERT_EQ(AKEY_STATE_DOWN,
1919 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1920 SW_LID))
1921 << "Should return value provided by mapper when device id is valid and the device "
1922 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001923
1924 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1925 AINPUT_SOURCE_TRACKBALL, SW_LID))
1926 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1927
1928 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1929 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1930 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1931}
1932
1933TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001934 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001935 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001936 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001937 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001938 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001939 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001940
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001941 mapper.addSupportedKeyCode(AKEYCODE_A);
1942 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001943
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001944 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001945 uint8_t flags[4] = { 0, 0, 0, 1 };
1946
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001947 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001948 << "Should return false when device id is >= 0 but unknown.";
1949 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1950
1951 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001952 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001953 << "Should return false when device id is valid but the sources are not supported by "
1954 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001955 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1956
1957 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001958 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001959 keyCodes, flags))
1960 << "Should return value provided by mapper when device id is valid and the device "
1961 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001962 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1963
1964 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001965 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1966 << "Should return false when the device id is < 0 but the sources are not supported by "
1967 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001968 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1969
1970 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001971 ASSERT_TRUE(
1972 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1973 << "Should return value provided by mapper when device id is < 0 and one of the "
1974 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001975 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1976}
1977
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001978TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001979 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001980 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001981
1982 NotifyConfigurationChangedArgs args;
1983
1984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1985 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1986}
1987
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001988TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001989 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001990 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001991 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001992 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001993 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001994 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001995 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001996 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001997
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001998 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001999 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002000 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
2001
2002 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002003 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002004 ASSERT_EQ(when, event.when);
2005 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002006 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002007 ASSERT_EQ(EV_KEY, event.type);
2008 ASSERT_EQ(KEY_A, event.code);
2009 ASSERT_EQ(1, event.value);
2010}
2011
Garfield Tan1c7bc862020-01-28 13:24:04 -08002012TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002013 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002014 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002015 constexpr int32_t eventHubId = 1;
2016 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08002017 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002018 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002019 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002020 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08002021
2022 NotifyDeviceResetArgs resetArgs;
2023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002024 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002025
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002026 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002027 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002029 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002030 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002031
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002032 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002033 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002035 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002036 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002037
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002038 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002039 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002041 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002042 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002043}
2044
Garfield Tan1c7bc862020-01-28 13:24:04 -08002045TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
2046 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002047 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002048 constexpr int32_t eventHubId = 1;
2049 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2050 // Must add at least one mapper or the device will be ignored!
2051 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002052 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002053 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
2054
2055 NotifyDeviceResetArgs resetArgs;
2056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2057 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
2058}
2059
Arthur Hungc23540e2018-11-29 20:42:11 +08002060TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002061 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002062 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002063 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08002064 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002065 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2066 FakeInputMapper& mapper =
2067 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002068 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002069
2070 const uint8_t hdmi1 = 1;
2071
2072 // Associated touch screen with second display.
2073 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2074
2075 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002076 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002077 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002078 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002079 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002080 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002081 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002082 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002083 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002084 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002085
2086 // Add the device, and make sure all of the callbacks are triggered.
2087 // The device is added after the input port associations are processed since
2088 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002089 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002092 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002093
Arthur Hung2c9a3342019-07-23 14:18:59 +08002094 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002095 ASSERT_EQ(deviceId, device->getId());
2096 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2097 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002098
2099 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002100 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002101 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002102 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002103}
2104
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002105TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2106 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002107 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002108 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2109 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2110 // Must add at least one mapper or the device will be ignored!
2111 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2112 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2113 mReader->pushNextDevice(device);
2114 mReader->pushNextDevice(device);
2115 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2116 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2117
2118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2119
2120 NotifyDeviceResetArgs resetArgs;
2121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2122 ASSERT_EQ(deviceId, resetArgs.deviceId);
2123 ASSERT_TRUE(device->isEnabled());
2124 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2125 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2126
2127 disableDevice(deviceId);
2128 mReader->loopOnce();
2129
2130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2131 ASSERT_EQ(deviceId, resetArgs.deviceId);
2132 ASSERT_FALSE(device->isEnabled());
2133 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2134 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2135
2136 enableDevice(deviceId);
2137 mReader->loopOnce();
2138
2139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2140 ASSERT_EQ(deviceId, resetArgs.deviceId);
2141 ASSERT_TRUE(device->isEnabled());
2142 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2143 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2144}
2145
2146TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2147 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002148 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002149 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2150 // Add two subdevices to device
2151 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2152 FakeInputMapper& mapperDevice1 =
2153 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2154 FakeInputMapper& mapperDevice2 =
2155 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2156 mReader->pushNextDevice(device);
2157 mReader->pushNextDevice(device);
2158 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2159 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2160
2161 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2162 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2163
2164 ASSERT_EQ(AKEY_STATE_DOWN,
2165 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2166 ASSERT_EQ(AKEY_STATE_DOWN,
2167 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2168 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2169 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2170}
2171
Prabir Pradhan7e186182020-11-10 13:56:45 -08002172TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2173 NotifyPointerCaptureChangedArgs args;
2174
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002175 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002176 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2177 mReader->loopOnce();
2178 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002179 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2180 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002181
2182 mFakePolicy->setPointerCapture(false);
2183 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2184 mReader->loopOnce();
2185 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002186 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002187
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002188 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002189 // does not change.
2190 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2191 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002192 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002193}
2194
Chris Ye87143712020-11-10 05:05:58 +00002195class FakeVibratorInputMapper : public FakeInputMapper {
2196public:
2197 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2198 : FakeInputMapper(deviceContext, sources) {}
2199
2200 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2201};
2202
2203TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2204 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002205 ftl::Flags<InputDeviceClass> deviceClass =
2206 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002207 constexpr int32_t eventHubId = 1;
2208 const char* DEVICE_LOCATION = "BLUETOOTH";
2209 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2210 FakeVibratorInputMapper& mapper =
2211 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2212 mReader->pushNextDevice(device);
2213
2214 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2215 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2216
2217 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2218 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2219}
2220
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002221// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002222
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002223class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002224public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002225 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002226
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002227 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002228
Andy Chenf9f1a022022-08-29 20:07:10 -04002229 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2230
Chris Yee2b1e5c2021-03-10 22:45:12 -08002231 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2232
2233 void dump(std::string& dump) override {}
2234
2235 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2236 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002237 }
2238
Chris Yee2b1e5c2021-03-10 22:45:12 -08002239 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2240 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002241 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002242
2243 bool setLightColor(int32_t lightId, int32_t color) override {
2244 getDeviceContext().setLightBrightness(lightId, color >> 24);
2245 return true;
2246 }
2247
2248 std::optional<int32_t> getLightColor(int32_t lightId) override {
2249 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2250 if (!result.has_value()) {
2251 return std::nullopt;
2252 }
2253 return result.value() << 24;
2254 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002255
2256 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2257
2258 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2259
2260private:
2261 InputDeviceContext& mDeviceContext;
2262 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2263 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04002264 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002265};
2266
Chris Yee2b1e5c2021-03-10 22:45:12 -08002267TEST_F(InputReaderTest, BatteryGetCapacity) {
2268 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002269 ftl::Flags<InputDeviceClass> deviceClass =
2270 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002271 constexpr int32_t eventHubId = 1;
2272 const char* DEVICE_LOCATION = "BLUETOOTH";
2273 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002274 FakePeripheralController& controller =
2275 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002276 mReader->pushNextDevice(device);
2277
2278 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2279
2280 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2281 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2282}
2283
2284TEST_F(InputReaderTest, BatteryGetStatus) {
2285 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002286 ftl::Flags<InputDeviceClass> deviceClass =
2287 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002288 constexpr int32_t eventHubId = 1;
2289 const char* DEVICE_LOCATION = "BLUETOOTH";
2290 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002291 FakePeripheralController& controller =
2292 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002293 mReader->pushNextDevice(device);
2294
2295 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2296
2297 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2298 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2299}
2300
Prabir Pradhane287ecd2022-09-07 21:18:05 +00002301TEST_F(InputReaderTest, BatteryGetDevicePath) {
2302 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2303 ftl::Flags<InputDeviceClass> deviceClass =
2304 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2305 constexpr int32_t eventHubId = 1;
2306 const char* DEVICE_LOCATION = "BLUETOOTH";
2307 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2308 device->addController<FakePeripheralController>(eventHubId);
2309 mReader->pushNextDevice(device);
2310
2311 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2312
2313 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), BATTERY_DEVPATH);
2314}
2315
Chris Ye3fdbfef2021-01-06 18:45:18 -08002316TEST_F(InputReaderTest, LightGetColor) {
2317 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002318 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002319 constexpr int32_t eventHubId = 1;
2320 const char* DEVICE_LOCATION = "BLUETOOTH";
2321 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002322 FakePeripheralController& controller =
2323 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002324 mReader->pushNextDevice(device);
2325 RawLightInfo info = {.id = 1,
2326 .name = "Mono",
2327 .maxBrightness = 255,
2328 .flags = InputLightClass::BRIGHTNESS,
2329 .path = ""};
2330 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2331 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2332
2333 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002334
Chris Yee2b1e5c2021-03-10 22:45:12 -08002335 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2336 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002337 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2338 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2339}
2340
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002341// --- InputReaderIntegrationTest ---
2342
2343// These tests create and interact with the InputReader only through its interface.
2344// The InputReader is started during SetUp(), which starts its processing in its own
2345// thread. The tests use linux uinput to emulate input devices.
2346// NOTE: Interacting with the physical device while these tests are running may cause
2347// the tests to fail.
2348class InputReaderIntegrationTest : public testing::Test {
2349protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002350 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002351 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002352 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002353
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002354 std::shared_ptr<FakePointerController> mFakePointerController;
2355
Chris Yea52ade12020-08-27 16:49:20 -07002356 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002357#if !defined(__ANDROID__)
2358 GTEST_SKIP();
2359#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002360 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002361 mFakePointerController = std::make_shared<FakePointerController>();
2362 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002363 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2364 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002365
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002366 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2367 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002368 ASSERT_EQ(mReader->start(), OK);
2369
2370 // Since this test is run on a real device, all the input devices connected
2371 // to the test device will show up in mReader. We wait for those input devices to
2372 // show up before beginning the tests.
2373 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2374 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2375 }
2376
Chris Yea52ade12020-08-27 16:49:20 -07002377 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002378#if !defined(__ANDROID__)
2379 return;
2380#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002381 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002382 mReader.reset();
2383 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002384 mFakePolicy.clear();
2385 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00002386
2387 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
2388 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
2389 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
2390 [&name](const InputDeviceInfo& info) {
2391 return info.getIdentifier().name == name;
2392 });
2393 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
2394 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002395};
2396
2397TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2398 // An invalid input device that is only used for this test.
2399 class InvalidUinputDevice : public UinputDevice {
2400 public:
Prabir Pradhanb7d434e2022-10-14 22:41:38 +00002401 InvalidUinputDevice() : UinputDevice("Invalid Device", 99 /*productId*/) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002402
2403 private:
2404 void configureDevice(int fd, uinput_user_dev* device) override {}
2405 };
2406
2407 const size_t numDevices = mFakePolicy->getInputDevices().size();
2408
2409 // UinputDevice does not set any event or key bits, so InputReader should not
2410 // consider it as a valid device.
2411 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2412 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2413 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2414 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2415
2416 invalidDevice.reset();
2417 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2418 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2419 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2420}
2421
2422TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2423 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2424
2425 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2426 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2427 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2428 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2429
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002430 const auto device = findDeviceByName(keyboard->getName());
2431 ASSERT_TRUE(device.has_value());
2432 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
2433 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
2434 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002435
2436 keyboard.reset();
2437 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2438 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2439 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2440}
2441
2442TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2443 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2444 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2445
2446 NotifyConfigurationChangedArgs configChangedArgs;
2447 ASSERT_NO_FATAL_FAILURE(
2448 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002449 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002450 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2451
2452 NotifyKeyArgs keyArgs;
2453 keyboard->pressAndReleaseHomeKey();
2454 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2455 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002456 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002457 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002458 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002459 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002460 prevTimestamp = keyArgs.eventTime;
2461
2462 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2463 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002464 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002465 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002466 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002467}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002468
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002469TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
2470 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2471 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2472
2473 const auto device = findDeviceByName(stylus->getName());
2474 ASSERT_TRUE(device.has_value());
2475
Prabir Pradhana3621852022-10-14 18:57:23 +00002476 // An external stylus with buttons should also be recognized as a keyboard.
2477 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002478 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
2479 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
2480
2481 const auto DOWN =
2482 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
2483 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
2484
2485 stylus->pressAndReleaseKey(BTN_STYLUS);
2486 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2487 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2488 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2489 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2490
2491 stylus->pressAndReleaseKey(BTN_STYLUS2);
2492 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2493 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
2494 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2495 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
2496
2497 stylus->pressAndReleaseKey(BTN_STYLUS3);
2498 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2499 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
2500 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2501 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
2502}
2503
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002504/**
2505 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2506 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2507 * are passed to the listener.
2508 */
2509static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2510TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2511 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2512 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2513 NotifyKeyArgs keyArgs;
2514
2515 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2516 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2517 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2518 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2519
2520 controller->pressAndReleaseKey(BTN_GEAR_UP);
2521 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2522 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2523 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2524}
2525
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002526// --- TouchIntegrationTest ---
2527
Arthur Hungaab25622020-01-16 11:22:11 +08002528class TouchIntegrationTest : public InputReaderIntegrationTest {
2529protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002530 const std::string UNIQUE_ID = "local:0";
2531
Chris Yea52ade12020-08-27 16:49:20 -07002532 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002533#if !defined(__ANDROID__)
2534 GTEST_SKIP();
2535#endif
Arthur Hungaab25622020-01-16 11:22:11 +08002536 InputReaderIntegrationTest::SetUp();
2537 // At least add an internal display.
2538 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2539 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002540 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002541
2542 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2543 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2544 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00002545 const auto info = findDeviceByName(mDevice->getName());
2546 ASSERT_TRUE(info);
2547 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08002548 }
2549
2550 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2551 int32_t orientation, const std::string& uniqueId,
2552 std::optional<uint8_t> physicalPort,
2553 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002554 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2555 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002556 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2557 }
2558
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002559 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2560 NotifyMotionArgs args;
2561 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2562 EXPECT_EQ(action, args.action);
2563 ASSERT_EQ(points.size(), args.pointerCount);
2564 for (size_t i = 0; i < args.pointerCount; i++) {
2565 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2566 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2567 }
2568 }
2569
Arthur Hungaab25622020-01-16 11:22:11 +08002570 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00002571 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08002572};
2573
2574TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2575 NotifyMotionArgs args;
2576 const Point centerPoint = mDevice->getCenterPoint();
2577
2578 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002579 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002580 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002581 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002582 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2583 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2584
2585 // ACTION_MOVE
2586 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002587 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002588 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2589 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2590
2591 // ACTION_UP
2592 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002593 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002594 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2595 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2596}
2597
2598TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2599 NotifyMotionArgs args;
2600 const Point centerPoint = mDevice->getCenterPoint();
2601
2602 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002603 mDevice->sendSlot(FIRST_SLOT);
2604 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002605 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002606 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002607 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2608 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2609
2610 // ACTION_POINTER_DOWN (Second slot)
2611 const Point secondPoint = centerPoint + Point(100, 100);
2612 mDevice->sendSlot(SECOND_SLOT);
2613 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002614 mDevice->sendDown(secondPoint);
2615 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002616 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002617 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002618
2619 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002620 mDevice->sendMove(secondPoint + Point(1, 1));
2621 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002622 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2624
2625 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002626 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002627 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002628 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002629 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002630
2631 // ACTION_UP
2632 mDevice->sendSlot(FIRST_SLOT);
2633 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002634 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002635 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2636 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2637}
2638
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002639/**
2640 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2641 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2642 * data?
2643 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2644 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2645 * for Pointer 0 only is generated after.
2646 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2647 * events, we will not miss any information.
2648 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2649 * event generated afterwards that contains the newest movement of pointer 0.
2650 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2651 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2652 * losing information about non-palm pointers.
2653 */
2654TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2655 NotifyMotionArgs args;
2656 const Point centerPoint = mDevice->getCenterPoint();
2657
2658 // ACTION_DOWN
2659 mDevice->sendSlot(FIRST_SLOT);
2660 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2661 mDevice->sendDown(centerPoint);
2662 mDevice->sendSync();
2663 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2664
2665 // ACTION_POINTER_DOWN (Second slot)
2666 const Point secondPoint = centerPoint + Point(100, 100);
2667 mDevice->sendSlot(SECOND_SLOT);
2668 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2669 mDevice->sendDown(secondPoint);
2670 mDevice->sendSync();
2671 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2672
2673 // ACTION_MOVE (First slot)
2674 mDevice->sendSlot(FIRST_SLOT);
2675 mDevice->sendMove(centerPoint + Point(5, 5));
2676 // ACTION_POINTER_UP (Second slot)
2677 mDevice->sendSlot(SECOND_SLOT);
2678 mDevice->sendPointerUp();
2679 // Send a single sync for the above 2 pointer updates
2680 mDevice->sendSync();
2681
2682 // First, we should get POINTER_UP for the second pointer
2683 assertReceivedMotion(ACTION_POINTER_1_UP,
2684 {/*first pointer */ centerPoint + Point(5, 5),
2685 /*second pointer*/ secondPoint});
2686
2687 // Next, the MOVE event for the first pointer
2688 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2689}
2690
2691/**
2692 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2693 * move, and then it will go up, all in the same frame.
2694 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2695 * gets sent to the listener.
2696 */
2697TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2698 NotifyMotionArgs args;
2699 const Point centerPoint = mDevice->getCenterPoint();
2700
2701 // ACTION_DOWN
2702 mDevice->sendSlot(FIRST_SLOT);
2703 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2704 mDevice->sendDown(centerPoint);
2705 mDevice->sendSync();
2706 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2707
2708 // ACTION_POINTER_DOWN (Second slot)
2709 const Point secondPoint = centerPoint + Point(100, 100);
2710 mDevice->sendSlot(SECOND_SLOT);
2711 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2712 mDevice->sendDown(secondPoint);
2713 mDevice->sendSync();
2714 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2715
2716 // ACTION_MOVE (First slot)
2717 mDevice->sendSlot(FIRST_SLOT);
2718 mDevice->sendMove(centerPoint + Point(5, 5));
2719 // ACTION_POINTER_UP (Second slot)
2720 mDevice->sendSlot(SECOND_SLOT);
2721 mDevice->sendMove(secondPoint + Point(6, 6));
2722 mDevice->sendPointerUp();
2723 // Send a single sync for the above 2 pointer updates
2724 mDevice->sendSync();
2725
2726 // First, we should get POINTER_UP for the second pointer
2727 // The movement of the second pointer during the liftoff frame is ignored.
2728 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2729 assertReceivedMotion(ACTION_POINTER_1_UP,
2730 {/*first pointer */ centerPoint + Point(5, 5),
2731 /*second pointer*/ secondPoint});
2732
2733 // Next, the MOVE event for the first pointer
2734 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2735}
2736
Arthur Hungaab25622020-01-16 11:22:11 +08002737TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2738 NotifyMotionArgs args;
2739 const Point centerPoint = mDevice->getCenterPoint();
2740
2741 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002742 mDevice->sendSlot(FIRST_SLOT);
2743 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002744 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002745 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002746 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2747 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2748
arthurhungcc7f9802020-04-30 17:55:40 +08002749 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002750 const Point secondPoint = centerPoint + Point(100, 100);
2751 mDevice->sendSlot(SECOND_SLOT);
2752 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2753 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002754 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002755 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002756 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002757
arthurhungcc7f9802020-04-30 17:55:40 +08002758 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002759 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002760 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002761 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2762 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2763
arthurhungcc7f9802020-04-30 17:55:40 +08002764 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2765 // a palm event.
2766 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002767 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002768 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002769 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002770 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002771 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002772
arthurhungcc7f9802020-04-30 17:55:40 +08002773 // Send up to second slot, expect first slot send moving.
2774 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002775 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002776 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2777 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002778
arthurhungcc7f9802020-04-30 17:55:40 +08002779 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002780 mDevice->sendSlot(FIRST_SLOT);
2781 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002782 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002783
arthurhungcc7f9802020-04-30 17:55:40 +08002784 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2785 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002786}
2787
Prabir Pradhanda20b172022-09-26 17:01:18 +00002788TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
2789 const Point centerPoint = mDevice->getCenterPoint();
2790
2791 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
2792 mDevice->sendSlot(FIRST_SLOT);
2793 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2794 mDevice->sendToolType(MT_TOOL_PEN);
2795 mDevice->sendDown(centerPoint);
2796 mDevice->sendSync();
2797 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2798 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2799 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2800
2801 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2802
2803 // Release the stylus touch.
2804 mDevice->sendUp();
2805 mDevice->sendSync();
2806 ASSERT_NO_FATAL_FAILURE(
2807 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2808
2809 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2810
2811 // Touch down with the finger, without the pen tool selected. The policy is not notified.
2812 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2813 mDevice->sendToolType(MT_TOOL_FINGER);
2814 mDevice->sendDown(centerPoint);
2815 mDevice->sendSync();
2816 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2817 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2818 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2819
2820 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2821
2822 mDevice->sendUp();
2823 mDevice->sendSync();
2824 ASSERT_NO_FATAL_FAILURE(
2825 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2826
2827 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
2828 // The policy should be notified of the stylus presence.
2829 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2830 mDevice->sendToolType(MT_TOOL_PEN);
2831 mDevice->sendMove(centerPoint);
2832 mDevice->sendSync();
2833 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2834 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2835 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2836
2837 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2838}
2839
Prabir Pradhan124ea442022-10-28 20:27:44 +00002840// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002841
Prabir Pradhan124ea442022-10-28 20:27:44 +00002842// Verify the behavior of button presses reported by various kinds of styluses, including buttons
2843// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
2844// stylus.
2845template <typename UinputStylusDevice>
2846class StylusButtonIntegrationTest : public TouchIntegrationTest {
2847protected:
2848 void SetUp() override {
2849#if !defined(__ANDROID__)
2850 GTEST_SKIP();
2851#endif
2852 TouchIntegrationTest::SetUp();
2853 mTouchscreen = mDevice.get();
2854 mTouchscreenInfo = mDeviceInfo;
2855
2856 setUpStylusDevice();
2857 }
2858
2859 UinputStylusDevice* mStylus{nullptr};
2860 InputDeviceInfo mStylusInfo{};
2861
2862 UinputTouchScreen* mTouchscreen{nullptr};
2863 InputDeviceInfo mTouchscreenInfo{};
2864
2865private:
2866 // When we are attempting to test stylus button events that are sent from the touchscreen,
2867 // use the same Uinput device for the touchscreen and the stylus.
2868 template <typename T = UinputStylusDevice>
2869 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2870 mStylus = mDevice.get();
2871 mStylusInfo = mDeviceInfo;
2872 }
2873
2874 // When we are attempting to stylus buttons from an external stylus being merged with touches
2875 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2876 template <typename T = UinputStylusDevice>
2877 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2878 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2879 mStylus = mStylusDeviceLifecycleTracker.get();
2880 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2881 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2882 const auto info = findDeviceByName(mStylus->getName());
2883 ASSERT_TRUE(info);
2884 mStylusInfo = *info;
2885 }
2886
2887 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2888
2889 // Hide the base class's device to expose it with a different name for readability.
2890 using TouchIntegrationTest::mDevice;
2891 using TouchIntegrationTest::mDeviceInfo;
2892};
2893
2894using StylusButtonIntegrationTestTypes =
2895 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2896TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2897
2898TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
2899 const auto stylusId = TestFixture::mStylusInfo.getId();
2900
2901 TestFixture::mStylus->pressKey(BTN_STYLUS);
2902 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2903 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2904 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2905
2906 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2907 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002908 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002909 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002910}
2911
Prabir Pradhan124ea442022-10-28 20:27:44 +00002912TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
2913 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2914 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2915 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002916
2917 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002918 TestFixture::mStylus->pressKey(BTN_STYLUS);
2919 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002920 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002921 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002922
2923 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002924 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2925 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2926 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2927 TestFixture::mTouchscreen->sendDown(centerPoint);
2928 TestFixture::mTouchscreen->sendSync();
2929 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002930 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2931 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002932 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2933 WithDeviceId(touchscreenId))));
2934 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002935 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2936 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002937 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2938 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002939
Prabir Pradhan124ea442022-10-28 20:27:44 +00002940 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2941 TestFixture::mTouchscreen->sendSync();
2942 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002943 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002944 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2945 WithDeviceId(touchscreenId))));
2946 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002947 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002948 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2949 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002950
2951 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002952 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2953 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002954 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002955 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002956}
2957
Prabir Pradhan124ea442022-10-28 20:27:44 +00002958TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
2959 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2960 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2961 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002962
2963 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002964 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2965 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2966 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2967 TestFixture::mTouchscreen->sendDown(centerPoint);
2968 TestFixture::mTouchscreen->sendSync();
2969 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002970 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002971 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2972 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002973
2974 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002975 TestFixture::mStylus->pressKey(BTN_STYLUS);
2976 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002977 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002978 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2979 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002980 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2981 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002982 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2983 WithDeviceId(touchscreenId))));
2984 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002985 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2986 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002987 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2988 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002989
Prabir Pradhan124ea442022-10-28 20:27:44 +00002990 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2991 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002992 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002993 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2994 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002995 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002996 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2997 WithDeviceId(touchscreenId))));
2998 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002999 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003000 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3001 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003002
3003 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00003004 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
3005 TestFixture::mTouchscreen->sendSync();
3006 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003007 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003008 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3009 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003010}
3011
Prabir Pradhan484d55a2022-10-14 23:17:16 +00003012// --- ExternalStylusIntegrationTest ---
3013
3014// Verify the behavior of an external stylus. An external stylus can report pressure or button
3015// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
3016// ongoing stylus gesture that is being emitted by the touchscreen.
3017using ExternalStylusIntegrationTest = TouchIntegrationTest;
3018
3019TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
3020 const Point centerPoint = mDevice->getCenterPoint();
3021
3022 // Create an external stylus capable of reporting pressure data that
3023 // should be fused with a touch pointer.
3024 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
3025 createUinputDevice<UinputExternalStylusWithPressure>();
3026 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
3027 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
3028 const auto stylusInfo = findDeviceByName(stylus->getName());
3029 ASSERT_TRUE(stylusInfo);
3030
3031 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
3032
3033 const auto touchscreenId = mDeviceInfo.getId();
3034
3035 // Set a pressure value on the stylus. It doesn't generate any events.
3036 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
3037 stylus->setPressure(100);
3038 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3039
3040 // Start a finger gesture, and ensure it shows up as stylus gesture
3041 // with the pressure set by the external stylus.
3042 mDevice->sendSlot(FIRST_SLOT);
3043 mDevice->sendTrackingId(FIRST_TRACKING_ID);
3044 mDevice->sendToolType(MT_TOOL_FINGER);
3045 mDevice->sendDown(centerPoint);
3046 mDevice->sendSync();
3047 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3048 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3049 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3050 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
3051
3052 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
3053 // event with the updated pressure.
3054 stylus->setPressure(200);
3055 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3056 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
3057 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3058 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
3059
3060 // The external stylus did not generate any events.
3061 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3062 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
3063}
3064
3065TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
3066 const Point centerPoint = mDevice->getCenterPoint();
3067
3068 // Create an external stylus capable of reporting pressure data that
3069 // should be fused with a touch pointer.
3070 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
3071 createUinputDevice<UinputExternalStylusWithPressure>();
3072 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
3073 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
3074 const auto stylusInfo = findDeviceByName(stylus->getName());
3075 ASSERT_TRUE(stylusInfo);
3076
3077 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
3078
3079 const auto touchscreenId = mDeviceInfo.getId();
3080
3081 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
3082 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
3083 stylus->setPressure(0);
3084
3085 // Start a finger gesture. The touch device will withhold generating any touches for
3086 // up to 72 milliseconds while waiting for pressure data from the external stylus.
3087 mDevice->sendSlot(FIRST_SLOT);
3088 mDevice->sendTrackingId(FIRST_TRACKING_ID);
3089 mDevice->sendToolType(MT_TOOL_FINGER);
3090 mDevice->sendDown(centerPoint);
3091 auto waitUntil = std::chrono::system_clock::now() +
3092 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
3093 mDevice->sendSync();
3094 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
3095
3096 // Since the external stylus did not report a pressure value within the timeout,
3097 // it shows up as a finger pointer.
3098 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3099 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3100 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithDeviceId(touchscreenId),
3101 WithPressure(1.f))));
3102
3103 // Change the pressure on the external stylus. Since the pressure was not present at the start
3104 // of the gesture, it is ignored for now.
3105 stylus->setPressure(200);
3106 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3107
3108 // Finish the finger gesture.
3109 mDevice->sendTrackingId(INVALID_TRACKING_ID);
3110 mDevice->sendSync();
3111 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3112 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3113 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
3114
3115 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
3116 mDevice->sendTrackingId(FIRST_TRACKING_ID);
3117 mDevice->sendToolType(MT_TOOL_FINGER);
3118 mDevice->sendDown(centerPoint);
3119 mDevice->sendSync();
3120 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3121 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3122 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3123 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
3124
3125 // The external stylus did not generate any events.
3126 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3127 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
3128}
3129
Michael Wrightd02c5b62014-02-10 15:10:22 -08003130// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08003131class InputDeviceTest : public testing::Test {
3132protected:
3133 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08003134 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003135 static const int32_t DEVICE_ID;
3136 static const int32_t DEVICE_GENERATION;
3137 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003138 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003139 static const int32_t EVENTHUB_ID;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003140 static const std::string DEVICE_BLUETOOTH_ADDRESS;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003141
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003142 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003143 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003144 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08003145 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00003146 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003147
Chris Yea52ade12020-08-27 16:49:20 -07003148 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003149 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003150 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003151 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08003152 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003153 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003154 InputDeviceIdentifier identifier;
3155 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08003156 identifier.location = DEVICE_LOCATION;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003157 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
arthurhungdcef2dc2020-08-11 14:47:50 +08003158 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003159 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08003160 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003161 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003162 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003163 }
3164
Chris Yea52ade12020-08-27 16:49:20 -07003165 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003166 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003167 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003168 }
3169};
3170
3171const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003172const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003173const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003174const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
3175const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003176const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07003177 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003178const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003179const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
Michael Wrightd02c5b62014-02-10 15:10:22 -08003180
3181TEST_F(InputDeviceTest, ImmutableProperties) {
3182 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003183 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003184 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003185}
3186
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00003187TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
3188 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
3189
3190 // Configuration
3191 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
3192 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003193 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00003194
3195 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
3196}
3197
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003198TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
3199 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07003200}
3201
Michael Wrightd02c5b62014-02-10 15:10:22 -08003202TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
3203 // Configuration.
3204 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003205 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003206
3207 // Reset.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003208 unused += mDevice->reset(ARBITRARY_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003209
3210 NotifyDeviceResetArgs resetArgs;
3211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3212 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3213 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3214
3215 // Metadata.
3216 ASSERT_TRUE(mDevice->isIgnored());
3217 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
3218
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00003219 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003220 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003221 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003222 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
3223 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
3224
3225 // State queries.
3226 ASSERT_EQ(0, mDevice->getMetaState());
3227
3228 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
3229 << "Ignored device should return unknown key code state.";
3230 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
3231 << "Ignored device should return unknown scan code state.";
3232 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
3233 << "Ignored device should return unknown switch state.";
3234
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003235 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08003236 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003237 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003238 << "Ignored device should never mark any key codes.";
3239 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
3240 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
3241}
3242
3243TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
3244 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003245 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08003246
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003247 FakeInputMapper& mapper1 =
3248 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003249 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3250 mapper1.setMetaState(AMETA_ALT_ON);
3251 mapper1.addSupportedKeyCode(AKEYCODE_A);
3252 mapper1.addSupportedKeyCode(AKEYCODE_B);
3253 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
3254 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
3255 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
3256 mapper1.setScanCodeState(3, AKEY_STATE_UP);
3257 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003259 FakeInputMapper& mapper2 =
3260 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003261 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003262
3263 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003264 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003265
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003266 std::string propertyValue;
3267 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003268 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003269 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003270
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003271 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
3272 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273
3274 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003275 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003276 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
3277 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278
3279 NotifyDeviceResetArgs resetArgs;
3280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3281 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3282 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3283
3284 // Metadata.
3285 ASSERT_FALSE(mDevice->isIgnored());
3286 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
3287
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00003288 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003289 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003290 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
3292 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
3293
3294 // State queries.
3295 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
3296 << "Should query mappers and combine meta states.";
3297
3298 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3299 << "Should return unknown key code state when source not supported.";
3300 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3301 << "Should return unknown scan code state when source not supported.";
3302 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3303 << "Should return unknown switch state when source not supported.";
3304
3305 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
3306 << "Should query mapper when source is supported.";
3307 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
3308 << "Should query mapper when source is supported.";
3309 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
3310 << "Should query mapper when source is supported.";
3311
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003312 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08003313 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003314 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003315 << "Should do nothing when source is unsupported.";
3316 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
3317 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
3318 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
3319 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
3320
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003321 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003322 << "Should query mapper when source is supported.";
3323 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
3324 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
3325 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
3326 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
3327
3328 // Event handling.
3329 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003330 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003331 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003332
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003333 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
3334 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003335}
3336
Arthur Hung2c9a3342019-07-23 14:18:59 +08003337// A single input device is associated with a specific display. Check that:
3338// 1. Device is disabled if the viewport corresponding to the associated display is not found
3339// 2. Device is disabled when setEnabled API is called
3340TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003341 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003342
3343 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003344 std::list<NotifyArgs> unused =
3345 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003346
3347 // Device should be enabled by default.
3348 ASSERT_TRUE(mDevice->isEnabled());
3349
3350 // Prepare associated info.
3351 constexpr uint8_t hdmi = 1;
3352 const std::string UNIQUE_ID = "local:1";
3353
3354 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003355 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3356 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003357 // Device should be disabled because it is associated with a specific display via
3358 // input port <-> display port association, but the corresponding display is not found
3359 ASSERT_FALSE(mDevice->isEnabled());
3360
3361 // Prepare displays.
3362 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003363 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
3364 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003365 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3366 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003367 ASSERT_TRUE(mDevice->isEnabled());
3368
3369 // Device should be disabled after set disable.
3370 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003371 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3372 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003373 ASSERT_FALSE(mDevice->isEnabled());
3374
3375 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003376 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3377 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003378 ASSERT_FALSE(mDevice->isEnabled());
3379}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003380
Christine Franks1ba71cc2021-04-07 14:37:42 -07003381TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
3382 // Device should be enabled by default.
3383 mFakePolicy->clearViewports();
3384 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003385 std::list<NotifyArgs> unused =
3386 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003387 ASSERT_TRUE(mDevice->isEnabled());
3388
3389 // Device should be disabled because it is associated with a specific display, but the
3390 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08003391 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003392 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3393 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003394 ASSERT_FALSE(mDevice->isEnabled());
3395
3396 // Device should be enabled when a display is found.
3397 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3398 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3399 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003400 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3401 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003402 ASSERT_TRUE(mDevice->isEnabled());
3403
3404 // Device should be disabled after set disable.
3405 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003406 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3407 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003408 ASSERT_FALSE(mDevice->isEnabled());
3409
3410 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003411 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3412 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003413 ASSERT_FALSE(mDevice->isEnabled());
3414}
3415
Christine Franks2a2293c2022-01-18 11:51:16 -08003416TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
3417 mFakePolicy->clearViewports();
3418 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003419 std::list<NotifyArgs> unused =
3420 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08003421
Christine Franks2a2293c2022-01-18 11:51:16 -08003422 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
3423 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3424 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3425 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003426 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3427 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08003428 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
3429}
3430
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07003431/**
3432 * This test reproduces a crash caused by a dangling reference that remains after device is added
3433 * and removed. The reference is accessed in InputDevice::dump(..);
3434 */
3435TEST_F(InputDeviceTest, DumpDoesNotCrash) {
3436 constexpr int32_t TEST_EVENTHUB_ID = 10;
3437 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
3438
3439 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
3440 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
3441 device.removeEventHubDevice(TEST_EVENTHUB_ID);
3442 std::string dumpStr, eventHubDevStr;
3443 device.dump(dumpStr, eventHubDevStr);
3444}
3445
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003446TEST_F(InputDeviceTest, GetBluetoothAddress) {
3447 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
3448 ASSERT_TRUE(address);
3449 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
3450}
3451
Michael Wrightd02c5b62014-02-10 15:10:22 -08003452// --- InputMapperTest ---
3453
3454class InputMapperTest : public testing::Test {
3455protected:
3456 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003457 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003458 static const int32_t DEVICE_ID;
3459 static const int32_t DEVICE_GENERATION;
3460 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003461 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003462 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003463
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003464 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003465 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003466 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08003467 std::unique_ptr<InstrumentedInputReader> mReader;
3468 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003469
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003470 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003471 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003472 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003473 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08003474 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003475 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08003476 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003477 // Consume the device reset notification generated when adding a new device.
3478 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003479 }
3480
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003481 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003482 SetUp(DEVICE_CLASSES);
3483 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003484
Chris Yea52ade12020-08-27 16:49:20 -07003485 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003486 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003487 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003488 }
3489
3490 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003491 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003492 }
3493
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003494 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003495 if (!changes ||
3496 (changes &
3497 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3498 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003499 mReader->requestRefreshConfiguration(changes);
3500 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003501 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003502 std::list<NotifyArgs> out =
3503 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003504 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003505 for (const NotifyArgs& args : out) {
3506 mFakeListener->notify(args);
3507 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003508 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003509 return out;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003510 }
3511
arthurhungdcef2dc2020-08-11 14:47:50 +08003512 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3513 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003514 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003515 InputDeviceIdentifier identifier;
3516 identifier.name = name;
3517 identifier.location = location;
3518 std::shared_ptr<InputDevice> device =
3519 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3520 identifier);
3521 mReader->pushNextDevice(device);
3522 mFakeEventHub->addDevice(eventHubId, name, classes);
3523 mReader->loopOnce();
3524 return device;
3525 }
3526
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003527 template <class T, typename... Args>
3528 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003529 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003530 configureDevice(0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003531 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
3532 resetArgList += mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003533 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003534 for (const NotifyArgs& loopArgs : resetArgList) {
3535 mFakeListener->notify(loopArgs);
3536 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003537 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003538 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003539 }
3540
3541 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003542 int32_t orientation, const std::string& uniqueId,
3543 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003544 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3545 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003546 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3547 }
3548
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003549 void clearViewports() {
3550 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551 }
3552
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003553 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
3554 int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003555 RawEvent event;
3556 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003557 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003558 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003559 event.type = type;
3560 event.code = code;
3561 event.value = value;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003562 std::list<NotifyArgs> processArgList = mapper.process(&event);
3563 for (const NotifyArgs& args : processArgList) {
3564 mFakeListener->notify(args);
3565 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003566 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003567 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003568 return processArgList;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003569 }
3570
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00003571 void resetMapper(InputMapper& mapper, nsecs_t when) {
3572 const auto resetArgs = mapper.reset(when);
3573 for (const auto args : resetArgs) {
3574 mFakeListener->notify(args);
3575 }
3576 // Loop the reader to flush the input listener queue.
3577 mReader->loopOnce();
3578 }
3579
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00003580 std::list<NotifyArgs> handleTimeout(InputMapper& mapper, nsecs_t when) {
3581 std::list<NotifyArgs> generatedArgs = mapper.timeoutExpired(when);
3582 for (const NotifyArgs& args : generatedArgs) {
3583 mFakeListener->notify(args);
3584 }
3585 // Loop the reader to flush the input listener queue.
3586 mReader->loopOnce();
3587 return generatedArgs;
3588 }
3589
Michael Wrightd02c5b62014-02-10 15:10:22 -08003590 static void assertMotionRange(const InputDeviceInfo& info,
3591 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3592 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003593 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003594 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3595 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3596 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3597 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3598 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3599 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3600 }
3601
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003602 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3603 float size, float touchMajor, float touchMinor, float toolMajor,
3604 float toolMinor, float orientation, float distance,
3605 float scaledAxisEpsilon = 1.f) {
3606 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3607 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003608 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3609 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003610 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3611 scaledAxisEpsilon);
3612 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3613 scaledAxisEpsilon);
3614 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3615 scaledAxisEpsilon);
3616 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3617 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003618 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3619 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3620 }
3621
Michael Wright17db18e2020-06-26 20:51:44 +01003622 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003623 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003624 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003625 ASSERT_NEAR(x, actualX, 1);
3626 ASSERT_NEAR(y, actualY, 1);
3627 }
3628};
3629
3630const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003631const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003632const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003633const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3634const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003635const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3636 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003637const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003638
3639// --- SwitchInputMapperTest ---
3640
3641class SwitchInputMapperTest : public InputMapperTest {
3642protected:
3643};
3644
3645TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003646 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003647
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003648 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003649}
3650
3651TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003652 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003653
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003654 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003655 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003656
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003657 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003658 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003659}
3660
3661TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003662 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003663 std::list<NotifyArgs> out;
3664 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3665 ASSERT_TRUE(out.empty());
3666 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3667 ASSERT_TRUE(out.empty());
3668 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3669 ASSERT_TRUE(out.empty());
3670 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003671
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003672 ASSERT_EQ(1u, out.size());
3673 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003674 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003675 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3676 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003677 args.switchMask);
3678 ASSERT_EQ(uint32_t(0), args.policyFlags);
3679}
3680
Chris Ye87143712020-11-10 05:05:58 +00003681// --- VibratorInputMapperTest ---
3682class VibratorInputMapperTest : public InputMapperTest {
3683protected:
3684 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3685};
3686
3687TEST_F(VibratorInputMapperTest, GetSources) {
3688 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3689
3690 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3691}
3692
3693TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3694 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3695
3696 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3697}
3698
3699TEST_F(VibratorInputMapperTest, Vibrate) {
3700 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003701 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003702 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3703
3704 VibrationElement pattern(2);
3705 VibrationSequence sequence(2);
3706 pattern.duration = std::chrono::milliseconds(200);
3707 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3708 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3709 sequence.addElement(pattern);
3710 pattern.duration = std::chrono::milliseconds(500);
3711 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3712 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3713 sequence.addElement(pattern);
3714
3715 std::vector<int64_t> timings = {0, 1};
3716 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3717
3718 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003719 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003720 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003721 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003722 // Verify vibrator state listener was notified.
3723 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003724 ASSERT_EQ(1u, out.size());
3725 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3726 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3727 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003728 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003729 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003730 ASSERT_FALSE(mapper.isVibrating());
3731 // Verify vibrator state listener was notified.
3732 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003733 ASSERT_EQ(1u, out.size());
3734 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3735 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3736 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003737}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003738
Chris Yef59a2f42020-10-16 12:55:26 -07003739// --- SensorInputMapperTest ---
3740
3741class SensorInputMapperTest : public InputMapperTest {
3742protected:
3743 static const int32_t ACCEL_RAW_MIN;
3744 static const int32_t ACCEL_RAW_MAX;
3745 static const int32_t ACCEL_RAW_FUZZ;
3746 static const int32_t ACCEL_RAW_FLAT;
3747 static const int32_t ACCEL_RAW_RESOLUTION;
3748
3749 static const int32_t GYRO_RAW_MIN;
3750 static const int32_t GYRO_RAW_MAX;
3751 static const int32_t GYRO_RAW_FUZZ;
3752 static const int32_t GYRO_RAW_FLAT;
3753 static const int32_t GYRO_RAW_RESOLUTION;
3754
3755 static const float GRAVITY_MS2_UNIT;
3756 static const float DEGREE_RADIAN_UNIT;
3757
3758 void prepareAccelAxes();
3759 void prepareGyroAxes();
3760 void setAccelProperties();
3761 void setGyroProperties();
3762 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3763};
3764
3765const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3766const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3767const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3768const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3769const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3770
3771const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3772const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3773const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3774const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3775const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3776
3777const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3778const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3779
3780void SensorInputMapperTest::prepareAccelAxes() {
3781 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3782 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3783 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3784 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3785 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3786 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3787}
3788
3789void SensorInputMapperTest::prepareGyroAxes() {
3790 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3791 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3792 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3793 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3794 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3795 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3796}
3797
3798void SensorInputMapperTest::setAccelProperties() {
3799 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3800 /* sensorDataIndex */ 0);
3801 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3802 /* sensorDataIndex */ 1);
3803 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3804 /* sensorDataIndex */ 2);
3805 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3806 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3807 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3808 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3809 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3810}
3811
3812void SensorInputMapperTest::setGyroProperties() {
3813 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3814 /* sensorDataIndex */ 0);
3815 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3816 /* sensorDataIndex */ 1);
3817 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3818 /* sensorDataIndex */ 2);
3819 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3820 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3821 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3822 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3823 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3824}
3825
3826TEST_F(SensorInputMapperTest, GetSources) {
3827 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3828
3829 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3830}
3831
3832TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3833 setAccelProperties();
3834 prepareAccelAxes();
3835 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3836
3837 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3838 std::chrono::microseconds(10000),
3839 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003840 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003841 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3842 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3843 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3844 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3845 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003846
3847 NotifySensorArgs args;
3848 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3849 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3850 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3851
3852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3853 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3854 ASSERT_EQ(args.deviceId, DEVICE_ID);
3855 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3856 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3857 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3858 ASSERT_EQ(args.values, values);
3859 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3860}
3861
3862TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3863 setGyroProperties();
3864 prepareGyroAxes();
3865 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3866
3867 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3868 std::chrono::microseconds(10000),
3869 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003870 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003871 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3872 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3873 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3874 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3875 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003876
3877 NotifySensorArgs args;
3878 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3879 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3880 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3881
3882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3883 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3884 ASSERT_EQ(args.deviceId, DEVICE_ID);
3885 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3886 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3887 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3888 ASSERT_EQ(args.values, values);
3889 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3890}
3891
Michael Wrightd02c5b62014-02-10 15:10:22 -08003892// --- KeyboardInputMapperTest ---
3893
3894class KeyboardInputMapperTest : public InputMapperTest {
3895protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003896 const std::string UNIQUE_ID = "local:0";
3897
3898 void prepareDisplay(int32_t orientation);
3899
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003900 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003901 int32_t originalKeyCode, int32_t rotatedKeyCode,
3902 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003903};
3904
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003905/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3906 * orientation.
3907 */
3908void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003909 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3910 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003911}
3912
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003913void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003914 int32_t originalScanCode, int32_t originalKeyCode,
3915 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003916 NotifyKeyArgs args;
3917
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003918 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3920 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3921 ASSERT_EQ(originalScanCode, args.scanCode);
3922 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003923 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003924
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003925 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3927 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3928 ASSERT_EQ(originalScanCode, args.scanCode);
3929 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003930 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003931}
3932
Michael Wrightd02c5b62014-02-10 15:10:22 -08003933TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003934 KeyboardInputMapper& mapper =
3935 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3936 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003937
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003938 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003939}
3940
3941TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3942 const int32_t USAGE_A = 0x070004;
3943 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003944 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3945 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003946 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3947 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3948 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003949
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003950 KeyboardInputMapper& mapper =
3951 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3952 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003953 // Initial metastate is AMETA_NONE.
3954 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003955
3956 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003957 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003958 NotifyKeyArgs args;
3959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3960 ASSERT_EQ(DEVICE_ID, args.deviceId);
3961 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3962 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3963 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3964 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3965 ASSERT_EQ(KEY_HOME, args.scanCode);
3966 ASSERT_EQ(AMETA_NONE, args.metaState);
3967 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3968 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3969 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3970
3971 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003972 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3974 ASSERT_EQ(DEVICE_ID, args.deviceId);
3975 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3976 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3977 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3978 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3979 ASSERT_EQ(KEY_HOME, args.scanCode);
3980 ASSERT_EQ(AMETA_NONE, args.metaState);
3981 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3982 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3983 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3984
3985 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003986 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3987 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3989 ASSERT_EQ(DEVICE_ID, args.deviceId);
3990 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3991 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3992 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3993 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3994 ASSERT_EQ(0, args.scanCode);
3995 ASSERT_EQ(AMETA_NONE, args.metaState);
3996 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3997 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3998 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3999
4000 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
4002 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4004 ASSERT_EQ(DEVICE_ID, args.deviceId);
4005 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4006 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4007 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4008 ASSERT_EQ(AKEYCODE_A, args.keyCode);
4009 ASSERT_EQ(0, args.scanCode);
4010 ASSERT_EQ(AMETA_NONE, args.metaState);
4011 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4012 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4013 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4014
4015 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
4017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4019 ASSERT_EQ(DEVICE_ID, args.deviceId);
4020 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4021 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4022 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4023 ASSERT_EQ(0, args.keyCode);
4024 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
4025 ASSERT_EQ(AMETA_NONE, args.metaState);
4026 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4027 ASSERT_EQ(0U, args.policyFlags);
4028 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4029
4030 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
4032 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4034 ASSERT_EQ(DEVICE_ID, args.deviceId);
4035 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4036 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4037 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4038 ASSERT_EQ(0, args.keyCode);
4039 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
4040 ASSERT_EQ(AMETA_NONE, args.metaState);
4041 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4042 ASSERT_EQ(0U, args.policyFlags);
4043 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4044}
4045
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004046/**
4047 * Ensure that the readTime is set to the time when the EV_KEY is received.
4048 */
4049TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
4050 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4051
4052 KeyboardInputMapper& mapper =
4053 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4054 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4055 NotifyKeyArgs args;
4056
4057 // Key down
4058 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
4059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4060 ASSERT_EQ(12, args.readTime);
4061
4062 // Key up
4063 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
4064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4065 ASSERT_EQ(15, args.readTime);
4066}
4067
Michael Wrightd02c5b62014-02-10 15:10:22 -08004068TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004069 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
4070 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07004071 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
4072 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
4073 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004074
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004075 KeyboardInputMapper& mapper =
4076 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4077 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078
Arthur Hung95f68612022-04-07 14:08:22 +08004079 // Initial metastate is AMETA_NONE.
4080 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004081
4082 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004084 NotifyKeyArgs args;
4085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4086 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004087 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08004088 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089
4090 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004091 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4093 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004094 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004095
4096 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004097 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4099 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004100 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004101
4102 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004103 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4105 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004106 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08004107 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004108}
4109
4110TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004111 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4112 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4113 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4114 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004115
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004116 KeyboardInputMapper& mapper =
4117 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4118 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004119
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004120 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004121 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4122 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
4123 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4124 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
4125 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4126 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
4127 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4128 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
4129}
4130
4131TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004132 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4133 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4134 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4135 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004136
Michael Wrightd02c5b62014-02-10 15:10:22 -08004137 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004138 KeyboardInputMapper& mapper =
4139 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4140 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004142 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004143 ASSERT_NO_FATAL_FAILURE(
4144 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4145 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4146 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4147 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4148 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4149 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4150 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004151
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004152 clearViewports();
4153 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004154 ASSERT_NO_FATAL_FAILURE(
4155 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4156 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4157 AKEYCODE_DPAD_UP, DISPLAY_ID));
4158 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4159 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4160 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4161 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004162
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004163 clearViewports();
4164 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004165 ASSERT_NO_FATAL_FAILURE(
4166 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4167 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4168 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4169 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4170 AKEYCODE_DPAD_UP, DISPLAY_ID));
4171 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4172 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004173
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004174 clearViewports();
4175 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004176 ASSERT_NO_FATAL_FAILURE(
4177 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4178 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4179 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4180 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4181 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4182 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4183 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184
4185 // Special case: if orientation changes while key is down, we still emit the same keycode
4186 // in the key up as we did in the key down.
4187 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004188 clearViewports();
4189 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004190 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4192 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4193 ASSERT_EQ(KEY_UP, args.scanCode);
4194 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
4195
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004196 clearViewports();
4197 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004198 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4200 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4201 ASSERT_EQ(KEY_UP, args.scanCode);
4202 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
4203}
4204
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004205TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
4206 // If the keyboard is not orientation aware,
4207 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004208 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004209
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004210 KeyboardInputMapper& mapper =
4211 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4212 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004213 NotifyKeyArgs args;
4214
4215 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004218 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4220 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
4221
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004222 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004223 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004225 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4227 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
4228}
4229
4230TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
4231 // If the keyboard is orientation aware,
4232 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004233 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004234
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004235 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004236 KeyboardInputMapper& mapper =
4237 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4238 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004239 NotifyKeyArgs args;
4240
4241 // Display id should be ADISPLAY_ID_NONE without any display configuration.
4242 // ^--- already checked by the previous test
4243
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004244 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004245 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004246 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004248 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4250 ASSERT_EQ(DISPLAY_ID, args.displayId);
4251
4252 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004253 clearViewports();
4254 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004255 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004256 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004258 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4260 ASSERT_EQ(newDisplayId, args.displayId);
4261}
4262
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004264 KeyboardInputMapper& mapper =
4265 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4266 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004268 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004269 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004271 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004272 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273}
4274
Philip Junker4af3b3d2021-12-14 10:36:55 +01004275TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
4276 KeyboardInputMapper& mapper =
4277 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4278 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4279
4280 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
4281 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
4282 << "If a mapping is available, the result is equal to the mapping";
4283
4284 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
4285 << "If no mapping is available, the result is the key location";
4286}
4287
Michael Wrightd02c5b62014-02-10 15:10:22 -08004288TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004289 KeyboardInputMapper& mapper =
4290 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4291 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004292
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004293 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004294 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004296 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004297 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004298}
4299
4300TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004301 KeyboardInputMapper& mapper =
4302 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4303 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004305 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004308 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309 ASSERT_TRUE(flags[0]);
4310 ASSERT_FALSE(flags[1]);
4311}
4312
4313TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004314 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4315 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4316 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4317 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4318 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4319 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004320
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004321 KeyboardInputMapper& mapper =
4322 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4323 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004324 // Initial metastate is AMETA_NONE.
4325 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004326
4327 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004328 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4329 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4330 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004331
4332 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4334 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004335 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4336 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4337 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004338 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339
4340 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004341 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004343 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4344 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4345 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004346 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004347
4348 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004349 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4350 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004351 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4352 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4353 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004354 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004355
4356 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004357 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004359 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4360 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4361 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004362 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363
4364 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004365 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4366 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004367 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4368 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4369 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004370 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371
4372 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004373 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4374 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004375 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4376 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4377 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004378 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379}
4380
Chris Yea52ade12020-08-27 16:49:20 -07004381TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
4382 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
4383 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
4384 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
4385 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
4386
4387 KeyboardInputMapper& mapper =
4388 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4389 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4390
Chris Yea52ade12020-08-27 16:49:20 -07004391 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004392 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07004393 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4394 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
4395 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
4396 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4397
4398 NotifyKeyArgs args;
4399 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07004401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4402 ASSERT_EQ(AMETA_NONE, args.metaState);
4403 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4404 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4405 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4406
4407 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004408 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07004409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4410 ASSERT_EQ(AMETA_NONE, args.metaState);
4411 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4412 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4413 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4414}
4415
Arthur Hung2c9a3342019-07-23 14:18:59 +08004416TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
4417 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004418 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4419 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4420 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4421 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004422
4423 // keyboard 2.
4424 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08004425 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08004426 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004427 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08004428 std::shared_ptr<InputDevice> device2 =
4429 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004430 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08004431
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004432 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4433 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4434 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4435 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004436
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004437 KeyboardInputMapper& mapper =
4438 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4439 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004440
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004441 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004442 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004443 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004444 std::list<NotifyArgs> unused =
4445 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4446 0 /*changes*/);
4447 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004448
4449 // Prepared displays and associated info.
4450 constexpr uint8_t hdmi1 = 0;
4451 constexpr uint8_t hdmi2 = 1;
4452 const std::string SECONDARY_UNIQUE_ID = "local:1";
4453
4454 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
4455 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
4456
4457 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004458 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4459 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004460 ASSERT_FALSE(device2->isEnabled());
4461
4462 // Prepare second display.
4463 constexpr int32_t newDisplayId = 2;
4464 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004465 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004466 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004467 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004468 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004469 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4470 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004471
4472 // Device should be enabled after the associated display is found.
4473 ASSERT_TRUE(mDevice->isEnabled());
4474 ASSERT_TRUE(device2->isEnabled());
4475
4476 // Test pad key events
4477 ASSERT_NO_FATAL_FAILURE(
4478 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4479 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4480 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4481 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4482 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4483 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4484 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4485
4486 ASSERT_NO_FATAL_FAILURE(
4487 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
4488 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4489 AKEYCODE_DPAD_RIGHT, newDisplayId));
4490 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4491 AKEYCODE_DPAD_DOWN, newDisplayId));
4492 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4493 AKEYCODE_DPAD_LEFT, newDisplayId));
4494}
Michael Wrightd02c5b62014-02-10 15:10:22 -08004495
arthurhungc903df12020-08-11 15:08:42 +08004496TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
4497 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4498 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4499 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4500 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4501 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4502 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4503
4504 KeyboardInputMapper& mapper =
4505 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4506 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004507 // Initial metastate is AMETA_NONE.
4508 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004509
4510 // Initialization should have turned all of the lights off.
4511 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4512 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4513 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4514
4515 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004518 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4519 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4520
4521 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004522 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4523 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004524 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4525 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
4526
4527 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004528 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4529 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004530 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4531 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4532
4533 mFakeEventHub->removeDevice(EVENTHUB_ID);
4534 mReader->loopOnce();
4535
4536 // keyboard 2 should default toggle keys.
4537 const std::string USB2 = "USB2";
4538 const std::string DEVICE_NAME2 = "KEYBOARD2";
4539 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4540 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4541 std::shared_ptr<InputDevice> device2 =
4542 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004543 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004544 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4545 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4546 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4547 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4548 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4549 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4550
arthurhung6fe95782020-10-05 22:41:16 +08004551 KeyboardInputMapper& mapper2 =
4552 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4553 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004554 std::list<NotifyArgs> unused =
4555 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4556 0 /*changes*/);
4557 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08004558
4559 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4560 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4561 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004562 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4563 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004564}
4565
Arthur Hungcb40a002021-08-03 14:31:01 +00004566TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4567 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4568 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4569 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4570
4571 // Suppose we have two mappers. (DPAD + KEYBOARD)
4572 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4573 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4574 KeyboardInputMapper& mapper =
4575 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4576 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004577 // Initial metastate is AMETA_NONE.
4578 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004579
4580 mReader->toggleCapsLockState(DEVICE_ID);
4581 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4582}
4583
Arthur Hungfb3cc112022-04-13 07:39:50 +00004584TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4585 // keyboard 1.
4586 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4587 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4588 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4589 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4590 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4591 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4592
4593 KeyboardInputMapper& mapper1 =
4594 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4595 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4596
4597 // keyboard 2.
4598 const std::string USB2 = "USB2";
4599 const std::string DEVICE_NAME2 = "KEYBOARD2";
4600 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4601 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4602 std::shared_ptr<InputDevice> device2 =
4603 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4604 ftl::Flags<InputDeviceClass>(0));
4605 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4606 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4607 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4608 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4609 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4610 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4611
4612 KeyboardInputMapper& mapper2 =
4613 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4614 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004615 std::list<NotifyArgs> unused =
4616 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4617 0 /*changes*/);
4618 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004619
Arthur Hung95f68612022-04-07 14:08:22 +08004620 // Initial metastate is AMETA_NONE.
4621 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4622 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4623
4624 // Toggle num lock on and off.
4625 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4626 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004627 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4628 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4629 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4630
4631 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4632 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4633 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4634 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4635 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4636
4637 // Toggle caps lock on and off.
4638 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4639 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4640 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4641 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4642 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4643
4644 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4645 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4646 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4647 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4648 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4649
4650 // Toggle scroll lock on and off.
4651 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4652 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4653 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4654 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4655 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4656
4657 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4658 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4659 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4660 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4661 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4662}
4663
Arthur Hung2141d542022-08-23 07:45:21 +00004664TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4665 const int32_t USAGE_A = 0x070004;
4666 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4667 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4668
4669 KeyboardInputMapper& mapper =
4670 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4671 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4672 // Key down by scan code.
4673 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4674 NotifyKeyArgs args;
4675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4676 ASSERT_EQ(DEVICE_ID, args.deviceId);
4677 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4678 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4679 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4680 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4681 ASSERT_EQ(KEY_HOME, args.scanCode);
4682 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4683
4684 // Disable device, it should synthesize cancellation events for down events.
4685 mFakePolicy->addDisabledDevice(DEVICE_ID);
4686 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4687
4688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4689 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4690 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4691 ASSERT_EQ(KEY_HOME, args.scanCode);
4692 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4693}
4694
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004695// --- KeyboardInputMapperTest_ExternalDevice ---
4696
4697class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4698protected:
Chris Yea52ade12020-08-27 16:49:20 -07004699 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004700};
4701
4702TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004703 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4704 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004705
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004706 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4707 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4708 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4709 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004710
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004711 KeyboardInputMapper& mapper =
4712 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4713 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004714
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004715 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004716 NotifyKeyArgs args;
4717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4718 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4719
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004720 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4722 ASSERT_EQ(uint32_t(0), args.policyFlags);
4723
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004724 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4726 ASSERT_EQ(uint32_t(0), args.policyFlags);
4727
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004728 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4730 ASSERT_EQ(uint32_t(0), args.policyFlags);
4731
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004732 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4734 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4735
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004736 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4738 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4739}
4740
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004741TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004742 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004743
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004744 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4745 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4746 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004747
Powei Fengd041c5d2019-05-03 17:11:33 -07004748 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004749 KeyboardInputMapper& mapper =
4750 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4751 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004752
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004754 NotifyKeyArgs args;
4755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4756 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4757
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004758 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4760 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4761
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004762 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4764 ASSERT_EQ(uint32_t(0), args.policyFlags);
4765
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004766 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4768 ASSERT_EQ(uint32_t(0), args.policyFlags);
4769
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4772 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4773
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004774 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4776 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4777}
4778
Michael Wrightd02c5b62014-02-10 15:10:22 -08004779// --- CursorInputMapperTest ---
4780
4781class CursorInputMapperTest : public InputMapperTest {
4782protected:
4783 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4784
Michael Wright17db18e2020-06-26 20:51:44 +01004785 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004786
Chris Yea52ade12020-08-27 16:49:20 -07004787 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004788 InputMapperTest::SetUp();
4789
Michael Wright17db18e2020-06-26 20:51:44 +01004790 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004791 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004792 }
4793
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004794 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4795 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004796
4797 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004798 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4799 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4800 }
4801
4802 void prepareSecondaryDisplay() {
4803 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4804 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4805 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004806 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004807
4808 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4809 float pressure) {
4810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4811 0.0f, 0.0f, 0.0f, EPSILON));
4812 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004813};
4814
4815const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4816
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004817void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4818 int32_t originalY, int32_t rotatedX,
4819 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004820 NotifyMotionArgs args;
4821
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004822 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4823 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4826 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004827 ASSERT_NO_FATAL_FAILURE(
4828 assertCursorPointerCoords(args.pointerCoords[0],
4829 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4830 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831}
4832
4833TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004835 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004836
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004837 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004838}
4839
4840TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004841 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004842 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004843
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004844 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004845}
4846
4847TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004849 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004850
4851 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004852 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853
4854 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004855 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4856 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004857 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4858 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4859
4860 // When the bounds are set, then there should be a valid motion range.
4861 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4862
4863 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004864 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004865
4866 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4867 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4868 1, 800 - 1, 0.0f, 0.0f));
4869 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4870 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4871 2, 480 - 1, 0.0f, 0.0f));
4872 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4873 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4874 0.0f, 1.0f, 0.0f, 0.0f));
4875}
4876
4877TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004878 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004879 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004880
4881 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004882 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004883
4884 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4885 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4886 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4887 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4888 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4889 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4890 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4891 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4892 0.0f, 1.0f, 0.0f, 0.0f));
4893}
4894
4895TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004896 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004897 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004898
arthurhungdcef2dc2020-08-11 14:47:50 +08004899 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004900
4901 NotifyMotionArgs args;
4902
4903 // Button press.
4904 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004905 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4906 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4908 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4909 ASSERT_EQ(DEVICE_ID, args.deviceId);
4910 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4911 ASSERT_EQ(uint32_t(0), args.policyFlags);
4912 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4913 ASSERT_EQ(0, args.flags);
4914 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4915 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4916 ASSERT_EQ(0, args.edgeFlags);
4917 ASSERT_EQ(uint32_t(1), args.pointerCount);
4918 ASSERT_EQ(0, args.pointerProperties[0].id);
4919 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004920 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004921 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4922 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4923 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4924
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4926 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4927 ASSERT_EQ(DEVICE_ID, args.deviceId);
4928 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4929 ASSERT_EQ(uint32_t(0), args.policyFlags);
4930 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4931 ASSERT_EQ(0, args.flags);
4932 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4933 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4934 ASSERT_EQ(0, args.edgeFlags);
4935 ASSERT_EQ(uint32_t(1), args.pointerCount);
4936 ASSERT_EQ(0, args.pointerProperties[0].id);
4937 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004938 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004939 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4940 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4941 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4942
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004944 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4945 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4947 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4948 ASSERT_EQ(DEVICE_ID, args.deviceId);
4949 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4950 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004951 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4952 ASSERT_EQ(0, args.flags);
4953 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4954 ASSERT_EQ(0, args.buttonState);
4955 ASSERT_EQ(0, args.edgeFlags);
4956 ASSERT_EQ(uint32_t(1), args.pointerCount);
4957 ASSERT_EQ(0, args.pointerProperties[0].id);
4958 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004959 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004960 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4961 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4962 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4963
4964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4965 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4966 ASSERT_EQ(DEVICE_ID, args.deviceId);
4967 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4968 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004969 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4970 ASSERT_EQ(0, args.flags);
4971 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4972 ASSERT_EQ(0, args.buttonState);
4973 ASSERT_EQ(0, args.edgeFlags);
4974 ASSERT_EQ(uint32_t(1), args.pointerCount);
4975 ASSERT_EQ(0, args.pointerProperties[0].id);
4976 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004977 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004978 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4979 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4980 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4981}
4982
4983TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004985 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004986
4987 NotifyMotionArgs args;
4988
4989 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004990 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4993 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004994 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4995 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4996 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004997
4998 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
5000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5002 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005003 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
5004 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005005}
5006
5007TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005008 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005009 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005010
5011 NotifyMotionArgs args;
5012
5013 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005014 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5015 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5017 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005018 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005019
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5021 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005022 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005023
Michael Wrightd02c5b62014-02-10 15:10:22 -08005024 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005025 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5026 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005028 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005029 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005030
5031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005032 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005033 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005034}
5035
5036TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005037 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005038 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005039
5040 NotifyMotionArgs args;
5041
5042 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
5045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5046 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5048 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005049 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
5050 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
5051 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005052
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5054 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005055 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
5056 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
5057 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005058
Michael Wrightd02c5b62014-02-10 15:10:22 -08005059 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005060 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
5061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
5062 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005065 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
5066 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
5067 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005068
5069 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005070 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005073 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005074 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005075
5076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005077 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005078 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005079}
5080
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005081TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005082 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005083 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005084 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5085 // need to be rotated.
5086 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005087 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005088
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005089 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005090 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
5091 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
5092 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
5093 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
5094 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
5095 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
5096 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
5097 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
5098}
5099
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005100TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005101 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005102 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005103 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5104 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005105 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005106
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005107 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005108 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005109 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
5110 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
5111 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
5112 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
5113 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
5114 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
5115 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
5116 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
5117
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005118 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005119 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005120 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
5121 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
5122 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
5123 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
5124 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
5125 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
5126 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
5127 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005128
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005129 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005130 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005131 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
5132 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
5133 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
5134 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
5135 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
5136 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
5137 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
5138 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
5139
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005140 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005141 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005142 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
5143 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
5144 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
5145 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
5146 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
5147 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
5148 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
5149 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005150}
5151
5152TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005153 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005154 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005155
5156 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5157 mFakePointerController->setPosition(100, 200);
5158 mFakePointerController->setButtonState(0);
5159
5160 NotifyMotionArgs motionArgs;
5161 NotifyKeyArgs keyArgs;
5162
5163 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
5165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5167 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5168 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5169 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005170 ASSERT_NO_FATAL_FAILURE(
5171 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005172
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5174 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5175 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5176 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005177 ASSERT_NO_FATAL_FAILURE(
5178 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005179
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
5181 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005184 ASSERT_EQ(0, motionArgs.buttonState);
5185 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005186 ASSERT_NO_FATAL_FAILURE(
5187 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005188
5189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005190 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005191 ASSERT_EQ(0, motionArgs.buttonState);
5192 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005193 ASSERT_NO_FATAL_FAILURE(
5194 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005195
5196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005197 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005198 ASSERT_EQ(0, motionArgs.buttonState);
5199 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005200 ASSERT_NO_FATAL_FAILURE(
5201 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005202
5203 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
5205 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
5206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5208 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5209 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5210 motionArgs.buttonState);
5211 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5212 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005213 ASSERT_NO_FATAL_FAILURE(
5214 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005215
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5217 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5218 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5219 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5220 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005221 ASSERT_NO_FATAL_FAILURE(
5222 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005223
5224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5225 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5226 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5227 motionArgs.buttonState);
5228 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5229 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005230 ASSERT_NO_FATAL_FAILURE(
5231 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005232
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
5234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005236 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005237 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5238 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005239 ASSERT_NO_FATAL_FAILURE(
5240 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005241
5242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005243 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005244 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5245 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005246 ASSERT_NO_FATAL_FAILURE(
5247 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005248
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005249 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5250 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005252 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5253 ASSERT_EQ(0, motionArgs.buttonState);
5254 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005255 ASSERT_NO_FATAL_FAILURE(
5256 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005257 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5258 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005259
5260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005261 ASSERT_EQ(0, motionArgs.buttonState);
5262 ASSERT_EQ(0, mFakePointerController->getButtonState());
5263 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005264 ASSERT_NO_FATAL_FAILURE(
5265 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005266
Michael Wrightd02c5b62014-02-10 15:10:22 -08005267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5268 ASSERT_EQ(0, motionArgs.buttonState);
5269 ASSERT_EQ(0, mFakePointerController->getButtonState());
5270 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005271 ASSERT_NO_FATAL_FAILURE(
5272 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005273
5274 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
5276 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5278 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5279 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005280
Michael Wrightd02c5b62014-02-10 15:10:22 -08005281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005282 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005283 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5284 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005285 ASSERT_NO_FATAL_FAILURE(
5286 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005287
5288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5289 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5290 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5291 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005292 ASSERT_NO_FATAL_FAILURE(
5293 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005294
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
5296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005298 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005299 ASSERT_EQ(0, motionArgs.buttonState);
5300 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005301 ASSERT_NO_FATAL_FAILURE(
5302 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005303
5304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005305 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005306 ASSERT_EQ(0, motionArgs.buttonState);
5307 ASSERT_EQ(0, mFakePointerController->getButtonState());
5308
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005309 ASSERT_NO_FATAL_FAILURE(
5310 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5312 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5313 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5314
5315 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
5317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5319 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5320 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005321
Michael Wrightd02c5b62014-02-10 15:10:22 -08005322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005323 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005324 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5325 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005326 ASSERT_NO_FATAL_FAILURE(
5327 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005328
5329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5330 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5331 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5332 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005333 ASSERT_NO_FATAL_FAILURE(
5334 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005335
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
5337 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005339 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005340 ASSERT_EQ(0, motionArgs.buttonState);
5341 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005342 ASSERT_NO_FATAL_FAILURE(
5343 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005344
5345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5346 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5347 ASSERT_EQ(0, motionArgs.buttonState);
5348 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005349 ASSERT_NO_FATAL_FAILURE(
5350 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005351
Michael Wrightd02c5b62014-02-10 15:10:22 -08005352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5353 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5354 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5355
5356 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005357 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
5358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5360 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5361 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005362
Michael Wrightd02c5b62014-02-10 15:10:22 -08005363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005364 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005365 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5366 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005367 ASSERT_NO_FATAL_FAILURE(
5368 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005369
5370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5371 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5372 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5373 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005374 ASSERT_NO_FATAL_FAILURE(
5375 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005376
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
5378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005380 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005381 ASSERT_EQ(0, motionArgs.buttonState);
5382 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005383 ASSERT_NO_FATAL_FAILURE(
5384 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005385
5386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5387 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5388 ASSERT_EQ(0, motionArgs.buttonState);
5389 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005390 ASSERT_NO_FATAL_FAILURE(
5391 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005392
Michael Wrightd02c5b62014-02-10 15:10:22 -08005393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5394 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5395 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5396
5397 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005398 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
5399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5401 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5402 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005403
Michael Wrightd02c5b62014-02-10 15:10:22 -08005404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005405 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005406 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5407 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005408 ASSERT_NO_FATAL_FAILURE(
5409 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005410
5411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5412 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5413 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5414 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005415 ASSERT_NO_FATAL_FAILURE(
5416 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005417
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
5419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005421 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005422 ASSERT_EQ(0, motionArgs.buttonState);
5423 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005424 ASSERT_NO_FATAL_FAILURE(
5425 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005426
5427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5428 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5429 ASSERT_EQ(0, motionArgs.buttonState);
5430 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005431 ASSERT_NO_FATAL_FAILURE(
5432 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005433
Michael Wrightd02c5b62014-02-10 15:10:22 -08005434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5435 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5436 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5437}
5438
5439TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005440 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005441 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005442
5443 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5444 mFakePointerController->setPosition(100, 200);
5445 mFakePointerController->setButtonState(0);
5446
5447 NotifyMotionArgs args;
5448
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5451 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005453 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5454 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5455 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5456 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 +01005457 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005458}
5459
5460TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005461 addConfigurationProperty("cursor.mode", "pointer");
5462 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005463 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005464
5465 NotifyDeviceResetArgs resetArgs;
5466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5467 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5468 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5469
5470 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5471 mFakePointerController->setPosition(100, 200);
5472 mFakePointerController->setButtonState(0);
5473
5474 NotifyMotionArgs args;
5475
5476 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5479 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5481 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5482 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5484 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 +01005485 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005486
5487 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005488 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5489 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5491 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5492 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5494 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5496 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
5498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5499 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5500
5501 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005502 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5503 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5505 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5506 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
5507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5508 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5510 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5511 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
5512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5513 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5514
5515 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
5517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
5518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5520 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5522 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5523 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 +01005524 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005525
5526 // Disable pointer capture and check that the device generation got bumped
5527 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08005528 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005529 mFakePolicy->setPointerCapture(false);
5530 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08005531 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005532
5533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005534 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5535
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005536 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5537 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5538 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5540 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005541 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5543 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 +01005544 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005545}
5546
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00005547/**
5548 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
5549 * pointer acceleration or speed processing should not be applied.
5550 */
5551TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5552 addConfigurationProperty("cursor.mode", "pointer");
5553 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5554 100.f /*high threshold*/, 10.f /*acceleration*/);
5555 mFakePolicy->setVelocityControlParams(testParams);
5556 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5557
5558 NotifyDeviceResetArgs resetArgs;
5559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5560 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5561 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5562
5563 NotifyMotionArgs args;
5564
5565 // Move and verify scale is applied.
5566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5567 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5568 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5570 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5571 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5572 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5573 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5574 ASSERT_GT(relX, 10);
5575 ASSERT_GT(relY, 20);
5576
5577 // Enable Pointer Capture
5578 mFakePolicy->setPointerCapture(true);
5579 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5580 NotifyPointerCaptureChangedArgs captureArgs;
5581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5582 ASSERT_TRUE(captureArgs.request.enable);
5583
5584 // Move and verify scale is not applied.
5585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5586 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5589 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5591 ASSERT_EQ(10, args.pointerCoords[0].getX());
5592 ASSERT_EQ(20, args.pointerCoords[0].getY());
5593}
5594
Prabir Pradhan208360b2022-06-24 18:37:04 +00005595TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5596 addConfigurationProperty("cursor.mode", "pointer");
5597 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5598
5599 NotifyDeviceResetArgs resetArgs;
5600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5601 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5602 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5603
5604 // Ensure the display is rotated.
5605 prepareDisplay(DISPLAY_ORIENTATION_90);
5606
5607 NotifyMotionArgs args;
5608
5609 // Verify that the coordinates are rotated.
5610 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5611 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5614 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5615 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5616 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5617 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5618
5619 // Enable Pointer Capture.
5620 mFakePolicy->setPointerCapture(true);
5621 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5622 NotifyPointerCaptureChangedArgs captureArgs;
5623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5624 ASSERT_TRUE(captureArgs.request.enable);
5625
5626 // Move and verify rotation is not applied.
5627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5628 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5629 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5631 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5632 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5633 ASSERT_EQ(10, args.pointerCoords[0].getX());
5634 ASSERT_EQ(20, args.pointerCoords[0].getY());
5635}
5636
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005637TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005638 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005639
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005640 // Set up the default display.
5641 prepareDisplay(DISPLAY_ORIENTATION_90);
5642
5643 // Set up the secondary display as the display on which the pointer should be shown.
5644 // The InputDevice is not associated with any display.
5645 prepareSecondaryDisplay();
5646 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005647 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5648
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005649 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005650 mFakePointerController->setPosition(100, 200);
5651 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005652
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005653 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5655 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005658 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5659 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5660 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005661 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005662}
5663
5664TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5665 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5666
5667 // Set up the default display.
5668 prepareDisplay(DISPLAY_ORIENTATION_90);
5669
5670 // Set up the secondary display as the display on which the pointer should be shown,
5671 // and associate the InputDevice with the secondary display.
5672 prepareSecondaryDisplay();
5673 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5674 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5675 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5676
5677 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5678 mFakePointerController->setPosition(100, 200);
5679 mFakePointerController->setButtonState(0);
5680
5681 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5682 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5683 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005685 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5686 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5687 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005688 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5689}
5690
5691TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5692 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5693
5694 // Set up the default display as the display on which the pointer should be shown.
5695 prepareDisplay(DISPLAY_ORIENTATION_90);
5696 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5697
5698 // Associate the InputDevice with the secondary display.
5699 prepareSecondaryDisplay();
5700 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5701 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5702
5703 // The mapper should not generate any events because it is associated with a display that is
5704 // different from the pointer display.
5705 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5707 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005709}
5710
Michael Wrightd02c5b62014-02-10 15:10:22 -08005711// --- TouchInputMapperTest ---
5712
5713class TouchInputMapperTest : public InputMapperTest {
5714protected:
5715 static const int32_t RAW_X_MIN;
5716 static const int32_t RAW_X_MAX;
5717 static const int32_t RAW_Y_MIN;
5718 static const int32_t RAW_Y_MAX;
5719 static const int32_t RAW_TOUCH_MIN;
5720 static const int32_t RAW_TOUCH_MAX;
5721 static const int32_t RAW_TOOL_MIN;
5722 static const int32_t RAW_TOOL_MAX;
5723 static const int32_t RAW_PRESSURE_MIN;
5724 static const int32_t RAW_PRESSURE_MAX;
5725 static const int32_t RAW_ORIENTATION_MIN;
5726 static const int32_t RAW_ORIENTATION_MAX;
5727 static const int32_t RAW_DISTANCE_MIN;
5728 static const int32_t RAW_DISTANCE_MAX;
5729 static const int32_t RAW_TILT_MIN;
5730 static const int32_t RAW_TILT_MAX;
5731 static const int32_t RAW_ID_MIN;
5732 static const int32_t RAW_ID_MAX;
5733 static const int32_t RAW_SLOT_MIN;
5734 static const int32_t RAW_SLOT_MAX;
5735 static const float X_PRECISION;
5736 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005737 static const float X_PRECISION_VIRTUAL;
5738 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005739
5740 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005741 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005742
5743 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5744
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005745 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005746 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005747
Michael Wrightd02c5b62014-02-10 15:10:22 -08005748 enum Axes {
5749 POSITION = 1 << 0,
5750 TOUCH = 1 << 1,
5751 TOOL = 1 << 2,
5752 PRESSURE = 1 << 3,
5753 ORIENTATION = 1 << 4,
5754 MINOR = 1 << 5,
5755 ID = 1 << 6,
5756 DISTANCE = 1 << 7,
5757 TILT = 1 << 8,
5758 SLOT = 1 << 9,
5759 TOOL_TYPE = 1 << 10,
5760 };
5761
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005762 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5763 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005764 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005765 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005766 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005767 int32_t toRawX(float displayX);
5768 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005769 int32_t toRotatedRawX(float displayX);
5770 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005771 float toCookedX(float rawX, float rawY);
5772 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005773 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005774 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005775 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005776 float toDisplayY(int32_t rawY, int32_t displayHeight);
5777
Michael Wrightd02c5b62014-02-10 15:10:22 -08005778};
5779
5780const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5781const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5782const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5783const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5784const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5785const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5786const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5787const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005788const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5789const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005790const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5791const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5792const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5793const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5794const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5795const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5796const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5797const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5798const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5799const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5800const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5801const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005802const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5803 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5804const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5805 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005806const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5807 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005808
5809const float TouchInputMapperTest::GEOMETRIC_SCALE =
5810 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5811 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5812
5813const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5814 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5815 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5816};
5817
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005818void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005819 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5820 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005821}
5822
5823void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5824 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5825 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005826}
5827
Santos Cordonfa5cf462017-04-05 10:37:00 -07005828void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005829 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5830 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5831 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005832}
5833
Michael Wrightd02c5b62014-02-10 15:10:22 -08005834void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005835 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5836 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5837 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5838 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005839}
5840
Jason Gerecke489fda82012-09-07 17:19:40 -07005841void TouchInputMapperTest::prepareLocationCalibration() {
5842 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5843}
5844
Michael Wrightd02c5b62014-02-10 15:10:22 -08005845int32_t TouchInputMapperTest::toRawX(float displayX) {
5846 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5847}
5848
5849int32_t TouchInputMapperTest::toRawY(float displayY) {
5850 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5851}
5852
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005853int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5854 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5855}
5856
5857int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5858 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5859}
5860
Jason Gerecke489fda82012-09-07 17:19:40 -07005861float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5862 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5863 return rawX;
5864}
5865
5866float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5867 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5868 return rawY;
5869}
5870
Michael Wrightd02c5b62014-02-10 15:10:22 -08005871float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005872 return toDisplayX(rawX, DISPLAY_WIDTH);
5873}
5874
5875float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5876 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005877}
5878
5879float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005880 return toDisplayY(rawY, DISPLAY_HEIGHT);
5881}
5882
5883float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5884 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005885}
5886
5887
5888// --- SingleTouchInputMapperTest ---
5889
5890class SingleTouchInputMapperTest : public TouchInputMapperTest {
5891protected:
5892 void prepareButtons();
5893 void prepareAxes(int axes);
5894
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005895 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5896 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5897 void processUp(SingleTouchInputMapper& mappery);
5898 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5899 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5900 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5901 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5902 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5903 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005904};
5905
5906void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005907 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005908}
5909
5910void SingleTouchInputMapperTest::prepareAxes(int axes) {
5911 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005912 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5913 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005914 }
5915 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005916 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5917 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005918 }
5919 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005920 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5921 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005922 }
5923 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005924 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5925 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005926 }
5927 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005928 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5929 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005930 }
5931}
5932
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005933void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005934 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5935 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5936 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005937}
5938
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005939void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005940 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5941 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005942}
5943
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005944void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005945 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005946}
5947
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005948void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005950}
5951
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005952void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5953 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005954 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005955}
5956
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005957void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005958 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005959}
5960
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005961void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5962 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5964 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005965}
5966
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005967void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5968 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005970}
5971
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005972void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005973 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005974}
5975
Michael Wrightd02c5b62014-02-10 15:10:22 -08005976TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005977 prepareButtons();
5978 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005979 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005980
Harry Cutts16a24cc2022-10-26 15:22:19 +00005981 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005982}
5983
Michael Wrightd02c5b62014-02-10 15:10:22 -08005984TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005985 prepareButtons();
5986 prepareAxes(POSITION);
5987 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005988 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005989
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005990 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005991}
5992
5993TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005994 addConfigurationProperty("touch.deviceType", "touchScreen");
5995 prepareDisplay(DISPLAY_ORIENTATION_0);
5996 prepareButtons();
5997 prepareAxes(POSITION);
5998 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005999 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006000
6001 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006002 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006003
6004 // Virtual key is down.
6005 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6006 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6007 processDown(mapper, x, y);
6008 processSync(mapper);
6009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
6010
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006011 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006012
6013 // Virtual key is up.
6014 processUp(mapper);
6015 processSync(mapper);
6016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
6017
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006018 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006019}
6020
6021TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006022 addConfigurationProperty("touch.deviceType", "touchScreen");
6023 prepareDisplay(DISPLAY_ORIENTATION_0);
6024 prepareButtons();
6025 prepareAxes(POSITION);
6026 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006027 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006028
6029 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006030 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006031
6032 // Virtual key is down.
6033 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6034 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6035 processDown(mapper, x, y);
6036 processSync(mapper);
6037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
6038
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006039 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006040
6041 // Virtual key is up.
6042 processUp(mapper);
6043 processSync(mapper);
6044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
6045
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006046 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006047}
6048
6049TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006050 addConfigurationProperty("touch.deviceType", "touchScreen");
6051 prepareDisplay(DISPLAY_ORIENTATION_0);
6052 prepareButtons();
6053 prepareAxes(POSITION);
6054 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006055 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006056
Michael Wrightd02c5b62014-02-10 15:10:22 -08006057 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07006058 ASSERT_TRUE(
6059 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006060 ASSERT_TRUE(flags[0]);
6061 ASSERT_FALSE(flags[1]);
6062}
6063
6064TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006065 addConfigurationProperty("touch.deviceType", "touchScreen");
6066 prepareDisplay(DISPLAY_ORIENTATION_0);
6067 prepareButtons();
6068 prepareAxes(POSITION);
6069 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006070 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006071
arthurhungdcef2dc2020-08-11 14:47:50 +08006072 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006073
6074 NotifyKeyArgs args;
6075
6076 // Press virtual key.
6077 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6078 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6079 processDown(mapper, x, y);
6080 processSync(mapper);
6081
6082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
6083 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
6084 ASSERT_EQ(DEVICE_ID, args.deviceId);
6085 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
6086 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
6087 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
6088 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
6089 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
6090 ASSERT_EQ(KEY_HOME, args.scanCode);
6091 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
6092 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
6093
6094 // Release virtual key.
6095 processUp(mapper);
6096 processSync(mapper);
6097
6098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
6099 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
6100 ASSERT_EQ(DEVICE_ID, args.deviceId);
6101 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
6102 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
6103 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
6104 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
6105 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
6106 ASSERT_EQ(KEY_HOME, args.scanCode);
6107 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
6108 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
6109
6110 // Should not have sent any motions.
6111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6112}
6113
6114TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006115 addConfigurationProperty("touch.deviceType", "touchScreen");
6116 prepareDisplay(DISPLAY_ORIENTATION_0);
6117 prepareButtons();
6118 prepareAxes(POSITION);
6119 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006120 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006121
arthurhungdcef2dc2020-08-11 14:47:50 +08006122 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006123
6124 NotifyKeyArgs keyArgs;
6125
6126 // Press virtual key.
6127 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6128 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6129 processDown(mapper, x, y);
6130 processSync(mapper);
6131
6132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6133 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
6134 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
6135 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
6136 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
6137 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6138 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
6139 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
6140 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
6141 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
6142 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
6143
6144 // Move out of bounds. This should generate a cancel and a pointer down since we moved
6145 // into the display area.
6146 y -= 100;
6147 processMove(mapper, x, y);
6148 processSync(mapper);
6149
6150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6151 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
6152 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
6153 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
6154 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
6155 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6156 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
6157 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
6158 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
6159 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
6160 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
6161 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
6162
6163 NotifyMotionArgs motionArgs;
6164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6165 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6166 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6167 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6168 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6169 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6170 ASSERT_EQ(0, motionArgs.flags);
6171 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6172 ASSERT_EQ(0, motionArgs.buttonState);
6173 ASSERT_EQ(0, motionArgs.edgeFlags);
6174 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6175 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6178 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6179 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6180 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6181 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6182
6183 // Keep moving out of bounds. Should generate a pointer move.
6184 y -= 50;
6185 processMove(mapper, x, y);
6186 processSync(mapper);
6187
6188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6189 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6190 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6191 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6192 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6193 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6194 ASSERT_EQ(0, motionArgs.flags);
6195 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6196 ASSERT_EQ(0, motionArgs.buttonState);
6197 ASSERT_EQ(0, motionArgs.edgeFlags);
6198 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6199 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6200 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6202 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6203 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6204 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6205 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6206
6207 // Release out of bounds. Should generate a pointer up.
6208 processUp(mapper);
6209 processSync(mapper);
6210
6211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6212 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6213 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6214 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6215 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6216 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6217 ASSERT_EQ(0, motionArgs.flags);
6218 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6219 ASSERT_EQ(0, motionArgs.buttonState);
6220 ASSERT_EQ(0, motionArgs.edgeFlags);
6221 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6222 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6223 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6225 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6226 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6227 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6228 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6229
6230 // Should not have sent any more keys or motions.
6231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6233}
6234
6235TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006236 addConfigurationProperty("touch.deviceType", "touchScreen");
6237 prepareDisplay(DISPLAY_ORIENTATION_0);
6238 prepareButtons();
6239 prepareAxes(POSITION);
6240 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006241 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006242
arthurhungdcef2dc2020-08-11 14:47:50 +08006243 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006244
6245 NotifyMotionArgs motionArgs;
6246
6247 // Initially go down out of bounds.
6248 int32_t x = -10;
6249 int32_t y = -10;
6250 processDown(mapper, x, y);
6251 processSync(mapper);
6252
6253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6254
6255 // Move into the display area. Should generate a pointer down.
6256 x = 50;
6257 y = 75;
6258 processMove(mapper, x, y);
6259 processSync(mapper);
6260
6261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6262 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6263 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
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), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6276 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6277 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6278 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6279
6280 // Release. Should generate a pointer up.
6281 processUp(mapper);
6282 processSync(mapper);
6283
6284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6285 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6286 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6287 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6288 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6289 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6290 ASSERT_EQ(0, motionArgs.flags);
6291 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6292 ASSERT_EQ(0, motionArgs.buttonState);
6293 ASSERT_EQ(0, motionArgs.edgeFlags);
6294 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6295 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6296 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6298 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6299 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6300 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6301 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6302
6303 // Should not have sent any more keys or motions.
6304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6306}
6307
Santos Cordonfa5cf462017-04-05 10:37:00 -07006308TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07006309 addConfigurationProperty("touch.deviceType", "touchScreen");
6310 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
6311
6312 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
6313 prepareButtons();
6314 prepareAxes(POSITION);
6315 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006316 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07006317
arthurhungdcef2dc2020-08-11 14:47:50 +08006318 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006319
6320 NotifyMotionArgs motionArgs;
6321
6322 // Down.
6323 int32_t x = 100;
6324 int32_t y = 125;
6325 processDown(mapper, x, y);
6326 processSync(mapper);
6327
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6329 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6330 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6331 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6332 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6333 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6334 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6335 ASSERT_EQ(0, motionArgs.flags);
6336 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6337 ASSERT_EQ(0, motionArgs.buttonState);
6338 ASSERT_EQ(0, motionArgs.edgeFlags);
6339 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6340 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6341 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6342 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6343 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6344 1, 0, 0, 0, 0, 0, 0, 0));
6345 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6346 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6347 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6348
6349 // Move.
6350 x += 50;
6351 y += 75;
6352 processMove(mapper, x, y);
6353 processSync(mapper);
6354
6355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6356 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6357 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6358 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6359 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6360 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, 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, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6371 1, 0, 0, 0, 0, 0, 0, 0));
6372 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6373 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6374 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6375
6376 // Up.
6377 processUp(mapper);
6378 processSync(mapper);
6379
6380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6381 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6382 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6383 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6384 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6385 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6386 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, 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, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6396 1, 0, 0, 0, 0, 0, 0, 0));
6397 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6398 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6399 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6400
6401 // Should not have sent any more keys or motions.
6402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6404}
6405
Michael Wrightd02c5b62014-02-10 15:10:22 -08006406TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006407 addConfigurationProperty("touch.deviceType", "touchScreen");
6408 prepareDisplay(DISPLAY_ORIENTATION_0);
6409 prepareButtons();
6410 prepareAxes(POSITION);
6411 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006412 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006413
arthurhungdcef2dc2020-08-11 14:47:50 +08006414 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006415
6416 NotifyMotionArgs motionArgs;
6417
6418 // Down.
6419 int32_t x = 100;
6420 int32_t y = 125;
6421 processDown(mapper, x, y);
6422 processSync(mapper);
6423
6424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6425 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6426 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6427 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6428 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6429 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6430 ASSERT_EQ(0, motionArgs.flags);
6431 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6432 ASSERT_EQ(0, motionArgs.buttonState);
6433 ASSERT_EQ(0, motionArgs.edgeFlags);
6434 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6435 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6436 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6437 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6438 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6439 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6440 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6441 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6442
6443 // Move.
6444 x += 50;
6445 y += 75;
6446 processMove(mapper, x, y);
6447 processSync(mapper);
6448
6449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6450 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6451 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6452 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6453 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6454 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6455 ASSERT_EQ(0, motionArgs.flags);
6456 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6457 ASSERT_EQ(0, motionArgs.buttonState);
6458 ASSERT_EQ(0, motionArgs.edgeFlags);
6459 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6460 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6461 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6462 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6463 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6464 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6465 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6466 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6467
6468 // Up.
6469 processUp(mapper);
6470 processSync(mapper);
6471
6472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6473 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6474 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6475 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6476 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6477 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6478 ASSERT_EQ(0, motionArgs.flags);
6479 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6480 ASSERT_EQ(0, motionArgs.buttonState);
6481 ASSERT_EQ(0, motionArgs.edgeFlags);
6482 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6483 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6484 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6486 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6487 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6488 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6489 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6490
6491 // Should not have sent any more keys or motions.
6492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6494}
6495
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006496TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006497 addConfigurationProperty("touch.deviceType", "touchScreen");
6498 prepareButtons();
6499 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006500 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
6501 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006502 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006503
6504 NotifyMotionArgs args;
6505
6506 // Rotation 90.
6507 prepareDisplay(DISPLAY_ORIENTATION_90);
6508 processDown(mapper, toRawX(50), toRawY(75));
6509 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 Pradhanc14266f2021-05-12 15:56:24 -07006520TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006521 addConfigurationProperty("touch.deviceType", "touchScreen");
6522 prepareButtons();
6523 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006524 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6525 // orientation-aware are affected by display rotation.
6526 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006527 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006528
6529 NotifyMotionArgs args;
6530
6531 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006532 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006533 prepareDisplay(DISPLAY_ORIENTATION_0);
6534 processDown(mapper, toRawX(50), toRawY(75));
6535 processSync(mapper);
6536
6537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6538 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6539 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6540
6541 processUp(mapper);
6542 processSync(mapper);
6543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6544
6545 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006546 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006547 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006548 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006549 processSync(mapper);
6550
6551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6552 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6553 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6554
6555 processUp(mapper);
6556 processSync(mapper);
6557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6558
6559 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006560 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006561 prepareDisplay(DISPLAY_ORIENTATION_180);
6562 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6563 processSync(mapper);
6564
6565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6566 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6567 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6568
6569 processUp(mapper);
6570 processSync(mapper);
6571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6572
6573 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006574 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006575 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006576 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006577 processSync(mapper);
6578
6579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6580 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6581 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6582
6583 processUp(mapper);
6584 processSync(mapper);
6585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6586}
6587
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006588TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6589 addConfigurationProperty("touch.deviceType", "touchScreen");
6590 prepareButtons();
6591 prepareAxes(POSITION);
6592 addConfigurationProperty("touch.orientationAware", "1");
6593 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6594 clearViewports();
6595 prepareDisplay(DISPLAY_ORIENTATION_0);
6596 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6597 NotifyMotionArgs args;
6598
6599 // Orientation 0.
6600 processDown(mapper, toRawX(50), toRawY(75));
6601 processSync(mapper);
6602
6603 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6604 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6605 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6606
6607 processUp(mapper);
6608 processSync(mapper);
6609 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6610}
6611
6612TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6613 addConfigurationProperty("touch.deviceType", "touchScreen");
6614 prepareButtons();
6615 prepareAxes(POSITION);
6616 addConfigurationProperty("touch.orientationAware", "1");
6617 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6618 clearViewports();
6619 prepareDisplay(DISPLAY_ORIENTATION_0);
6620 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6621 NotifyMotionArgs args;
6622
6623 // Orientation 90.
6624 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6625 processSync(mapper);
6626
6627 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6628 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6629 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6630
6631 processUp(mapper);
6632 processSync(mapper);
6633 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6634}
6635
6636TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6637 addConfigurationProperty("touch.deviceType", "touchScreen");
6638 prepareButtons();
6639 prepareAxes(POSITION);
6640 addConfigurationProperty("touch.orientationAware", "1");
6641 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6642 clearViewports();
6643 prepareDisplay(DISPLAY_ORIENTATION_0);
6644 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6645 NotifyMotionArgs args;
6646
6647 // Orientation 180.
6648 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6649 processSync(mapper);
6650
6651 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6652 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6653 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6654
6655 processUp(mapper);
6656 processSync(mapper);
6657 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6658}
6659
6660TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6661 addConfigurationProperty("touch.deviceType", "touchScreen");
6662 prepareButtons();
6663 prepareAxes(POSITION);
6664 addConfigurationProperty("touch.orientationAware", "1");
6665 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6666 clearViewports();
6667 prepareDisplay(DISPLAY_ORIENTATION_0);
6668 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6669 NotifyMotionArgs args;
6670
6671 // Orientation 270.
6672 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6673 processSync(mapper);
6674
6675 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6676 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6677 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6678
6679 processUp(mapper);
6680 processSync(mapper);
6681 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6682}
6683
6684TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6685 addConfigurationProperty("touch.deviceType", "touchScreen");
6686 prepareButtons();
6687 prepareAxes(POSITION);
6688 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6689 // orientation-aware are affected by display rotation.
6690 addConfigurationProperty("touch.orientationAware", "0");
6691 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6692 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6693
6694 NotifyMotionArgs args;
6695
6696 // Orientation 90, Rotation 0.
6697 clearViewports();
6698 prepareDisplay(DISPLAY_ORIENTATION_0);
6699 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6700 processSync(mapper);
6701
6702 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6703 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6704 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6705
6706 processUp(mapper);
6707 processSync(mapper);
6708 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6709
6710 // Orientation 90, Rotation 90.
6711 clearViewports();
6712 prepareDisplay(DISPLAY_ORIENTATION_90);
6713 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6714 processSync(mapper);
6715
6716 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6717 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6718 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6719
6720 processUp(mapper);
6721 processSync(mapper);
6722 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6723
6724 // Orientation 90, Rotation 180.
6725 clearViewports();
6726 prepareDisplay(DISPLAY_ORIENTATION_180);
6727 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6728 processSync(mapper);
6729
6730 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6731 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6732 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6733
6734 processUp(mapper);
6735 processSync(mapper);
6736 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6737
6738 // Orientation 90, Rotation 270.
6739 clearViewports();
6740 prepareDisplay(DISPLAY_ORIENTATION_270);
6741 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6742 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6743 processSync(mapper);
6744
6745 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6746 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6747 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6748
6749 processUp(mapper);
6750 processSync(mapper);
6751 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6752}
6753
Michael Wrightd02c5b62014-02-10 15:10:22 -08006754TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006755 addConfigurationProperty("touch.deviceType", "touchScreen");
6756 prepareDisplay(DISPLAY_ORIENTATION_0);
6757 prepareButtons();
6758 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006759 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006760
6761 // These calculations are based on the input device calibration documentation.
6762 int32_t rawX = 100;
6763 int32_t rawY = 200;
6764 int32_t rawPressure = 10;
6765 int32_t rawToolMajor = 12;
6766 int32_t rawDistance = 2;
6767 int32_t rawTiltX = 30;
6768 int32_t rawTiltY = 110;
6769
6770 float x = toDisplayX(rawX);
6771 float y = toDisplayY(rawY);
6772 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6773 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6774 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6775 float distance = float(rawDistance);
6776
6777 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6778 float tiltScale = M_PI / 180;
6779 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6780 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6781 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6782 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6783
6784 processDown(mapper, rawX, rawY);
6785 processPressure(mapper, rawPressure);
6786 processToolMajor(mapper, rawToolMajor);
6787 processDistance(mapper, rawDistance);
6788 processTilt(mapper, rawTiltX, rawTiltY);
6789 processSync(mapper);
6790
6791 NotifyMotionArgs args;
6792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6794 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6795 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6796}
6797
Jason Gerecke489fda82012-09-07 17:19:40 -07006798TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006799 addConfigurationProperty("touch.deviceType", "touchScreen");
6800 prepareDisplay(DISPLAY_ORIENTATION_0);
6801 prepareLocationCalibration();
6802 prepareButtons();
6803 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006804 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006805
6806 int32_t rawX = 100;
6807 int32_t rawY = 200;
6808
6809 float x = toDisplayX(toCookedX(rawX, rawY));
6810 float y = toDisplayY(toCookedY(rawX, rawY));
6811
6812 processDown(mapper, rawX, rawY);
6813 processSync(mapper);
6814
6815 NotifyMotionArgs args;
6816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6818 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6819}
6820
Michael Wrightd02c5b62014-02-10 15:10:22 -08006821TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006822 addConfigurationProperty("touch.deviceType", "touchScreen");
6823 prepareDisplay(DISPLAY_ORIENTATION_0);
6824 prepareButtons();
6825 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006826 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006827
6828 NotifyMotionArgs motionArgs;
6829 NotifyKeyArgs keyArgs;
6830
6831 processDown(mapper, 100, 200);
6832 processSync(mapper);
6833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6834 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6835 ASSERT_EQ(0, motionArgs.buttonState);
6836
6837 // press BTN_LEFT, release BTN_LEFT
6838 processKey(mapper, BTN_LEFT, 1);
6839 processSync(mapper);
6840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6841 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6842 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6843
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6845 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6846 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6847
Michael Wrightd02c5b62014-02-10 15:10:22 -08006848 processKey(mapper, BTN_LEFT, 0);
6849 processSync(mapper);
6850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006851 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006852 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006853
6854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006855 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006856 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006857
6858 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6859 processKey(mapper, BTN_RIGHT, 1);
6860 processKey(mapper, BTN_MIDDLE, 1);
6861 processSync(mapper);
6862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6863 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6864 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6865 motionArgs.buttonState);
6866
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6868 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6869 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6870
6871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6872 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6873 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6874 motionArgs.buttonState);
6875
Michael Wrightd02c5b62014-02-10 15:10:22 -08006876 processKey(mapper, BTN_RIGHT, 0);
6877 processSync(mapper);
6878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006879 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006880 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006881
6882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006883 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006884 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006885
6886 processKey(mapper, BTN_MIDDLE, 0);
6887 processSync(mapper);
6888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006889 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006890 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006891
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006893 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006894 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006895
6896 // press BTN_BACK, release BTN_BACK
6897 processKey(mapper, BTN_BACK, 1);
6898 processSync(mapper);
6899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6900 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6901 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006902
Michael Wrightd02c5b62014-02-10 15:10:22 -08006903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006904 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006905 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6906
6907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6908 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6909 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006910
6911 processKey(mapper, BTN_BACK, 0);
6912 processSync(mapper);
6913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006914 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006915 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006916
6917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006918 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006919 ASSERT_EQ(0, motionArgs.buttonState);
6920
Michael Wrightd02c5b62014-02-10 15:10:22 -08006921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6922 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6923 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6924
6925 // press BTN_SIDE, release BTN_SIDE
6926 processKey(mapper, BTN_SIDE, 1);
6927 processSync(mapper);
6928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6929 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6930 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006931
Michael Wrightd02c5b62014-02-10 15:10:22 -08006932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006933 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006934 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6935
6936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6937 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6938 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006939
6940 processKey(mapper, BTN_SIDE, 0);
6941 processSync(mapper);
6942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006943 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006944 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006945
6946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006947 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006948 ASSERT_EQ(0, motionArgs.buttonState);
6949
Michael Wrightd02c5b62014-02-10 15:10:22 -08006950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6951 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6952 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6953
6954 // press BTN_FORWARD, release BTN_FORWARD
6955 processKey(mapper, BTN_FORWARD, 1);
6956 processSync(mapper);
6957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6958 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6959 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006960
Michael Wrightd02c5b62014-02-10 15:10:22 -08006961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006962 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006963 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6964
6965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6966 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6967 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006968
6969 processKey(mapper, BTN_FORWARD, 0);
6970 processSync(mapper);
6971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006972 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006973 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006974
6975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006976 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006977 ASSERT_EQ(0, motionArgs.buttonState);
6978
Michael Wrightd02c5b62014-02-10 15:10:22 -08006979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6980 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6981 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6982
6983 // press BTN_EXTRA, release BTN_EXTRA
6984 processKey(mapper, BTN_EXTRA, 1);
6985 processSync(mapper);
6986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6987 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6988 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006989
Michael Wrightd02c5b62014-02-10 15:10:22 -08006990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006991 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006992 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6993
6994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6995 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6996 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006997
6998 processKey(mapper, BTN_EXTRA, 0);
6999 processSync(mapper);
7000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007001 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007002 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007003
7004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007005 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007006 ASSERT_EQ(0, motionArgs.buttonState);
7007
Michael Wrightd02c5b62014-02-10 15:10:22 -08007008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7009 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7010 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7011
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7013
Michael Wrightd02c5b62014-02-10 15:10:22 -08007014 // press BTN_STYLUS, release BTN_STYLUS
7015 processKey(mapper, BTN_STYLUS, 1);
7016 processSync(mapper);
7017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7018 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007019 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7020
7021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7022 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7023 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007024
7025 processKey(mapper, BTN_STYLUS, 0);
7026 processSync(mapper);
7027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007028 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007029 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007030
7031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007032 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007033 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007034
7035 // press BTN_STYLUS2, release BTN_STYLUS2
7036 processKey(mapper, BTN_STYLUS2, 1);
7037 processSync(mapper);
7038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7039 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007040 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7041
7042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7043 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7044 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007045
7046 processKey(mapper, BTN_STYLUS2, 0);
7047 processSync(mapper);
7048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007049 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007050 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007051
7052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007053 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007054 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007055
7056 // release touch
7057 processUp(mapper);
7058 processSync(mapper);
7059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7060 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7061 ASSERT_EQ(0, motionArgs.buttonState);
7062}
7063
7064TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007065 addConfigurationProperty("touch.deviceType", "touchScreen");
7066 prepareDisplay(DISPLAY_ORIENTATION_0);
7067 prepareButtons();
7068 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007069 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007070
7071 NotifyMotionArgs motionArgs;
7072
7073 // default tool type is finger
7074 processDown(mapper, 100, 200);
7075 processSync(mapper);
7076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7077 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7078 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7079
7080 // eraser
7081 processKey(mapper, BTN_TOOL_RUBBER, 1);
7082 processSync(mapper);
7083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7084 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7085 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7086
7087 // stylus
7088 processKey(mapper, BTN_TOOL_RUBBER, 0);
7089 processKey(mapper, BTN_TOOL_PEN, 1);
7090 processSync(mapper);
7091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7092 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7093 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7094
7095 // brush
7096 processKey(mapper, BTN_TOOL_PEN, 0);
7097 processKey(mapper, BTN_TOOL_BRUSH, 1);
7098 processSync(mapper);
7099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7102
7103 // pencil
7104 processKey(mapper, BTN_TOOL_BRUSH, 0);
7105 processKey(mapper, BTN_TOOL_PENCIL, 1);
7106 processSync(mapper);
7107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7109 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7110
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007111 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007112 processKey(mapper, BTN_TOOL_PENCIL, 0);
7113 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7114 processSync(mapper);
7115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7117 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7118
7119 // mouse
7120 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7121 processKey(mapper, BTN_TOOL_MOUSE, 1);
7122 processSync(mapper);
7123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7124 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7125 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7126
7127 // lens
7128 processKey(mapper, BTN_TOOL_MOUSE, 0);
7129 processKey(mapper, BTN_TOOL_LENS, 1);
7130 processSync(mapper);
7131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7132 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7133 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7134
7135 // double-tap
7136 processKey(mapper, BTN_TOOL_LENS, 0);
7137 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7138 processSync(mapper);
7139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7141 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7142
7143 // triple-tap
7144 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7145 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7146 processSync(mapper);
7147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7148 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7149 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7150
7151 // quad-tap
7152 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7153 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7154 processSync(mapper);
7155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7157 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7158
7159 // finger
7160 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7161 processKey(mapper, BTN_TOOL_FINGER, 1);
7162 processSync(mapper);
7163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7164 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7166
7167 // stylus trumps finger
7168 processKey(mapper, BTN_TOOL_PEN, 1);
7169 processSync(mapper);
7170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7171 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7172 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7173
7174 // eraser trumps stylus
7175 processKey(mapper, BTN_TOOL_RUBBER, 1);
7176 processSync(mapper);
7177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7178 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7179 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7180
7181 // mouse trumps eraser
7182 processKey(mapper, BTN_TOOL_MOUSE, 1);
7183 processSync(mapper);
7184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7185 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7186 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7187
7188 // back to default tool type
7189 processKey(mapper, BTN_TOOL_MOUSE, 0);
7190 processKey(mapper, BTN_TOOL_RUBBER, 0);
7191 processKey(mapper, BTN_TOOL_PEN, 0);
7192 processKey(mapper, BTN_TOOL_FINGER, 0);
7193 processSync(mapper);
7194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7195 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7196 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7197}
7198
7199TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007200 addConfigurationProperty("touch.deviceType", "touchScreen");
7201 prepareDisplay(DISPLAY_ORIENTATION_0);
7202 prepareButtons();
7203 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007204 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007205 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007206
7207 NotifyMotionArgs motionArgs;
7208
7209 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7210 processKey(mapper, BTN_TOOL_FINGER, 1);
7211 processMove(mapper, 100, 200);
7212 processSync(mapper);
7213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7214 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7215 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7216 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7217
7218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7219 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7220 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7221 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7222
7223 // move a little
7224 processMove(mapper, 150, 250);
7225 processSync(mapper);
7226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7227 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7229 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7230
7231 // down when BTN_TOUCH is pressed, pressure defaults to 1
7232 processKey(mapper, BTN_TOUCH, 1);
7233 processSync(mapper);
7234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7235 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7237 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7238
7239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7240 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7242 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7243
7244 // up when BTN_TOUCH is released, hover restored
7245 processKey(mapper, BTN_TOUCH, 0);
7246 processSync(mapper);
7247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7248 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7250 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7251
7252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7253 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7254 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7255 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7256
7257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7258 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7260 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7261
7262 // exit hover when pointer goes away
7263 processKey(mapper, BTN_TOOL_FINGER, 0);
7264 processSync(mapper);
7265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7266 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7267 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7268 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7269}
7270
7271TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007272 addConfigurationProperty("touch.deviceType", "touchScreen");
7273 prepareDisplay(DISPLAY_ORIENTATION_0);
7274 prepareButtons();
7275 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007276 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007277
7278 NotifyMotionArgs motionArgs;
7279
7280 // initially hovering because pressure is 0
7281 processDown(mapper, 100, 200);
7282 processPressure(mapper, 0);
7283 processSync(mapper);
7284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7285 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7287 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7288
7289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7290 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7292 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7293
7294 // move a little
7295 processMove(mapper, 150, 250);
7296 processSync(mapper);
7297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7298 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7300 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7301
7302 // down when pressure is non-zero
7303 processPressure(mapper, RAW_PRESSURE_MAX);
7304 processSync(mapper);
7305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7306 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7308 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7309
7310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7311 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7313 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7314
7315 // up when pressure becomes 0, hover restored
7316 processPressure(mapper, 0);
7317 processSync(mapper);
7318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7319 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7321 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7322
7323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7324 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7326 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7327
7328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7329 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7331 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7332
7333 // exit hover when pointer goes away
7334 processUp(mapper);
7335 processSync(mapper);
7336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7337 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7339 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7340}
7341
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007342TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
7343 addConfigurationProperty("touch.deviceType", "touchScreen");
7344 prepareDisplay(DISPLAY_ORIENTATION_0);
7345 prepareButtons();
7346 prepareAxes(POSITION | PRESSURE);
7347 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7348
7349 // Touch down.
7350 processDown(mapper, 100, 200);
7351 processPressure(mapper, 1);
7352 processSync(mapper);
7353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7354 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
7355
7356 // Reset the mapper. This should cancel the ongoing gesture.
7357 resetMapper(mapper, ARBITRARY_TIME);
7358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7359 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
7360
7361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7362}
7363
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007364TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
7365 addConfigurationProperty("touch.deviceType", "touchScreen");
7366 prepareDisplay(DISPLAY_ORIENTATION_0);
7367 prepareButtons();
7368 prepareAxes(POSITION | PRESSURE);
7369 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7370
7371 // Set the initial state for the touch pointer.
7372 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
7373 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
7374 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
7375 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7376
7377 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007378 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
7379 // does not generate any events.
7380 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007381
7382 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
7383 // the recreated touch state to generate a down event.
7384 processSync(mapper);
7385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7386 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
7387
7388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7389}
7390
lilinnan687e58f2022-07-19 16:00:50 +08007391TEST_F(SingleTouchInputMapperTest,
7392 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
7393 addConfigurationProperty("touch.deviceType", "touchScreen");
7394 prepareDisplay(DISPLAY_ORIENTATION_0);
7395 prepareButtons();
7396 prepareAxes(POSITION);
7397 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7398 NotifyMotionArgs motionArgs;
7399
7400 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00007401 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08007402 processSync(mapper);
7403
7404 // We should receive a down event
7405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7406 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7407
7408 // Change display id
7409 clearViewports();
7410 prepareSecondaryDisplay(ViewportType::INTERNAL);
7411
7412 // We should receive a cancel event
7413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7414 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7415 // Then receive reset called
7416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7417}
7418
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007419TEST_F(SingleTouchInputMapperTest,
7420 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
7421 addConfigurationProperty("touch.deviceType", "touchScreen");
7422 prepareDisplay(DISPLAY_ORIENTATION_0);
7423 prepareButtons();
7424 prepareAxes(POSITION);
7425 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7427 NotifyMotionArgs motionArgs;
7428
7429 // Start a new gesture.
7430 processDown(mapper, 100, 200);
7431 processSync(mapper);
7432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7433 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7434
7435 // Make the viewport inactive. This will put the device in disabled mode.
7436 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7437 viewport->isActive = false;
7438 mFakePolicy->updateViewport(*viewport);
7439 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7440
7441 // We should receive a cancel event for the ongoing gesture.
7442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7443 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7444 // Then we should be notified that the device was reset.
7445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7446
7447 // No events are generated while the viewport is inactive.
7448 processMove(mapper, 101, 201);
7449 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007450 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007451 processSync(mapper);
7452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7453
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007454 // Start a new gesture while the viewport is still inactive.
7455 processDown(mapper, 300, 400);
7456 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
7457 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
7458 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7459 processSync(mapper);
7460
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007461 // Make the viewport active again. The device should resume processing events.
7462 viewport->isActive = true;
7463 mFakePolicy->updateViewport(*viewport);
7464 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7465
7466 // The device is reset because it changes back to direct mode, without generating any events.
7467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7469
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007470 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007471 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7473 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007474
7475 // No more events.
7476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
7478}
7479
Prabir Pradhan211ba622022-10-31 21:09:21 +00007480TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
7481 addConfigurationProperty("touch.deviceType", "touchScreen");
7482 prepareDisplay(DISPLAY_ORIENTATION_0);
7483 prepareButtons();
7484 prepareAxes(POSITION);
7485 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7487
7488 // Press a stylus button.
7489 processKey(mapper, BTN_STYLUS, 1);
7490 processSync(mapper);
7491
7492 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
7493 processDown(mapper, 100, 200);
7494 processSync(mapper);
7495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7496 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7497 WithCoords(toDisplayX(100), toDisplayY(200)),
7498 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7500 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7501 WithCoords(toDisplayX(100), toDisplayY(200)),
7502 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7503
7504 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
7505 // the button has not actually been released, since there will be no pointers through which the
7506 // button state can be reported. The event is generated at the location of the pointer before
7507 // it went up.
7508 processUp(mapper);
7509 processSync(mapper);
7510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7511 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7512 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7514 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7515 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7516}
7517
Prabir Pradhan5632d622021-09-06 07:57:20 -07007518// --- TouchDisplayProjectionTest ---
7519
7520class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7521public:
7522 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7523 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7524 // rotated equivalent of the given un-rotated physical display bounds.
7525 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
7526 uint32_t inverseRotationFlags;
7527 auto width = DISPLAY_WIDTH;
7528 auto height = DISPLAY_HEIGHT;
7529 switch (orientation) {
7530 case DISPLAY_ORIENTATION_90:
7531 inverseRotationFlags = ui::Transform::ROT_270;
7532 std::swap(width, height);
7533 break;
7534 case DISPLAY_ORIENTATION_180:
7535 inverseRotationFlags = ui::Transform::ROT_180;
7536 break;
7537 case DISPLAY_ORIENTATION_270:
7538 inverseRotationFlags = ui::Transform::ROT_90;
7539 std::swap(width, height);
7540 break;
7541 case DISPLAY_ORIENTATION_0:
7542 inverseRotationFlags = ui::Transform::ROT_0;
7543 break;
7544 default:
7545 FAIL() << "Invalid orientation: " << orientation;
7546 }
7547
7548 const ui::Transform rotation(inverseRotationFlags, width, height);
7549 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7550
7551 std::optional<DisplayViewport> internalViewport =
7552 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7553 DisplayViewport& v = *internalViewport;
7554 v.displayId = DISPLAY_ID;
7555 v.orientation = orientation;
7556
7557 v.logicalLeft = 0;
7558 v.logicalTop = 0;
7559 v.logicalRight = 100;
7560 v.logicalBottom = 100;
7561
7562 v.physicalLeft = rotatedPhysicalDisplay.left;
7563 v.physicalTop = rotatedPhysicalDisplay.top;
7564 v.physicalRight = rotatedPhysicalDisplay.right;
7565 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7566
7567 v.deviceWidth = width;
7568 v.deviceHeight = height;
7569
7570 v.isActive = true;
7571 v.uniqueId = UNIQUE_ID;
7572 v.type = ViewportType::INTERNAL;
7573 mFakePolicy->updateViewport(v);
7574 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7575 }
7576
7577 void assertReceivedMove(const Point& point) {
7578 NotifyMotionArgs motionArgs;
7579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7581 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7583 1, 0, 0, 0, 0, 0, 0, 0));
7584 }
7585};
7586
7587TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7588 addConfigurationProperty("touch.deviceType", "touchScreen");
7589 prepareDisplay(DISPLAY_ORIENTATION_0);
7590
7591 prepareButtons();
7592 prepareAxes(POSITION);
7593 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7594
7595 NotifyMotionArgs motionArgs;
7596
7597 // Configure the DisplayViewport such that the logical display maps to a subsection of
7598 // the display panel called the physical display. Here, the physical display is bounded by the
7599 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7600 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7601 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7602 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7603
7604 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7605 DISPLAY_ORIENTATION_270}) {
7606 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7607
7608 // Touches outside the physical display should be ignored, and should not generate any
7609 // events. Ensure touches at the following points that lie outside of the physical display
7610 // area do not generate any events.
7611 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7612 processDown(mapper, toRawX(point.x), toRawY(point.y));
7613 processSync(mapper);
7614 processUp(mapper);
7615 processSync(mapper);
7616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7617 << "Unexpected event generated for touch outside physical display at point: "
7618 << point.x << ", " << point.y;
7619 }
7620 }
7621}
7622
7623TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7624 addConfigurationProperty("touch.deviceType", "touchScreen");
7625 prepareDisplay(DISPLAY_ORIENTATION_0);
7626
7627 prepareButtons();
7628 prepareAxes(POSITION);
7629 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7630
7631 NotifyMotionArgs motionArgs;
7632
7633 // Configure the DisplayViewport such that the logical display maps to a subsection of
7634 // the display panel called the physical display. Here, the physical display is bounded by the
7635 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7636 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7637
7638 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7639 DISPLAY_ORIENTATION_270}) {
7640 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7641
7642 // Touches that start outside the physical display should be ignored until it enters the
7643 // physical display bounds, at which point it should generate a down event. Start a touch at
7644 // the point (5, 100), which is outside the physical display bounds.
7645 static const Point kOutsidePoint{5, 100};
7646 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7647 processSync(mapper);
7648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7649
7650 // Move the touch into the physical display area. This should generate a pointer down.
7651 processMove(mapper, toRawX(11), toRawY(21));
7652 processSync(mapper);
7653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7654 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7655 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7656 ASSERT_NO_FATAL_FAILURE(
7657 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7658
7659 // Move the touch inside the physical display area. This should generate a pointer move.
7660 processMove(mapper, toRawX(69), toRawY(159));
7661 processSync(mapper);
7662 assertReceivedMove({69, 159});
7663
7664 // Move outside the physical display area. Since the pointer is already down, this should
7665 // now continue generating events.
7666 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7667 processSync(mapper);
7668 assertReceivedMove(kOutsidePoint);
7669
7670 // Release. This should generate a pointer up.
7671 processUp(mapper);
7672 processSync(mapper);
7673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7674 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7676 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7677
7678 // Ensure no more events were generated.
7679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7681 }
7682}
7683
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007684// --- ExternalStylusFusionTest ---
7685
7686class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7687public:
7688 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7689 addConfigurationProperty("touch.deviceType", "touchScreen");
7690 prepareDisplay(DISPLAY_ORIENTATION_0);
7691 prepareButtons();
7692 prepareAxes(POSITION);
7693 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7694
7695 mStylusState.when = ARBITRARY_TIME;
7696 mStylusState.pressure = 0.f;
7697 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7698 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
7699 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
7700 processExternalStylusState(mapper);
7701 return mapper;
7702 }
7703
7704 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7705 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7706 for (const NotifyArgs& args : generatedArgs) {
7707 mFakeListener->notify(args);
7708 }
7709 // Loop the reader to flush the input listener queue.
7710 mReader->loopOnce();
7711 return generatedArgs;
7712 }
7713
7714protected:
7715 StylusState mStylusState{};
7716 static constexpr uint32_t EXPECTED_SOURCE =
7717 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7718
7719 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7720 auto toolTypeSource =
7721 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7722
7723 // The first pointer is withheld.
7724 processDown(mapper, 100, 200);
7725 processSync(mapper);
7726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7727 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7728 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7729
7730 // The external stylus reports pressure. The withheld finger pointer is released as a
7731 // stylus.
7732 mStylusState.pressure = 1.f;
7733 processExternalStylusState(mapper);
7734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7735 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7736 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7737
7738 // Subsequent pointer events are not withheld.
7739 processMove(mapper, 101, 201);
7740 processSync(mapper);
7741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7742 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7743
7744 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7746 }
7747
7748 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7749 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7750
7751 // Releasing the touch pointer ends the gesture.
7752 processUp(mapper);
7753 processSync(mapper);
7754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7755 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7756 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7757
7758 mStylusState.pressure = 0.f;
7759 processExternalStylusState(mapper);
7760 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7762 }
7763
7764 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7765 auto toolTypeSource =
7766 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER));
7767
7768 // The first pointer is withheld when an external stylus is connected,
7769 // and a timeout is requested.
7770 processDown(mapper, 100, 200);
7771 processSync(mapper);
7772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7773 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7774 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7775
7776 // If the timeout expires early, it is requested again.
7777 handleTimeout(mapper, ARBITRARY_TIME + 1);
7778 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7779 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7780
7781 // When the timeout expires, the withheld touch is released as a finger pointer.
7782 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7784 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7785
7786 // Subsequent pointer events are not withheld.
7787 processMove(mapper, 101, 201);
7788 processSync(mapper);
7789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7790 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7791 processUp(mapper);
7792 processSync(mapper);
7793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7794 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7795
7796 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7798 }
7799
7800private:
7801 InputDeviceInfo mExternalStylusDeviceInfo{};
7802};
7803
7804TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7805 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7806 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7807}
7808
7809TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7810 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7811 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7812}
7813
7814TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7815 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7816 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7817}
7818
7819// Test a successful stylus fusion gesture where the pressure is reported by the external
7820// before the touch is reported by the touchscreen.
7821TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7822 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7823 auto toolTypeSource =
7824 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7825
7826 // The external stylus reports pressure first. It is ignored for now.
7827 mStylusState.pressure = 1.f;
7828 processExternalStylusState(mapper);
7829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7830 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7831
7832 // When the touch goes down afterwards, it is reported as a stylus pointer.
7833 processDown(mapper, 100, 200);
7834 processSync(mapper);
7835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7836 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7837 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7838
7839 processMove(mapper, 101, 201);
7840 processSync(mapper);
7841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7842 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7843 processUp(mapper);
7844 processSync(mapper);
7845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7846 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7847
7848 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7850}
7851
7852TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7853 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7854
7855 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7856 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7857
7858 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7859 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7860 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7861 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7862}
7863
7864TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7865 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7866 auto toolTypeSource =
7867 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7868
7869 mStylusState.pressure = 0.8f;
7870 processExternalStylusState(mapper);
7871 processDown(mapper, 100, 200);
7872 processSync(mapper);
7873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7874 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7875 WithPressure(0.8f))));
7876 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7877
7878 // The external stylus reports a pressure change. We wait for some time for a touch event.
7879 mStylusState.pressure = 0.6f;
7880 processExternalStylusState(mapper);
7881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7882 ASSERT_NO_FATAL_FAILURE(
7883 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7884
7885 // If a touch is reported within the timeout, it reports the updated pressure.
7886 processMove(mapper, 101, 201);
7887 processSync(mapper);
7888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7889 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7890 WithPressure(0.6f))));
7891 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7892
7893 // There is another pressure change.
7894 mStylusState.pressure = 0.5f;
7895 processExternalStylusState(mapper);
7896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7897 ASSERT_NO_FATAL_FAILURE(
7898 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7899
7900 // If a touch is not reported within the timeout, a move event is generated to report
7901 // the new pressure.
7902 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7904 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7905 WithPressure(0.5f))));
7906
7907 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7908 // repeated indefinitely.
7909 mStylusState.pressure = 0.0f;
7910 processExternalStylusState(mapper);
7911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7912 ASSERT_NO_FATAL_FAILURE(
7913 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7914 processMove(mapper, 102, 202);
7915 processSync(mapper);
7916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7917 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7918 WithPressure(0.5f))));
7919 processMove(mapper, 103, 203);
7920 processSync(mapper);
7921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7922 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7923 WithPressure(0.5f))));
7924
7925 processUp(mapper);
7926 processSync(mapper);
7927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7928 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7929 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7930
7931 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7933}
7934
7935TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7936 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7937 auto source = WithSource(EXPECTED_SOURCE);
7938
7939 mStylusState.pressure = 1.f;
7940 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_ERASER;
7941 processExternalStylusState(mapper);
7942 processDown(mapper, 100, 200);
7943 processSync(mapper);
7944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7945 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7946 WithToolType(AMOTION_EVENT_TOOL_TYPE_ERASER))));
7947 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7948
7949 // The external stylus reports a tool change. We wait for some time for a touch event.
7950 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7951 processExternalStylusState(mapper);
7952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7953 ASSERT_NO_FATAL_FAILURE(
7954 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7955
7956 // If a touch is reported within the timeout, it reports the updated pressure.
7957 processMove(mapper, 101, 201);
7958 processSync(mapper);
7959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7960 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7961 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7962 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7963
7964 // There is another tool type change.
7965 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
7966 processExternalStylusState(mapper);
7967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7968 ASSERT_NO_FATAL_FAILURE(
7969 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7970
7971 // If a touch is not reported within the timeout, a move event is generated to report
7972 // the new tool type.
7973 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7975 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7976 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7977
7978 processUp(mapper);
7979 processSync(mapper);
7980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7981 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
7982 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7983
7984 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7986}
7987
7988TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7989 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7990 auto toolTypeSource =
7991 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7992
7993 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7994
7995 // The external stylus reports a button change. We wait for some time for a touch event.
7996 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7997 processExternalStylusState(mapper);
7998 ASSERT_NO_FATAL_FAILURE(
7999 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8000
8001 // If a touch is reported within the timeout, it reports the updated button state.
8002 processMove(mapper, 101, 201);
8003 processSync(mapper);
8004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8005 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8006 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8008 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8009 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8010 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8011
8012 // The button is now released.
8013 mStylusState.buttons = 0;
8014 processExternalStylusState(mapper);
8015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8016 ASSERT_NO_FATAL_FAILURE(
8017 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8018
8019 // If a touch is not reported within the timeout, a move event is generated to report
8020 // the new button state.
8021 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8023 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
8024 WithButtonState(0))));
8025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00008026 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8027 WithButtonState(0))));
8028
8029 processUp(mapper);
8030 processSync(mapper);
8031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008032 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8033
8034 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8036}
8037
Michael Wrightd02c5b62014-02-10 15:10:22 -08008038// --- MultiTouchInputMapperTest ---
8039
8040class MultiTouchInputMapperTest : public TouchInputMapperTest {
8041protected:
8042 void prepareAxes(int axes);
8043
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008044 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
8045 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
8046 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
8047 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
8048 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
8049 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
8050 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
8051 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
8052 void processId(MultiTouchInputMapper& mapper, int32_t id);
8053 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
8054 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
8055 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008056 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008057 void processMTSync(MultiTouchInputMapper& mapper);
8058 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008059};
8060
8061void MultiTouchInputMapperTest::prepareAxes(int axes) {
8062 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008063 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
8064 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008065 }
8066 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008067 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
8068 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008069 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008070 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
8071 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072 }
8073 }
8074 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008075 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8076 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008077 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008078 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008079 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008080 }
8081 }
8082 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008083 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
8084 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008085 }
8086 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008087 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
8088 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008089 }
8090 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008091 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
8092 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008093 }
8094 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008095 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
8096 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008097 }
8098 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008099 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
8100 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008101 }
8102 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008103 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008104 }
8105}
8106
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008107void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
8108 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008109 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
8110 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008111}
8112
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008113void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
8114 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008115 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008116}
8117
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008118void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
8119 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008121}
8122
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008123void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008125}
8126
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008127void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008128 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008129}
8130
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008131void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
8132 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008134}
8135
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008136void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008138}
8139
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008140void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008142}
8143
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008144void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008146}
8147
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008148void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008150}
8151
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008152void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008154}
8155
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008156void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
8157 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008159}
8160
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008161void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
8162 int32_t value) {
8163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
8164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
8165}
8166
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008167void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008169}
8170
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008171void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008173}
8174
Michael Wrightd02c5b62014-02-10 15:10:22 -08008175TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008176 addConfigurationProperty("touch.deviceType", "touchScreen");
8177 prepareDisplay(DISPLAY_ORIENTATION_0);
8178 prepareAxes(POSITION);
8179 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008180 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008181
arthurhungdcef2dc2020-08-11 14:47:50 +08008182 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008183
8184 NotifyMotionArgs motionArgs;
8185
8186 // Two fingers down at once.
8187 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8188 processPosition(mapper, x1, y1);
8189 processMTSync(mapper);
8190 processPosition(mapper, x2, y2);
8191 processMTSync(mapper);
8192 processSync(mapper);
8193
8194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8195 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8196 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8197 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8198 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8199 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8200 ASSERT_EQ(0, motionArgs.flags);
8201 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8202 ASSERT_EQ(0, motionArgs.buttonState);
8203 ASSERT_EQ(0, motionArgs.edgeFlags);
8204 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8205 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8206 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8207 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8208 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8209 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8210 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8211 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8212
8213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8214 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8215 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8216 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8217 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008218 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008219 ASSERT_EQ(0, motionArgs.flags);
8220 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8221 ASSERT_EQ(0, motionArgs.buttonState);
8222 ASSERT_EQ(0, motionArgs.edgeFlags);
8223 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8224 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8225 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8226 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8227 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8229 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8230 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8231 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8232 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8233 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8234 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8235
8236 // Move.
8237 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8238 processPosition(mapper, x1, y1);
8239 processMTSync(mapper);
8240 processPosition(mapper, x2, y2);
8241 processMTSync(mapper);
8242 processSync(mapper);
8243
8244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8245 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8246 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8247 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8248 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8250 ASSERT_EQ(0, motionArgs.flags);
8251 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8252 ASSERT_EQ(0, motionArgs.buttonState);
8253 ASSERT_EQ(0, motionArgs.edgeFlags);
8254 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8255 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8256 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8257 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8258 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8260 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8262 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8263 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8264 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8265 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8266
8267 // First finger up.
8268 x2 += 15; y2 -= 20;
8269 processPosition(mapper, x2, y2);
8270 processMTSync(mapper);
8271 processSync(mapper);
8272
8273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8274 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8275 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8276 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8277 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008278 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008279 ASSERT_EQ(0, motionArgs.flags);
8280 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8281 ASSERT_EQ(0, motionArgs.buttonState);
8282 ASSERT_EQ(0, motionArgs.edgeFlags);
8283 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8284 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8285 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8286 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8287 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8289 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8291 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8292 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8293 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8294 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8295
8296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8297 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8298 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8299 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8300 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8301 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8302 ASSERT_EQ(0, motionArgs.flags);
8303 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8304 ASSERT_EQ(0, motionArgs.buttonState);
8305 ASSERT_EQ(0, motionArgs.edgeFlags);
8306 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8307 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8308 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8309 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8310 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8311 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8312 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8313 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8314
8315 // Move.
8316 x2 += 20; y2 -= 25;
8317 processPosition(mapper, x2, y2);
8318 processMTSync(mapper);
8319 processSync(mapper);
8320
8321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8322 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8323 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8324 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8325 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8327 ASSERT_EQ(0, motionArgs.flags);
8328 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8329 ASSERT_EQ(0, motionArgs.buttonState);
8330 ASSERT_EQ(0, motionArgs.edgeFlags);
8331 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8332 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8333 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8334 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8335 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8336 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8337 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8338 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8339
8340 // New finger down.
8341 int32_t x3 = 700, y3 = 300;
8342 processPosition(mapper, x2, y2);
8343 processMTSync(mapper);
8344 processPosition(mapper, x3, y3);
8345 processMTSync(mapper);
8346 processSync(mapper);
8347
8348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8349 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8350 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8351 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8352 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008353 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008354 ASSERT_EQ(0, motionArgs.flags);
8355 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8356 ASSERT_EQ(0, motionArgs.buttonState);
8357 ASSERT_EQ(0, motionArgs.edgeFlags);
8358 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8359 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8360 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8361 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8362 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8364 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8366 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8367 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8368 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8369 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8370
8371 // Second finger up.
8372 x3 += 30; y3 -= 20;
8373 processPosition(mapper, x3, y3);
8374 processMTSync(mapper);
8375 processSync(mapper);
8376
8377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8378 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8379 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8380 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8381 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008382 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008383 ASSERT_EQ(0, motionArgs.flags);
8384 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8385 ASSERT_EQ(0, motionArgs.buttonState);
8386 ASSERT_EQ(0, motionArgs.edgeFlags);
8387 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8388 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8389 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8390 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8391 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8393 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8395 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8396 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8397 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8398 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8399
8400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8401 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8402 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8403 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8404 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8405 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8406 ASSERT_EQ(0, motionArgs.flags);
8407 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8408 ASSERT_EQ(0, motionArgs.buttonState);
8409 ASSERT_EQ(0, motionArgs.edgeFlags);
8410 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8411 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8412 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8414 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8415 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8416 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8417 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8418
8419 // Last finger up.
8420 processMTSync(mapper);
8421 processSync(mapper);
8422
8423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8424 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8425 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8426 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8427 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8428 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8429 ASSERT_EQ(0, motionArgs.flags);
8430 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8431 ASSERT_EQ(0, motionArgs.buttonState);
8432 ASSERT_EQ(0, motionArgs.edgeFlags);
8433 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8434 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8435 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8437 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8438 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8439 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8440 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8441
8442 // Should not have sent any more keys or motions.
8443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8445}
8446
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008447TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8448 addConfigurationProperty("touch.deviceType", "touchScreen");
8449 prepareDisplay(DISPLAY_ORIENTATION_0);
8450
8451 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8452 /*fuzz*/ 0, /*resolution*/ 10);
8453 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8454 /*fuzz*/ 0, /*resolution*/ 11);
8455 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8456 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8457 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8458 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8459 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8460 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8461 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8462 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8463
8464 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8465
8466 // X and Y axes
8467 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8468 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8469 // Touch major and minor
8470 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8471 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8472 // Tool major and minor
8473 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8474 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8475}
8476
8477TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8478 addConfigurationProperty("touch.deviceType", "touchScreen");
8479 prepareDisplay(DISPLAY_ORIENTATION_0);
8480
8481 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8482 /*fuzz*/ 0, /*resolution*/ 10);
8483 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8484 /*fuzz*/ 0, /*resolution*/ 11);
8485
8486 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8487
8488 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8489
8490 // Touch major and minor
8491 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8492 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8493 // Tool major and minor
8494 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8495 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8496}
8497
Michael Wrightd02c5b62014-02-10 15:10:22 -08008498TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008499 addConfigurationProperty("touch.deviceType", "touchScreen");
8500 prepareDisplay(DISPLAY_ORIENTATION_0);
8501 prepareAxes(POSITION | ID);
8502 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008503 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008504
arthurhungdcef2dc2020-08-11 14:47:50 +08008505 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008506
8507 NotifyMotionArgs motionArgs;
8508
8509 // Two fingers down at once.
8510 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8511 processPosition(mapper, x1, y1);
8512 processId(mapper, 1);
8513 processMTSync(mapper);
8514 processPosition(mapper, x2, y2);
8515 processId(mapper, 2);
8516 processMTSync(mapper);
8517 processSync(mapper);
8518
8519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8520 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8521 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8522 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8523 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8525 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8526
8527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008528 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008529 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8530 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8531 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8532 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8533 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8535 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8536 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8537 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8538
8539 // Move.
8540 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8541 processPosition(mapper, x1, y1);
8542 processId(mapper, 1);
8543 processMTSync(mapper);
8544 processPosition(mapper, x2, y2);
8545 processId(mapper, 2);
8546 processMTSync(mapper);
8547 processSync(mapper);
8548
8549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8550 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8551 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8552 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8554 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8555 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8557 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8559 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8560
8561 // First finger up.
8562 x2 += 15; y2 -= 20;
8563 processPosition(mapper, x2, y2);
8564 processId(mapper, 2);
8565 processMTSync(mapper);
8566 processSync(mapper);
8567
8568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008569 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008570 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8571 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8573 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8574 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8576 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8578 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8579
8580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8581 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8582 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8583 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8586 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8587
8588 // Move.
8589 x2 += 20; y2 -= 25;
8590 processPosition(mapper, x2, y2);
8591 processId(mapper, 2);
8592 processMTSync(mapper);
8593 processSync(mapper);
8594
8595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8596 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8597 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8598 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8599 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8601 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8602
8603 // New finger down.
8604 int32_t x3 = 700, y3 = 300;
8605 processPosition(mapper, x2, y2);
8606 processId(mapper, 2);
8607 processMTSync(mapper);
8608 processPosition(mapper, x3, y3);
8609 processId(mapper, 3);
8610 processMTSync(mapper);
8611 processSync(mapper);
8612
8613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008614 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008615 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8616 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8617 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8618 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8619 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8621 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8623 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8624
8625 // Second finger up.
8626 x3 += 30; y3 -= 20;
8627 processPosition(mapper, x3, y3);
8628 processId(mapper, 3);
8629 processMTSync(mapper);
8630 processSync(mapper);
8631
8632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008633 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008634 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8635 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8637 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8638 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8640 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8642 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8643
8644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8645 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8646 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8647 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8648 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8650 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8651
8652 // Last finger up.
8653 processMTSync(mapper);
8654 processSync(mapper);
8655
8656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8657 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8658 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8659 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8660 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8662 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8663
8664 // Should not have sent any more keys or motions.
8665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8667}
8668
8669TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008670 addConfigurationProperty("touch.deviceType", "touchScreen");
8671 prepareDisplay(DISPLAY_ORIENTATION_0);
8672 prepareAxes(POSITION | ID | SLOT);
8673 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008674 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008675
arthurhungdcef2dc2020-08-11 14:47:50 +08008676 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008677
8678 NotifyMotionArgs motionArgs;
8679
8680 // Two fingers down at once.
8681 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8682 processPosition(mapper, x1, y1);
8683 processId(mapper, 1);
8684 processSlot(mapper, 1);
8685 processPosition(mapper, x2, y2);
8686 processId(mapper, 2);
8687 processSync(mapper);
8688
8689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8690 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8691 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8692 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8693 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8694 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8695 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8696
8697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008698 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008699 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8700 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8701 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8702 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8703 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8705 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8706 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8707 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8708
8709 // Move.
8710 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8711 processSlot(mapper, 0);
8712 processPosition(mapper, x1, y1);
8713 processSlot(mapper, 1);
8714 processPosition(mapper, x2, y2);
8715 processSync(mapper);
8716
8717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8718 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8719 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8720 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8721 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8722 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8723 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8725 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8727 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8728
8729 // First finger up.
8730 x2 += 15; y2 -= 20;
8731 processSlot(mapper, 0);
8732 processId(mapper, -1);
8733 processSlot(mapper, 1);
8734 processPosition(mapper, x2, y2);
8735 processSync(mapper);
8736
8737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008738 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008739 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8740 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8741 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8742 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8743 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8745 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8747 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8748
8749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8750 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8751 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8752 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8753 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8754 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8755 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8756
8757 // Move.
8758 x2 += 20; y2 -= 25;
8759 processPosition(mapper, x2, y2);
8760 processSync(mapper);
8761
8762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8763 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8764 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8765 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8766 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8768 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8769
8770 // New finger down.
8771 int32_t x3 = 700, y3 = 300;
8772 processPosition(mapper, x2, y2);
8773 processSlot(mapper, 0);
8774 processId(mapper, 3);
8775 processPosition(mapper, x3, y3);
8776 processSync(mapper);
8777
8778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008779 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008780 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8781 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8782 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8783 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8784 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8786 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8788 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8789
8790 // Second finger up.
8791 x3 += 30; y3 -= 20;
8792 processSlot(mapper, 1);
8793 processId(mapper, -1);
8794 processSlot(mapper, 0);
8795 processPosition(mapper, x3, y3);
8796 processSync(mapper);
8797
8798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008799 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008800 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8801 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8802 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8803 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8804 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8806 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8807 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8808 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8809
8810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8811 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8812 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8813 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8814 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8816 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8817
8818 // Last finger up.
8819 processId(mapper, -1);
8820 processSync(mapper);
8821
8822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8823 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8824 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8825 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8826 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8827 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8828 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8829
8830 // Should not have sent any more keys or motions.
8831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8833}
8834
8835TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008836 addConfigurationProperty("touch.deviceType", "touchScreen");
8837 prepareDisplay(DISPLAY_ORIENTATION_0);
8838 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008839 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008840
8841 // These calculations are based on the input device calibration documentation.
8842 int32_t rawX = 100;
8843 int32_t rawY = 200;
8844 int32_t rawTouchMajor = 7;
8845 int32_t rawTouchMinor = 6;
8846 int32_t rawToolMajor = 9;
8847 int32_t rawToolMinor = 8;
8848 int32_t rawPressure = 11;
8849 int32_t rawDistance = 0;
8850 int32_t rawOrientation = 3;
8851 int32_t id = 5;
8852
8853 float x = toDisplayX(rawX);
8854 float y = toDisplayY(rawY);
8855 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8856 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8857 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8858 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8859 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8860 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8861 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8862 float distance = float(rawDistance);
8863
8864 processPosition(mapper, rawX, rawY);
8865 processTouchMajor(mapper, rawTouchMajor);
8866 processTouchMinor(mapper, rawTouchMinor);
8867 processToolMajor(mapper, rawToolMajor);
8868 processToolMinor(mapper, rawToolMinor);
8869 processPressure(mapper, rawPressure);
8870 processOrientation(mapper, rawOrientation);
8871 processDistance(mapper, rawDistance);
8872 processId(mapper, id);
8873 processMTSync(mapper);
8874 processSync(mapper);
8875
8876 NotifyMotionArgs args;
8877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8878 ASSERT_EQ(0, args.pointerProperties[0].id);
8879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8880 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8881 orientation, distance));
8882}
8883
8884TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008885 addConfigurationProperty("touch.deviceType", "touchScreen");
8886 prepareDisplay(DISPLAY_ORIENTATION_0);
8887 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8888 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008889 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008890
8891 // These calculations are based on the input device calibration documentation.
8892 int32_t rawX = 100;
8893 int32_t rawY = 200;
8894 int32_t rawTouchMajor = 140;
8895 int32_t rawTouchMinor = 120;
8896 int32_t rawToolMajor = 180;
8897 int32_t rawToolMinor = 160;
8898
8899 float x = toDisplayX(rawX);
8900 float y = toDisplayY(rawY);
8901 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8902 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8903 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8904 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8905 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8906
8907 processPosition(mapper, rawX, rawY);
8908 processTouchMajor(mapper, rawTouchMajor);
8909 processTouchMinor(mapper, rawTouchMinor);
8910 processToolMajor(mapper, rawToolMajor);
8911 processToolMinor(mapper, rawToolMinor);
8912 processMTSync(mapper);
8913 processSync(mapper);
8914
8915 NotifyMotionArgs args;
8916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8918 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8919}
8920
8921TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008922 addConfigurationProperty("touch.deviceType", "touchScreen");
8923 prepareDisplay(DISPLAY_ORIENTATION_0);
8924 prepareAxes(POSITION | TOUCH | TOOL);
8925 addConfigurationProperty("touch.size.calibration", "diameter");
8926 addConfigurationProperty("touch.size.scale", "10");
8927 addConfigurationProperty("touch.size.bias", "160");
8928 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008929 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008930
8931 // These calculations are based on the input device calibration documentation.
8932 // Note: We only provide a single common touch/tool value because the device is assumed
8933 // not to emit separate values for each pointer (isSummed = 1).
8934 int32_t rawX = 100;
8935 int32_t rawY = 200;
8936 int32_t rawX2 = 150;
8937 int32_t rawY2 = 250;
8938 int32_t rawTouchMajor = 5;
8939 int32_t rawToolMajor = 8;
8940
8941 float x = toDisplayX(rawX);
8942 float y = toDisplayY(rawY);
8943 float x2 = toDisplayX(rawX2);
8944 float y2 = toDisplayY(rawY2);
8945 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8946 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8947 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8948
8949 processPosition(mapper, rawX, rawY);
8950 processTouchMajor(mapper, rawTouchMajor);
8951 processToolMajor(mapper, rawToolMajor);
8952 processMTSync(mapper);
8953 processPosition(mapper, rawX2, rawY2);
8954 processTouchMajor(mapper, rawTouchMajor);
8955 processToolMajor(mapper, rawToolMajor);
8956 processMTSync(mapper);
8957 processSync(mapper);
8958
8959 NotifyMotionArgs args;
8960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8961 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8962
8963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008964 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008965 ASSERT_EQ(size_t(2), args.pointerCount);
8966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8967 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8969 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8970}
8971
8972TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008973 addConfigurationProperty("touch.deviceType", "touchScreen");
8974 prepareDisplay(DISPLAY_ORIENTATION_0);
8975 prepareAxes(POSITION | TOUCH | TOOL);
8976 addConfigurationProperty("touch.size.calibration", "area");
8977 addConfigurationProperty("touch.size.scale", "43");
8978 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008979 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008980
8981 // These calculations are based on the input device calibration documentation.
8982 int32_t rawX = 100;
8983 int32_t rawY = 200;
8984 int32_t rawTouchMajor = 5;
8985 int32_t rawToolMajor = 8;
8986
8987 float x = toDisplayX(rawX);
8988 float y = toDisplayY(rawY);
8989 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8990 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8991 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8992
8993 processPosition(mapper, rawX, rawY);
8994 processTouchMajor(mapper, rawTouchMajor);
8995 processToolMajor(mapper, rawToolMajor);
8996 processMTSync(mapper);
8997 processSync(mapper);
8998
8999 NotifyMotionArgs args;
9000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9001 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9002 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
9003}
9004
9005TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009006 addConfigurationProperty("touch.deviceType", "touchScreen");
9007 prepareDisplay(DISPLAY_ORIENTATION_0);
9008 prepareAxes(POSITION | PRESSURE);
9009 addConfigurationProperty("touch.pressure.calibration", "amplitude");
9010 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009011 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009012
Michael Wrightaa449c92017-12-13 21:21:43 +00009013 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009014 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00009015 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
9016 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
9017 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
9018
Michael Wrightd02c5b62014-02-10 15:10:22 -08009019 // These calculations are based on the input device calibration documentation.
9020 int32_t rawX = 100;
9021 int32_t rawY = 200;
9022 int32_t rawPressure = 60;
9023
9024 float x = toDisplayX(rawX);
9025 float y = toDisplayY(rawY);
9026 float pressure = float(rawPressure) * 0.01f;
9027
9028 processPosition(mapper, rawX, rawY);
9029 processPressure(mapper, rawPressure);
9030 processMTSync(mapper);
9031 processSync(mapper);
9032
9033 NotifyMotionArgs args;
9034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9035 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9036 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
9037}
9038
9039TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009040 addConfigurationProperty("touch.deviceType", "touchScreen");
9041 prepareDisplay(DISPLAY_ORIENTATION_0);
9042 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009043 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009044
9045 NotifyMotionArgs motionArgs;
9046 NotifyKeyArgs keyArgs;
9047
9048 processId(mapper, 1);
9049 processPosition(mapper, 100, 200);
9050 processSync(mapper);
9051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9052 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9053 ASSERT_EQ(0, motionArgs.buttonState);
9054
9055 // press BTN_LEFT, release BTN_LEFT
9056 processKey(mapper, BTN_LEFT, 1);
9057 processSync(mapper);
9058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9060 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
9061
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9063 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9064 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
9065
Michael Wrightd02c5b62014-02-10 15:10:22 -08009066 processKey(mapper, BTN_LEFT, 0);
9067 processSync(mapper);
9068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009069 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009070 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009071
9072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009074 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009075
9076 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
9077 processKey(mapper, BTN_RIGHT, 1);
9078 processKey(mapper, BTN_MIDDLE, 1);
9079 processSync(mapper);
9080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9081 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9082 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
9083 motionArgs.buttonState);
9084
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9086 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9087 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
9088
9089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9090 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9091 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
9092 motionArgs.buttonState);
9093
Michael Wrightd02c5b62014-02-10 15:10:22 -08009094 processKey(mapper, BTN_RIGHT, 0);
9095 processSync(mapper);
9096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009097 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009098 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009099
9100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009101 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009102 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009103
9104 processKey(mapper, BTN_MIDDLE, 0);
9105 processSync(mapper);
9106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009107 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009108 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009109
9110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009112 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009113
9114 // press BTN_BACK, release BTN_BACK
9115 processKey(mapper, BTN_BACK, 1);
9116 processSync(mapper);
9117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9118 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9119 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009120
Michael Wrightd02c5b62014-02-10 15:10:22 -08009121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009123 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
9124
9125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9126 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9127 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009128
9129 processKey(mapper, BTN_BACK, 0);
9130 processSync(mapper);
9131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009132 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009133 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009134
9135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009136 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009137 ASSERT_EQ(0, motionArgs.buttonState);
9138
Michael Wrightd02c5b62014-02-10 15:10:22 -08009139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9140 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9141 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
9142
9143 // press BTN_SIDE, release BTN_SIDE
9144 processKey(mapper, BTN_SIDE, 1);
9145 processSync(mapper);
9146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9147 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9148 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009149
Michael Wrightd02c5b62014-02-10 15:10:22 -08009150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009152 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
9153
9154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9155 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9156 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009157
9158 processKey(mapper, BTN_SIDE, 0);
9159 processSync(mapper);
9160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009161 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009162 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009163
9164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009166 ASSERT_EQ(0, motionArgs.buttonState);
9167
Michael Wrightd02c5b62014-02-10 15:10:22 -08009168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9169 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9170 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
9171
9172 // press BTN_FORWARD, release BTN_FORWARD
9173 processKey(mapper, BTN_FORWARD, 1);
9174 processSync(mapper);
9175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9176 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9177 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009178
Michael Wrightd02c5b62014-02-10 15:10:22 -08009179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009181 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9182
9183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9184 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9185 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009186
9187 processKey(mapper, BTN_FORWARD, 0);
9188 processSync(mapper);
9189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009190 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009191 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009192
9193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009194 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009195 ASSERT_EQ(0, motionArgs.buttonState);
9196
Michael Wrightd02c5b62014-02-10 15:10:22 -08009197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9198 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9199 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9200
9201 // press BTN_EXTRA, release BTN_EXTRA
9202 processKey(mapper, BTN_EXTRA, 1);
9203 processSync(mapper);
9204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9205 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9206 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009207
Michael Wrightd02c5b62014-02-10 15:10:22 -08009208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009209 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009210 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9211
9212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9213 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9214 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009215
9216 processKey(mapper, BTN_EXTRA, 0);
9217 processSync(mapper);
9218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009219 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009220 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009221
9222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009223 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009224 ASSERT_EQ(0, motionArgs.buttonState);
9225
Michael Wrightd02c5b62014-02-10 15:10:22 -08009226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9227 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9228 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9229
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9231
Michael Wrightd02c5b62014-02-10 15:10:22 -08009232 // press BTN_STYLUS, release BTN_STYLUS
9233 processKey(mapper, BTN_STYLUS, 1);
9234 processSync(mapper);
9235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009237 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
9238
9239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9240 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9241 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009242
9243 processKey(mapper, BTN_STYLUS, 0);
9244 processSync(mapper);
9245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009246 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009247 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009248
9249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009251 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009252
9253 // press BTN_STYLUS2, release BTN_STYLUS2
9254 processKey(mapper, BTN_STYLUS2, 1);
9255 processSync(mapper);
9256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009258 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
9259
9260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9261 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9262 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009263
9264 processKey(mapper, BTN_STYLUS2, 0);
9265 processSync(mapper);
9266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009267 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009268 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009269
9270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009272 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009273
9274 // release touch
9275 processId(mapper, -1);
9276 processSync(mapper);
9277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9278 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9279 ASSERT_EQ(0, motionArgs.buttonState);
9280}
9281
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009282TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
9283 addConfigurationProperty("touch.deviceType", "touchScreen");
9284 prepareDisplay(DISPLAY_ORIENTATION_0);
9285 prepareAxes(POSITION | ID | SLOT);
9286 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9287
9288 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
9289 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
9290
9291 // Touch down.
9292 processId(mapper, 1);
9293 processPosition(mapper, 100, 200);
9294 processSync(mapper);
9295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9296 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
9297
9298 // Press and release button mapped to the primary stylus button.
9299 processKey(mapper, BTN_A, 1);
9300 processSync(mapper);
9301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9302 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9303 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9305 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9306 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9307
9308 processKey(mapper, BTN_A, 0);
9309 processSync(mapper);
9310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9311 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9313 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9314
9315 // Press and release the HID usage mapped to the secondary stylus button.
9316 processHidUsage(mapper, 0xabcd, 1);
9317 processSync(mapper);
9318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9319 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9320 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9322 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9323 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9324
9325 processHidUsage(mapper, 0xabcd, 0);
9326 processSync(mapper);
9327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9328 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9330 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9331
9332 // Release touch.
9333 processId(mapper, -1);
9334 processSync(mapper);
9335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9336 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
9337}
9338
Michael Wrightd02c5b62014-02-10 15:10:22 -08009339TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009340 addConfigurationProperty("touch.deviceType", "touchScreen");
9341 prepareDisplay(DISPLAY_ORIENTATION_0);
9342 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009343 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009344
9345 NotifyMotionArgs motionArgs;
9346
9347 // default tool type is finger
9348 processId(mapper, 1);
9349 processPosition(mapper, 100, 200);
9350 processSync(mapper);
9351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9352 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9353 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9354
9355 // eraser
9356 processKey(mapper, BTN_TOOL_RUBBER, 1);
9357 processSync(mapper);
9358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9359 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9360 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
9361
9362 // stylus
9363 processKey(mapper, BTN_TOOL_RUBBER, 0);
9364 processKey(mapper, BTN_TOOL_PEN, 1);
9365 processSync(mapper);
9366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9368 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9369
9370 // brush
9371 processKey(mapper, BTN_TOOL_PEN, 0);
9372 processKey(mapper, BTN_TOOL_BRUSH, 1);
9373 processSync(mapper);
9374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9376 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9377
9378 // pencil
9379 processKey(mapper, BTN_TOOL_BRUSH, 0);
9380 processKey(mapper, BTN_TOOL_PENCIL, 1);
9381 processSync(mapper);
9382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9384 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9385
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009386 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009387 processKey(mapper, BTN_TOOL_PENCIL, 0);
9388 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
9389 processSync(mapper);
9390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9391 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9392 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9393
9394 // mouse
9395 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9396 processKey(mapper, BTN_TOOL_MOUSE, 1);
9397 processSync(mapper);
9398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9400 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
9401
9402 // lens
9403 processKey(mapper, BTN_TOOL_MOUSE, 0);
9404 processKey(mapper, BTN_TOOL_LENS, 1);
9405 processSync(mapper);
9406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9407 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9408 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
9409
9410 // double-tap
9411 processKey(mapper, BTN_TOOL_LENS, 0);
9412 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
9413 processSync(mapper);
9414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9415 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9416 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9417
9418 // triple-tap
9419 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9420 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
9421 processSync(mapper);
9422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9423 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9424 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9425
9426 // quad-tap
9427 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9428 processKey(mapper, BTN_TOOL_QUADTAP, 1);
9429 processSync(mapper);
9430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9432 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9433
9434 // finger
9435 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9436 processKey(mapper, BTN_TOOL_FINGER, 1);
9437 processSync(mapper);
9438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9441
9442 // stylus trumps finger
9443 processKey(mapper, BTN_TOOL_PEN, 1);
9444 processSync(mapper);
9445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9446 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9447 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9448
9449 // eraser trumps stylus
9450 processKey(mapper, BTN_TOOL_RUBBER, 1);
9451 processSync(mapper);
9452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9453 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9454 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
9455
9456 // mouse trumps eraser
9457 processKey(mapper, BTN_TOOL_MOUSE, 1);
9458 processSync(mapper);
9459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9461 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
9462
9463 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9464 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9465 processSync(mapper);
9466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9468 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9469
9470 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9471 processToolType(mapper, MT_TOOL_PEN);
9472 processSync(mapper);
9473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9474 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9475 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9476
9477 // back to default tool type
9478 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9479 processKey(mapper, BTN_TOOL_MOUSE, 0);
9480 processKey(mapper, BTN_TOOL_RUBBER, 0);
9481 processKey(mapper, BTN_TOOL_PEN, 0);
9482 processKey(mapper, BTN_TOOL_FINGER, 0);
9483 processSync(mapper);
9484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9485 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9486 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9487}
9488
9489TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009490 addConfigurationProperty("touch.deviceType", "touchScreen");
9491 prepareDisplay(DISPLAY_ORIENTATION_0);
9492 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009493 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009494 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009495
9496 NotifyMotionArgs motionArgs;
9497
9498 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9499 processId(mapper, 1);
9500 processPosition(mapper, 100, 200);
9501 processSync(mapper);
9502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9503 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9504 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9505 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9506
9507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9508 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // move a little
9513 processPosition(mapper, 150, 250);
9514 processSync(mapper);
9515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9516 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9517 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9518 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9519
9520 // down when BTN_TOUCH is pressed, pressure defaults to 1
9521 processKey(mapper, BTN_TOUCH, 1);
9522 processSync(mapper);
9523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9524 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9525 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9526 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9527
9528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9529 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9531 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9532
9533 // up when BTN_TOUCH is released, hover restored
9534 processKey(mapper, BTN_TOUCH, 0);
9535 processSync(mapper);
9536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9537 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9538 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9539 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9540
9541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9542 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9544 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9545
9546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9547 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // exit hover when pointer goes away
9552 processId(mapper, -1);
9553 processSync(mapper);
9554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9555 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9557 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9558}
9559
9560TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009561 addConfigurationProperty("touch.deviceType", "touchScreen");
9562 prepareDisplay(DISPLAY_ORIENTATION_0);
9563 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009564 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009565
9566 NotifyMotionArgs motionArgs;
9567
9568 // initially hovering because pressure is 0
9569 processId(mapper, 1);
9570 processPosition(mapper, 100, 200);
9571 processPressure(mapper, 0);
9572 processSync(mapper);
9573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9574 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9576 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9577
9578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9579 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9581 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9582
9583 // move a little
9584 processPosition(mapper, 150, 250);
9585 processSync(mapper);
9586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9587 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9589 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9590
9591 // down when pressure becomes non-zero
9592 processPressure(mapper, RAW_PRESSURE_MAX);
9593 processSync(mapper);
9594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9595 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9597 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9598
9599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9600 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9602 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9603
9604 // up when pressure becomes 0, hover restored
9605 processPressure(mapper, 0);
9606 processSync(mapper);
9607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9608 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9609 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9610 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9611
9612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9613 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9615 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9616
9617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9618 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9620 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9621
9622 // exit hover when pointer goes away
9623 processId(mapper, -1);
9624 processSync(mapper);
9625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9626 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9628 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9629}
9630
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009631/**
9632 * Set the input device port <--> display port associations, and check that the
9633 * events are routed to the display that matches the display port.
9634 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9635 */
9636TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009637 const std::string usb2 = "USB2";
9638 const uint8_t hdmi1 = 0;
9639 const uint8_t hdmi2 = 1;
9640 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009641 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009642
9643 addConfigurationProperty("touch.deviceType", "touchScreen");
9644 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009645 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009646
9647 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9648 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9649
9650 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9651 // for this input device is specified, and the matching viewport is not present,
9652 // the input device should be disabled (at the mapper level).
9653
9654 // Add viewport for display 2 on hdmi2
9655 prepareSecondaryDisplay(type, hdmi2);
9656 // Send a touch event
9657 processPosition(mapper, 100, 100);
9658 processSync(mapper);
9659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9660
9661 // Add viewport for display 1 on hdmi1
9662 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
9663 // Send a touch event again
9664 processPosition(mapper, 100, 100);
9665 processSync(mapper);
9666
9667 NotifyMotionArgs args;
9668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9669 ASSERT_EQ(DISPLAY_ID, args.displayId);
9670}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009671
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009672TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9673 addConfigurationProperty("touch.deviceType", "touchScreen");
9674 prepareAxes(POSITION);
9675 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9676
9677 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9678
9679 prepareDisplay(DISPLAY_ORIENTATION_0);
9680 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9681
9682 // Send a touch event
9683 processPosition(mapper, 100, 100);
9684 processSync(mapper);
9685
9686 NotifyMotionArgs args;
9687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9688 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9689}
9690
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009691TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009692 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009693 std::shared_ptr<FakePointerController> fakePointerController =
9694 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009695 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009696 fakePointerController->setPosition(100, 200);
9697 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009698 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009699
Garfield Tan888a6a42020-01-09 11:39:16 -08009700 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009701 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009702
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009703 prepareDisplay(DISPLAY_ORIENTATION_0);
9704 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009705 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009706
Harry Cutts16a24cc2022-10-26 15:22:19 +00009707 // Check source is a touchpad that would obtain the PointerController.
9708 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009709
9710 NotifyMotionArgs motionArgs;
9711 processPosition(mapper, 100, 100);
9712 processSync(mapper);
9713
9714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9715 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9716 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9717}
9718
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009719/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009720 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9721 */
9722TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9723 addConfigurationProperty("touch.deviceType", "touchScreen");
9724 prepareAxes(POSITION);
9725 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9726
9727 prepareDisplay(DISPLAY_ORIENTATION_0);
9728 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
9729 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
9730 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
9731 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9732
9733 NotifyMotionArgs args;
9734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9735 ASSERT_EQ(26, args.readTime);
9736
9737 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
9738 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
9739 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9740
9741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9742 ASSERT_EQ(33, args.readTime);
9743}
9744
9745/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009746 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9747 * events should not be delivered to the listener.
9748 */
9749TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9750 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009751 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009752 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9753 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
9754 ViewportType::INTERNAL);
9755 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9756 prepareAxes(POSITION);
9757 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9758
9759 NotifyMotionArgs motionArgs;
9760 processPosition(mapper, 100, 100);
9761 processSync(mapper);
9762
9763 mFakeListener->assertNotifyMotionWasNotCalled();
9764}
9765
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009766/**
9767 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9768 * the touch mapper can process the events and the events can be delivered to the listener.
9769 */
9770TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9771 addConfigurationProperty("touch.deviceType", "touchScreen");
9772 addConfigurationProperty("touch.enableForInactiveViewport", "1");
9773 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9774 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
9775 ViewportType::INTERNAL);
9776 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9777 prepareAxes(POSITION);
9778 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9779
9780 NotifyMotionArgs motionArgs;
9781 processPosition(mapper, 100, 100);
9782 processSync(mapper);
9783
9784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9785 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9786}
9787
Garfield Tanc734e4f2021-01-15 20:01:39 -08009788TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9789 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009790 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Garfield Tanc734e4f2021-01-15 20:01:39 -08009791 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9792 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
9793 ViewportType::INTERNAL);
9794 std::optional<DisplayViewport> optionalDisplayViewport =
9795 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9796 ASSERT_TRUE(optionalDisplayViewport.has_value());
9797 DisplayViewport displayViewport = *optionalDisplayViewport;
9798
9799 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9800 prepareAxes(POSITION);
9801 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9802
9803 // Finger down
9804 int32_t x = 100, y = 100;
9805 processPosition(mapper, x, y);
9806 processSync(mapper);
9807
9808 NotifyMotionArgs motionArgs;
9809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9810 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9811
9812 // Deactivate display viewport
9813 displayViewport.isActive = false;
9814 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9815 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9816
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009817 // The ongoing touch should be canceled immediately
9818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9819 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9820
9821 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009822 x += 10, y += 10;
9823 processPosition(mapper, x, y);
9824 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009826
9827 // Reactivate display viewport
9828 displayViewport.isActive = true;
9829 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9830 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9831
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009832 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009833 x += 10, y += 10;
9834 processPosition(mapper, x, y);
9835 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9837 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009838}
9839
Arthur Hung7c645402019-01-25 17:45:42 +08009840TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9841 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009842 prepareAxes(POSITION | ID | SLOT);
9843 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009844 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009845
9846 // Create the second touch screen device, and enable multi fingers.
9847 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009848 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009849 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009850 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009851 std::shared_ptr<InputDevice> device2 =
9852 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009853 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009854
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009855 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9856 0 /*flat*/, 0 /*fuzz*/);
9857 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9858 0 /*flat*/, 0 /*fuzz*/);
9859 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
9860 0 /*flat*/, 0 /*fuzz*/);
9861 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
9862 0 /*flat*/, 0 /*fuzz*/);
9863 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
9864 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9865 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009866
9867 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009868 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009869 std::list<NotifyArgs> unused =
9870 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9871 0 /*changes*/);
9872 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009873
9874 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009875 std::shared_ptr<FakePointerController> fakePointerController =
9876 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009877 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009878
9879 // Setup policy for associated displays and show touches.
9880 const uint8_t hdmi1 = 0;
9881 const uint8_t hdmi2 = 1;
9882 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9883 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9884 mFakePolicy->setShowTouches(true);
9885
9886 // Create displays.
9887 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009888 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009889
9890 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009891 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9892 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9893 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009894
9895 // Two fingers down at default display.
9896 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9897 processPosition(mapper, x1, y1);
9898 processId(mapper, 1);
9899 processSlot(mapper, 1);
9900 processPosition(mapper, x2, y2);
9901 processId(mapper, 2);
9902 processSync(mapper);
9903
9904 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9905 fakePointerController->getSpots().find(DISPLAY_ID);
9906 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9907 ASSERT_EQ(size_t(2), iter->second.size());
9908
9909 // Two fingers down at second display.
9910 processPosition(mapper2, x1, y1);
9911 processId(mapper2, 1);
9912 processSlot(mapper2, 1);
9913 processPosition(mapper2, x2, y2);
9914 processId(mapper2, 2);
9915 processSync(mapper2);
9916
9917 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9918 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9919 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009920
9921 // Disable the show touches configuration and ensure the spots are cleared.
9922 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009923 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9924 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009925
9926 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009927}
9928
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009929TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009930 prepareAxes(POSITION);
9931 addConfigurationProperty("touch.deviceType", "touchScreen");
9932 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009933 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009934
9935 NotifyMotionArgs motionArgs;
9936 // Unrotated video frame
9937 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9938 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009939 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009940 processPosition(mapper, 100, 200);
9941 processSync(mapper);
9942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9943 ASSERT_EQ(frames, motionArgs.videoFrames);
9944
9945 // Subsequent touch events should not have any videoframes
9946 // This is implemented separately in FakeEventHub,
9947 // but that should match the behaviour of TouchVideoDevice.
9948 processPosition(mapper, 200, 200);
9949 processSync(mapper);
9950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9951 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9952}
9953
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009954TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009955 prepareAxes(POSITION);
9956 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009957 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009958 // Unrotated video frame
9959 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9960 NotifyMotionArgs motionArgs;
9961
9962 // Test all 4 orientations
9963 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009964 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9965 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9966 clearViewports();
9967 prepareDisplay(orientation);
9968 std::vector<TouchVideoFrame> frames{frame};
9969 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9970 processPosition(mapper, 100, 200);
9971 processSync(mapper);
9972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9973 ASSERT_EQ(frames, motionArgs.videoFrames);
9974 }
9975}
9976
9977TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9978 prepareAxes(POSITION);
9979 addConfigurationProperty("touch.deviceType", "touchScreen");
9980 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9981 // orientation-aware are affected by display rotation.
9982 addConfigurationProperty("touch.orientationAware", "0");
9983 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9984 // Unrotated video frame
9985 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9986 NotifyMotionArgs motionArgs;
9987
9988 // Test all 4 orientations
9989 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009990 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9991 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9992 clearViewports();
9993 prepareDisplay(orientation);
9994 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009995 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009996 processPosition(mapper, 100, 200);
9997 processSync(mapper);
9998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009999 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
10000 // compared to the display. This is so that when the window transform (which contains the
10001 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
10002 // window's coordinate space.
10003 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010004 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +080010005
10006 // Release finger.
10007 processSync(mapper);
10008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010009 }
10010}
10011
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010012TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010013 prepareAxes(POSITION);
10014 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010015 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010016 // Unrotated video frames. There's no rule that they must all have the same dimensions,
10017 // so mix these.
10018 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10019 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
10020 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
10021 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
10022 NotifyMotionArgs motionArgs;
10023
10024 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010025 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010026 processPosition(mapper, 100, 200);
10027 processSync(mapper);
10028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010029 ASSERT_EQ(frames, motionArgs.videoFrames);
10030}
10031
10032TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
10033 prepareAxes(POSITION);
10034 addConfigurationProperty("touch.deviceType", "touchScreen");
10035 // Since InputReader works in the un-rotated coordinate space, only devices that are not
10036 // orientation-aware are affected by display rotation.
10037 addConfigurationProperty("touch.orientationAware", "0");
10038 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10039 // Unrotated video frames. There's no rule that they must all have the same dimensions,
10040 // so mix these.
10041 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10042 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
10043 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
10044 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
10045 NotifyMotionArgs motionArgs;
10046
10047 prepareDisplay(DISPLAY_ORIENTATION_90);
10048 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
10049 processPosition(mapper, 100, 200);
10050 processSync(mapper);
10051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10052 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
10053 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
10054 // compared to the display. This is so that when the window transform (which contains the
10055 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
10056 // window's coordinate space.
10057 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
10058 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010059 ASSERT_EQ(frames, motionArgs.videoFrames);
10060}
10061
Arthur Hung9da14732019-09-02 16:16:58 +080010062/**
10063 * If we had defined port associations, but the viewport is not ready, the touch device would be
10064 * expected to be disabled, and it should be enabled after the viewport has found.
10065 */
10066TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +080010067 constexpr uint8_t hdmi2 = 1;
10068 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010069 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +080010070
10071 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
10072
10073 addConfigurationProperty("touch.deviceType", "touchScreen");
10074 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010075 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +080010076
10077 ASSERT_EQ(mDevice->isEnabled(), false);
10078
10079 // Add display on hdmi2, the device should be enabled and can receive touch event.
10080 prepareSecondaryDisplay(type, hdmi2);
10081 ASSERT_EQ(mDevice->isEnabled(), true);
10082
10083 // Send a touch event.
10084 processPosition(mapper, 100, 100);
10085 processSync(mapper);
10086
10087 NotifyMotionArgs args;
10088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10089 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
10090}
10091
Arthur Hung421eb1c2020-01-16 00:09:42 +080010092TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010093 addConfigurationProperty("touch.deviceType", "touchScreen");
10094 prepareDisplay(DISPLAY_ORIENTATION_0);
10095 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010096 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010097
10098 NotifyMotionArgs motionArgs;
10099
10100 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10101 // finger down
10102 processId(mapper, 1);
10103 processPosition(mapper, x1, y1);
10104 processSync(mapper);
10105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10106 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10107 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10108
10109 // finger move
10110 processId(mapper, 1);
10111 processPosition(mapper, x2, y2);
10112 processSync(mapper);
10113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10114 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10115 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10116
10117 // finger up.
10118 processId(mapper, -1);
10119 processSync(mapper);
10120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10121 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10122 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10123
10124 // new finger down
10125 processId(mapper, 1);
10126 processPosition(mapper, x3, y3);
10127 processSync(mapper);
10128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10129 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10130 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10131}
10132
10133/**
arthurhungcc7f9802020-04-30 17:55:40 +080010134 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
10135 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +080010136 */
arthurhungcc7f9802020-04-30 17:55:40 +080010137TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010138 addConfigurationProperty("touch.deviceType", "touchScreen");
10139 prepareDisplay(DISPLAY_ORIENTATION_0);
10140 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010141 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010142
10143 NotifyMotionArgs motionArgs;
10144
10145 // default tool type is finger
10146 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +080010147 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010148 processPosition(mapper, x1, y1);
10149 processSync(mapper);
10150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10151 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10152 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10153
10154 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
10155 processToolType(mapper, MT_TOOL_PALM);
10156 processSync(mapper);
10157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10158 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10159
10160 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +080010161 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010162 processPosition(mapper, x2, y2);
10163 processSync(mapper);
10164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10165
10166 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +080010167 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010168 processSync(mapper);
10169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10170
10171 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +080010172 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010173 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010174 processPosition(mapper, x3, y3);
10175 processSync(mapper);
10176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10177 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10178 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10179}
10180
arthurhungbf89a482020-04-17 17:37:55 +080010181/**
arthurhungcc7f9802020-04-30 17:55:40 +080010182 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10183 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +080010184 */
arthurhungcc7f9802020-04-30 17:55:40 +080010185TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +080010186 addConfigurationProperty("touch.deviceType", "touchScreen");
10187 prepareDisplay(DISPLAY_ORIENTATION_0);
10188 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
10189 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10190
10191 NotifyMotionArgs motionArgs;
10192
10193 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +080010194 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10195 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010196 processPosition(mapper, x1, y1);
10197 processSync(mapper);
10198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10199 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10200 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10201
10202 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +080010203 processSlot(mapper, SECOND_SLOT);
10204 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010205 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +080010206 processSync(mapper);
10207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010208 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010209 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
10210
10211 // If the tool type of the first finger changes to MT_TOOL_PALM,
10212 // we expect to receive ACTION_POINTER_UP with cancel flag.
10213 processSlot(mapper, FIRST_SLOT);
10214 processId(mapper, FIRST_TRACKING_ID);
10215 processToolType(mapper, MT_TOOL_PALM);
10216 processSync(mapper);
10217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010218 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010219 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10220
10221 // The following MOVE events of second finger should be processed.
10222 processSlot(mapper, SECOND_SLOT);
10223 processId(mapper, SECOND_TRACKING_ID);
10224 processPosition(mapper, x2 + 1, y2 + 1);
10225 processSync(mapper);
10226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10228 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10229
10230 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
10231 // it. Second finger receive move.
10232 processSlot(mapper, FIRST_SLOT);
10233 processId(mapper, INVALID_TRACKING_ID);
10234 processSync(mapper);
10235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10237 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10238
10239 // Second finger keeps moving.
10240 processSlot(mapper, SECOND_SLOT);
10241 processId(mapper, SECOND_TRACKING_ID);
10242 processPosition(mapper, x2 + 2, y2 + 2);
10243 processSync(mapper);
10244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10245 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10246 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10247
10248 // Second finger up.
10249 processId(mapper, INVALID_TRACKING_ID);
10250 processSync(mapper);
10251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10252 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10253 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10254}
10255
10256/**
10257 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
10258 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
10259 */
10260TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
10261 addConfigurationProperty("touch.deviceType", "touchScreen");
10262 prepareDisplay(DISPLAY_ORIENTATION_0);
10263 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
10264 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10265
10266 NotifyMotionArgs motionArgs;
10267
10268 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10269 // First finger down.
10270 processId(mapper, FIRST_TRACKING_ID);
10271 processPosition(mapper, x1, y1);
10272 processSync(mapper);
10273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10274 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10275 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10276
10277 // Second finger down.
10278 processSlot(mapper, SECOND_SLOT);
10279 processId(mapper, SECOND_TRACKING_ID);
10280 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +080010281 processSync(mapper);
10282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010283 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +080010284 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10285
arthurhungcc7f9802020-04-30 17:55:40 +080010286 // If the tool type of the first finger changes to MT_TOOL_PALM,
10287 // we expect to receive ACTION_POINTER_UP with cancel flag.
10288 processSlot(mapper, FIRST_SLOT);
10289 processId(mapper, FIRST_TRACKING_ID);
10290 processToolType(mapper, MT_TOOL_PALM);
10291 processSync(mapper);
10292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010293 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010294 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10295
10296 // Second finger keeps moving.
10297 processSlot(mapper, SECOND_SLOT);
10298 processId(mapper, SECOND_TRACKING_ID);
10299 processPosition(mapper, x2 + 1, y2 + 1);
10300 processSync(mapper);
10301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10303
10304 // second finger becomes palm, receive cancel due to only 1 finger is active.
10305 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010306 processToolType(mapper, MT_TOOL_PALM);
10307 processSync(mapper);
10308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10309 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10310
arthurhungcc7f9802020-04-30 17:55:40 +080010311 // third finger down.
10312 processSlot(mapper, THIRD_SLOT);
10313 processId(mapper, THIRD_TRACKING_ID);
10314 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +080010315 processPosition(mapper, x3, y3);
10316 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +080010317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10318 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10319 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010320 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10321
10322 // third finger move
10323 processId(mapper, THIRD_TRACKING_ID);
10324 processPosition(mapper, x3 + 1, y3 + 1);
10325 processSync(mapper);
10326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10328
10329 // first finger up, third finger receive move.
10330 processSlot(mapper, FIRST_SLOT);
10331 processId(mapper, INVALID_TRACKING_ID);
10332 processSync(mapper);
10333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10335 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10336
10337 // second finger up, third finger receive move.
10338 processSlot(mapper, SECOND_SLOT);
10339 processId(mapper, INVALID_TRACKING_ID);
10340 processSync(mapper);
10341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10343 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10344
10345 // third finger up.
10346 processSlot(mapper, THIRD_SLOT);
10347 processId(mapper, INVALID_TRACKING_ID);
10348 processSync(mapper);
10349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10350 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10351 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10352}
10353
10354/**
10355 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10356 * and the active finger could still be allowed to receive the events
10357 */
10358TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
10359 addConfigurationProperty("touch.deviceType", "touchScreen");
10360 prepareDisplay(DISPLAY_ORIENTATION_0);
10361 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
10362 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10363
10364 NotifyMotionArgs motionArgs;
10365
10366 // default tool type is finger
10367 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10368 processId(mapper, FIRST_TRACKING_ID);
10369 processPosition(mapper, x1, y1);
10370 processSync(mapper);
10371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10372 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10374
10375 // Second finger down.
10376 processSlot(mapper, SECOND_SLOT);
10377 processId(mapper, SECOND_TRACKING_ID);
10378 processPosition(mapper, x2, y2);
10379 processSync(mapper);
10380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010381 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010382 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10383
10384 // If the tool type of the second finger changes to MT_TOOL_PALM,
10385 // we expect to receive ACTION_POINTER_UP with cancel flag.
10386 processId(mapper, SECOND_TRACKING_ID);
10387 processToolType(mapper, MT_TOOL_PALM);
10388 processSync(mapper);
10389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010390 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010391 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10392
10393 // The following MOVE event should be processed.
10394 processSlot(mapper, FIRST_SLOT);
10395 processId(mapper, FIRST_TRACKING_ID);
10396 processPosition(mapper, x1 + 1, y1 + 1);
10397 processSync(mapper);
10398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10400 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10401
10402 // second finger up.
10403 processSlot(mapper, SECOND_SLOT);
10404 processId(mapper, INVALID_TRACKING_ID);
10405 processSync(mapper);
10406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10407 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10408
10409 // first finger keep moving
10410 processSlot(mapper, FIRST_SLOT);
10411 processId(mapper, FIRST_TRACKING_ID);
10412 processPosition(mapper, x1 + 2, y1 + 2);
10413 processSync(mapper);
10414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10415 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10416
10417 // first finger up.
10418 processId(mapper, INVALID_TRACKING_ID);
10419 processSync(mapper);
10420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10421 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10422 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010423}
10424
Arthur Hung9ad18942021-06-19 02:04:46 +000010425/**
10426 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10427 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10428 * cause slot be valid again.
10429 */
10430TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10431 addConfigurationProperty("touch.deviceType", "touchScreen");
10432 prepareDisplay(DISPLAY_ORIENTATION_0);
10433 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10434 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10435
10436 NotifyMotionArgs motionArgs;
10437
10438 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10439 // First finger down.
10440 processId(mapper, FIRST_TRACKING_ID);
10441 processPosition(mapper, x1, y1);
10442 processPressure(mapper, RAW_PRESSURE_MAX);
10443 processSync(mapper);
10444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10445 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10446 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10447
10448 // First finger move.
10449 processId(mapper, FIRST_TRACKING_ID);
10450 processPosition(mapper, x1 + 1, y1 + 1);
10451 processPressure(mapper, RAW_PRESSURE_MAX);
10452 processSync(mapper);
10453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10454 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10455 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10456
10457 // Second finger down.
10458 processSlot(mapper, SECOND_SLOT);
10459 processId(mapper, SECOND_TRACKING_ID);
10460 processPosition(mapper, x2, y2);
10461 processPressure(mapper, RAW_PRESSURE_MAX);
10462 processSync(mapper);
10463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010464 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010465 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10466
10467 // second finger up with some unexpected data.
10468 processSlot(mapper, SECOND_SLOT);
10469 processId(mapper, INVALID_TRACKING_ID);
10470 processPosition(mapper, x2, y2);
10471 processSync(mapper);
10472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010473 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010474 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10475
10476 // first finger up with some unexpected data.
10477 processSlot(mapper, FIRST_SLOT);
10478 processId(mapper, INVALID_TRACKING_ID);
10479 processPosition(mapper, x2, y2);
10480 processPressure(mapper, RAW_PRESSURE_MAX);
10481 processSync(mapper);
10482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10483 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10484 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10485}
10486
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010487TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10488 addConfigurationProperty("touch.deviceType", "touchScreen");
10489 prepareDisplay(DISPLAY_ORIENTATION_0);
10490 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10491 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10492
10493 // First finger down.
10494 processId(mapper, FIRST_TRACKING_ID);
10495 processPosition(mapper, 100, 200);
10496 processPressure(mapper, RAW_PRESSURE_MAX);
10497 processSync(mapper);
10498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10499 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10500
10501 // Second finger down.
10502 processSlot(mapper, SECOND_SLOT);
10503 processId(mapper, SECOND_TRACKING_ID);
10504 processPosition(mapper, 300, 400);
10505 processPressure(mapper, RAW_PRESSURE_MAX);
10506 processSync(mapper);
10507 ASSERT_NO_FATAL_FAILURE(
10508 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10509
10510 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010511 // preserved. Resetting should cancel the ongoing gesture.
10512 resetMapper(mapper, ARBITRARY_TIME);
10513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10514 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010515
10516 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10517 // the existing touch state to generate a down event.
10518 processPosition(mapper, 301, 302);
10519 processSync(mapper);
10520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10521 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10523 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10524
10525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10526}
10527
10528TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10529 addConfigurationProperty("touch.deviceType", "touchScreen");
10530 prepareDisplay(DISPLAY_ORIENTATION_0);
10531 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10532 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10533
10534 // First finger touches down and releases.
10535 processId(mapper, FIRST_TRACKING_ID);
10536 processPosition(mapper, 100, 200);
10537 processPressure(mapper, RAW_PRESSURE_MAX);
10538 processSync(mapper);
10539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10540 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10541 processId(mapper, INVALID_TRACKING_ID);
10542 processSync(mapper);
10543 ASSERT_NO_FATAL_FAILURE(
10544 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10545
10546 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10547 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010548 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10550
10551 // Send an empty sync frame. Since there are no pointers, no events are generated.
10552 processSync(mapper);
10553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10554}
10555
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010556// --- MultiTouchInputMapperTest_ExternalDevice ---
10557
10558class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10559protected:
Chris Yea52ade12020-08-27 16:49:20 -070010560 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010561};
10562
10563/**
10564 * Expect fallback to internal viewport if device is external and external viewport is not present.
10565 */
10566TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10567 prepareAxes(POSITION);
10568 addConfigurationProperty("touch.deviceType", "touchScreen");
10569 prepareDisplay(DISPLAY_ORIENTATION_0);
10570 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10571
10572 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10573
10574 NotifyMotionArgs motionArgs;
10575
10576 // Expect the event to be sent to the internal viewport,
10577 // because an external viewport is not present.
10578 processPosition(mapper, 100, 100);
10579 processSync(mapper);
10580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10581 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10582
10583 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010584 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010585 processPosition(mapper, 100, 100);
10586 processSync(mapper);
10587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10588 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10589}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010590
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010591TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10592 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10593 std::shared_ptr<FakePointerController> fakePointerController =
10594 std::make_shared<FakePointerController>();
10595 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10596 fakePointerController->setPosition(0, 0);
10597 fakePointerController->setButtonState(0);
10598
10599 // prepare device and capture
10600 prepareDisplay(DISPLAY_ORIENTATION_0);
10601 prepareAxes(POSITION | ID | SLOT);
10602 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10603 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10604 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010605 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010606 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10607
10608 // captured touchpad should be a touchpad source
10609 NotifyDeviceResetArgs resetArgs;
10610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10611 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10612
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010613 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010614
10615 const InputDeviceInfo::MotionRange* relRangeX =
10616 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10617 ASSERT_NE(relRangeX, nullptr);
10618 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10619 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10620 const InputDeviceInfo::MotionRange* relRangeY =
10621 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10622 ASSERT_NE(relRangeY, nullptr);
10623 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10624 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10625
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010626 // run captured pointer tests - note that this is unscaled, so input listener events should be
10627 // identical to what the hardware sends (accounting for any
10628 // calibration).
10629 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010630 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010631 processId(mapper, 1);
10632 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10633 processKey(mapper, BTN_TOUCH, 1);
10634 processSync(mapper);
10635
10636 // expect coord[0] to contain initial location of touch 0
10637 NotifyMotionArgs args;
10638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10639 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10640 ASSERT_EQ(1U, args.pointerCount);
10641 ASSERT_EQ(0, args.pointerProperties[0].id);
10642 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10643 ASSERT_NO_FATAL_FAILURE(
10644 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10645
10646 // FINGER 1 DOWN
10647 processSlot(mapper, 1);
10648 processId(mapper, 2);
10649 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10650 processSync(mapper);
10651
10652 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010654 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010655 ASSERT_EQ(2U, args.pointerCount);
10656 ASSERT_EQ(0, args.pointerProperties[0].id);
10657 ASSERT_EQ(1, args.pointerProperties[1].id);
10658 ASSERT_NO_FATAL_FAILURE(
10659 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10660 ASSERT_NO_FATAL_FAILURE(
10661 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10662
10663 // FINGER 1 MOVE
10664 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10665 processSync(mapper);
10666
10667 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10668 // from move
10669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10671 ASSERT_NO_FATAL_FAILURE(
10672 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10673 ASSERT_NO_FATAL_FAILURE(
10674 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10675
10676 // FINGER 0 MOVE
10677 processSlot(mapper, 0);
10678 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10679 processSync(mapper);
10680
10681 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10683 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10684 ASSERT_NO_FATAL_FAILURE(
10685 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10686 ASSERT_NO_FATAL_FAILURE(
10687 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10688
10689 // BUTTON DOWN
10690 processKey(mapper, BTN_LEFT, 1);
10691 processSync(mapper);
10692
10693 // touchinputmapper design sends a move before button press
10694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10695 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10697 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10698
10699 // BUTTON UP
10700 processKey(mapper, BTN_LEFT, 0);
10701 processSync(mapper);
10702
10703 // touchinputmapper design sends a move after button release
10704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10705 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10707 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10708
10709 // FINGER 0 UP
10710 processId(mapper, -1);
10711 processSync(mapper);
10712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10713 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10714
10715 // FINGER 1 MOVE
10716 processSlot(mapper, 1);
10717 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10718 processSync(mapper);
10719
10720 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10722 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10723 ASSERT_EQ(1U, args.pointerCount);
10724 ASSERT_EQ(1, args.pointerProperties[0].id);
10725 ASSERT_NO_FATAL_FAILURE(
10726 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10727
10728 // FINGER 1 UP
10729 processId(mapper, -1);
10730 processKey(mapper, BTN_TOUCH, 0);
10731 processSync(mapper);
10732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10733 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10734
Harry Cutts16a24cc2022-10-26 15:22:19 +000010735 // A non captured touchpad should have a mouse and touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010736 mFakePolicy->setPointerCapture(false);
10737 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Harry Cutts16a24cc2022-10-26 15:22:19 +000010739 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010740}
10741
10742TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10743 std::shared_ptr<FakePointerController> fakePointerController =
10744 std::make_shared<FakePointerController>();
10745 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10746 fakePointerController->setPosition(0, 0);
10747 fakePointerController->setButtonState(0);
10748
10749 // prepare device and capture
10750 prepareDisplay(DISPLAY_ORIENTATION_0);
10751 prepareAxes(POSITION | ID | SLOT);
10752 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10753 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010754 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010755 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10756 // run uncaptured pointer tests - pushes out generic events
10757 // FINGER 0 DOWN
10758 processId(mapper, 3);
10759 processPosition(mapper, 100, 100);
10760 processKey(mapper, BTN_TOUCH, 1);
10761 processSync(mapper);
10762
10763 // start at (100,100), cursor should be at (0,0) * scale
10764 NotifyMotionArgs args;
10765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10766 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10767 ASSERT_NO_FATAL_FAILURE(
10768 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10769
10770 // FINGER 0 MOVE
10771 processPosition(mapper, 200, 200);
10772 processSync(mapper);
10773
10774 // compute scaling to help with touch position checking
10775 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10776 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10777 float scale =
10778 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10779
10780 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10782 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10784 0, 0, 0, 0, 0, 0, 0));
10785}
10786
10787TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10788 std::shared_ptr<FakePointerController> fakePointerController =
10789 std::make_shared<FakePointerController>();
10790
10791 prepareDisplay(DISPLAY_ORIENTATION_0);
10792 prepareAxes(POSITION | ID | SLOT);
10793 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010794 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010795 mFakePolicy->setPointerCapture(false);
10796 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10797
Harry Cutts16a24cc2022-10-26 15:22:19 +000010798 // An uncaptured touchpad should be a pointer device, with additional touchpad source.
10799 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010800
Harry Cutts16a24cc2022-10-26 15:22:19 +000010801 // A captured touchpad should just have a touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010802 mFakePolicy->setPointerCapture(true);
10803 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10804 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10805}
10806
HQ Liue6983c72022-04-19 22:14:56 +000010807class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10808protected:
10809 float mPointerMovementScale;
10810 float mPointerXZoomScale;
10811 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10812 addConfigurationProperty("touch.deviceType", "pointer");
10813 std::shared_ptr<FakePointerController> fakePointerController =
10814 std::make_shared<FakePointerController>();
10815 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10816 fakePointerController->setPosition(0, 0);
10817 fakePointerController->setButtonState(0);
10818 prepareDisplay(DISPLAY_ORIENTATION_0);
10819
10820 prepareAxes(POSITION);
10821 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10822 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10823 // needs to be disabled, and the pointer gesture needs to be enabled.
10824 mFakePolicy->setPointerCapture(false);
10825 mFakePolicy->setPointerGestureEnabled(true);
10826 mFakePolicy->setPointerController(fakePointerController);
10827
10828 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10829 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10830 mPointerMovementScale =
10831 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10832 mPointerXZoomScale =
10833 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10834 }
10835
10836 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10837 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10838 /*flat*/ 0,
10839 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10840 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10841 /*flat*/ 0,
10842 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10843 }
10844};
10845
10846/**
10847 * Two fingers down on a pointer mode touch pad. The width
10848 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10849 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10850 * be greater than the both value to be freeform gesture, so that after two
10851 * fingers start to move downwards, the gesture should be swipe.
10852 */
10853TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10854 // The min freeform gesture width is 25units/mm x 30mm = 750
10855 // which is greater than fraction of the diagnal length of the touchpad (349).
10856 // Thus, MaxSwipWidth is 750.
10857 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10858 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10859 NotifyMotionArgs motionArgs;
10860
10861 // Two fingers down at once.
10862 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10863 // Pointer's initial position is used the [0,0] coordinate.
10864 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10865
10866 processId(mapper, FIRST_TRACKING_ID);
10867 processPosition(mapper, x1, y1);
10868 processMTSync(mapper);
10869 processId(mapper, SECOND_TRACKING_ID);
10870 processPosition(mapper, x2, y2);
10871 processMTSync(mapper);
10872 processSync(mapper);
10873
10874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10875 ASSERT_EQ(1U, motionArgs.pointerCount);
10876 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10877 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010878 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010879 ASSERT_NO_FATAL_FAILURE(
10880 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10881
10882 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10883 // that there should be 1 pointer.
10884 int32_t movingDistance = 200;
10885 y1 += movingDistance;
10886 y2 += movingDistance;
10887
10888 processId(mapper, FIRST_TRACKING_ID);
10889 processPosition(mapper, x1, y1);
10890 processMTSync(mapper);
10891 processId(mapper, SECOND_TRACKING_ID);
10892 processPosition(mapper, x2, y2);
10893 processMTSync(mapper);
10894 processSync(mapper);
10895
10896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10897 ASSERT_EQ(1U, motionArgs.pointerCount);
10898 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10899 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010900 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10902 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10903 0, 0, 0, 0));
10904}
10905
10906/**
10907 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10908 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10909 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10910 * value to be freeform gesture, so that after two fingers start to move downwards,
10911 * the gesture should be swipe.
10912 */
10913TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10914 // The min freeform gesture width is 5units/mm x 30mm = 150
10915 // which is greater than fraction of the diagnal length of the touchpad (349).
10916 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
10917 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
10918 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10919 NotifyMotionArgs motionArgs;
10920
10921 // Two fingers down at once.
10922 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10923 // Pointer's initial position is used the [0,0] coordinate.
10924 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10925
10926 processId(mapper, FIRST_TRACKING_ID);
10927 processPosition(mapper, x1, y1);
10928 processMTSync(mapper);
10929 processId(mapper, SECOND_TRACKING_ID);
10930 processPosition(mapper, x2, y2);
10931 processMTSync(mapper);
10932 processSync(mapper);
10933
10934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10935 ASSERT_EQ(1U, motionArgs.pointerCount);
10936 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10937 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010938 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010939 ASSERT_NO_FATAL_FAILURE(
10940 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10941
10942 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10943 // and there should be 1 pointer.
10944 int32_t movingDistance = 200;
10945 y1 += movingDistance;
10946 y2 += movingDistance;
10947
10948 processId(mapper, FIRST_TRACKING_ID);
10949 processPosition(mapper, x1, y1);
10950 processMTSync(mapper);
10951 processId(mapper, SECOND_TRACKING_ID);
10952 processPosition(mapper, x2, y2);
10953 processMTSync(mapper);
10954 processSync(mapper);
10955
10956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10957 ASSERT_EQ(1U, motionArgs.pointerCount);
10958 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10959 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010960 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010961 // New coordinate is the scaled relative coordinate from the initial coordinate.
10962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10963 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10964 0, 0, 0, 0));
10965}
10966
10967/**
10968 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10969 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10970 * freeform gestures after two fingers start to move downwards.
10971 */
10972TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
10973 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10974 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10975
10976 NotifyMotionArgs motionArgs;
10977
10978 // Two fingers down at once. Wider than the max swipe width.
10979 // The gesture is expected to be PRESS, then transformed to FREEFORM
10980 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10981
10982 processId(mapper, FIRST_TRACKING_ID);
10983 processPosition(mapper, x1, y1);
10984 processMTSync(mapper);
10985 processId(mapper, SECOND_TRACKING_ID);
10986 processPosition(mapper, x2, y2);
10987 processMTSync(mapper);
10988 processSync(mapper);
10989
10990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10991 ASSERT_EQ(1U, motionArgs.pointerCount);
10992 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10993 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010994 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010995 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10996 ASSERT_NO_FATAL_FAILURE(
10997 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10998
10999 int32_t movingDistance = 200;
11000
11001 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
11002 // then two down events for two pointers.
11003 y1 += movingDistance;
11004 y2 += movingDistance;
11005
11006 processId(mapper, FIRST_TRACKING_ID);
11007 processPosition(mapper, x1, y1);
11008 processMTSync(mapper);
11009 processId(mapper, SECOND_TRACKING_ID);
11010 processPosition(mapper, x2, y2);
11011 processMTSync(mapper);
11012 processSync(mapper);
11013
11014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11015 // The previous PRESS gesture is cancelled, because it is transformed to freeform
11016 ASSERT_EQ(1U, motionArgs.pointerCount);
11017 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
11018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11019 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
11020 ASSERT_EQ(1U, motionArgs.pointerCount);
11021 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11023 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011024 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011025 ASSERT_EQ(2U, motionArgs.pointerCount);
11026 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
11027 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011028 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011029 // Two pointers' scaled relative coordinates from their initial centroid.
11030 // Initial y coordinates are 0 as y1 and y2 have the same value.
11031 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
11032 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
11033 // When pointers move, the new coordinates equal to the initial coordinates plus
11034 // scaled moving distance.
11035 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11036 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11037 0, 0, 0, 0));
11038 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11039 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11040 0, 0, 0, 0));
11041
11042 // Move two fingers down again, expect one MOVE motion event.
11043 y1 += movingDistance;
11044 y2 += movingDistance;
11045
11046 processId(mapper, FIRST_TRACKING_ID);
11047 processPosition(mapper, x1, y1);
11048 processMTSync(mapper);
11049 processId(mapper, SECOND_TRACKING_ID);
11050 processPosition(mapper, x2, y2);
11051 processMTSync(mapper);
11052 processSync(mapper);
11053
11054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11055 ASSERT_EQ(2U, motionArgs.pointerCount);
11056 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11057 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011058 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011059 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11060 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11061 0, 0, 0, 0, 0));
11062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11063 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11064 0, 0, 0, 0, 0));
11065}
11066
Harry Cutts39b7ca22022-10-05 15:55:48 +000011067TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
11068 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
11069 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11070 NotifyMotionArgs motionArgs;
11071
11072 // Place two fingers down.
11073 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
11074
11075 processId(mapper, FIRST_TRACKING_ID);
11076 processPosition(mapper, x1, y1);
11077 processMTSync(mapper);
11078 processId(mapper, SECOND_TRACKING_ID);
11079 processPosition(mapper, x2, y2);
11080 processMTSync(mapper);
11081 processSync(mapper);
11082
11083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11084 ASSERT_EQ(1U, motionArgs.pointerCount);
11085 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11086 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
11087 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
11088 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
11089
11090 // Move the two fingers down and to the left.
11091 int32_t movingDistance = 200;
11092 x1 -= movingDistance;
11093 y1 += movingDistance;
11094 x2 -= movingDistance;
11095 y2 += movingDistance;
11096
11097 processId(mapper, FIRST_TRACKING_ID);
11098 processPosition(mapper, x1, y1);
11099 processMTSync(mapper);
11100 processId(mapper, SECOND_TRACKING_ID);
11101 processPosition(mapper, x2, y2);
11102 processMTSync(mapper);
11103 processSync(mapper);
11104
11105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11106 ASSERT_EQ(1U, motionArgs.pointerCount);
11107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11108 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
11109 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
11110 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
11111}
11112
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011113TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
11114 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
11115 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
11116 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
11118
11119 // Start a stylus gesture.
11120 processKey(mapper, BTN_TOOL_PEN, 1);
11121 processId(mapper, FIRST_TRACKING_ID);
11122 processPosition(mapper, 100, 200);
11123 processSync(mapper);
11124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11125 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
11126 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11127 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11128 // TODO(b/257078296): Pointer mode generates extra event.
11129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11130 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11131 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11132 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11134
11135 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
11136 // gesture should be disabled.
11137 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
11138 viewport->isActive = false;
11139 mFakePolicy->updateViewport(*viewport);
11140 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
11141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11142 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11143 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11144 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11145 // TODO(b/257078296): Pointer mode generates extra event.
11146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11147 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11148 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11149 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11151}
11152
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011153// --- JoystickInputMapperTest ---
11154
11155class JoystickInputMapperTest : public InputMapperTest {
11156protected:
11157 static const int32_t RAW_X_MIN;
11158 static const int32_t RAW_X_MAX;
11159 static const int32_t RAW_Y_MIN;
11160 static const int32_t RAW_Y_MAX;
11161
11162 void SetUp() override {
11163 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
11164 }
11165 void prepareAxes() {
11166 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
11167 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
11168 }
11169
11170 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
11171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
11172 }
11173
11174 void processSync(JoystickInputMapper& mapper) {
11175 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
11176 }
11177
11178 void prepareVirtualDisplay(int32_t orientation) {
11179 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
11180 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
11181 NO_PORT, ViewportType::VIRTUAL);
11182 }
11183};
11184
11185const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
11186const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
11187const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
11188const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
11189
11190TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
11191 prepareAxes();
11192 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
11193
11194 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
11195
11196 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
11197
11198 // Send an axis event
11199 processAxis(mapper, ABS_X, 100);
11200 processSync(mapper);
11201
11202 NotifyMotionArgs args;
11203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11204 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11205
11206 // Send another axis event
11207 processAxis(mapper, ABS_Y, 100);
11208 processSync(mapper);
11209
11210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11211 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11212}
11213
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011214// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011215
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011216class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011217protected:
11218 static const char* DEVICE_NAME;
11219 static const char* DEVICE_LOCATION;
11220 static const int32_t DEVICE_ID;
11221 static const int32_t DEVICE_GENERATION;
11222 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011223 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011224 static const int32_t EVENTHUB_ID;
11225
11226 std::shared_ptr<FakeEventHub> mFakeEventHub;
11227 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011228 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011229 std::unique_ptr<InstrumentedInputReader> mReader;
11230 std::shared_ptr<InputDevice> mDevice;
11231
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011232 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011233 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011234 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011235 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011236 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011237 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011238 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11239 }
11240
11241 void SetUp() override { SetUp(DEVICE_CLASSES); }
11242
11243 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011244 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011245 mFakePolicy.clear();
11246 }
11247
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070011248 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011249 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
11250 mReader->requestRefreshConfiguration(changes);
11251 mReader->loopOnce();
11252 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070011253 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011254 }
11255
11256 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11257 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011258 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011259 InputDeviceIdentifier identifier;
11260 identifier.name = name;
11261 identifier.location = location;
11262 std::shared_ptr<InputDevice> device =
11263 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11264 identifier);
11265 mReader->pushNextDevice(device);
11266 mFakeEventHub->addDevice(eventHubId, name, classes);
11267 mReader->loopOnce();
11268 return device;
11269 }
11270
11271 template <class T, typename... Args>
11272 T& addControllerAndConfigure(Args... args) {
11273 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11274
11275 return controller;
11276 }
11277};
11278
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011279const char* PeripheralControllerTest::DEVICE_NAME = "device";
11280const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11281const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11282const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11283const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011284const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11285 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011286const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011287
11288// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011289class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011290protected:
11291 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011292 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011293 }
11294};
11295
11296TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011297 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011298
11299 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
11300 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
11301}
11302
11303TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011304 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011305
11306 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
11307 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
11308}
11309
11310// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011311class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011312protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011313 void SetUp() override {
11314 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11315 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011316};
11317
Chris Ye85758332021-05-16 23:05:17 -070011318TEST_F(LightControllerTest, MonoLight) {
11319 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011320 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011321 .maxBrightness = 255,
11322 .flags = InputLightClass::BRIGHTNESS,
11323 .path = ""};
11324 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011325
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011326 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011327 InputDeviceInfo info;
11328 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011329 std::vector<InputDeviceLightInfo> lights = info.getLights();
11330 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011331 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11332 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11333
11334 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11335 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11336}
11337
11338TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11339 RawLightInfo infoMono = {.id = 1,
11340 .name = "mono_keyboard_backlight",
11341 .maxBrightness = 255,
11342 .flags = InputLightClass::BRIGHTNESS |
11343 InputLightClass::KEYBOARD_BACKLIGHT,
11344 .path = ""};
11345 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11346
11347 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11348 InputDeviceInfo info;
11349 controller.populateDeviceInfo(&info);
11350 std::vector<InputDeviceLightInfo> lights = info.getLights();
11351 ASSERT_EQ(1U, lights.size());
11352 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11353 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011354
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011355 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11356 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011357}
11358
11359TEST_F(LightControllerTest, RGBLight) {
11360 RawLightInfo infoRed = {.id = 1,
11361 .name = "red",
11362 .maxBrightness = 255,
11363 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11364 .path = ""};
11365 RawLightInfo infoGreen = {.id = 2,
11366 .name = "green",
11367 .maxBrightness = 255,
11368 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11369 .path = ""};
11370 RawLightInfo infoBlue = {.id = 3,
11371 .name = "blue",
11372 .maxBrightness = 255,
11373 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11374 .path = ""};
11375 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11376 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11377 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11378
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011379 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011380 InputDeviceInfo info;
11381 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011382 std::vector<InputDeviceLightInfo> lights = info.getLights();
11383 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011384 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11385 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11386 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11387
11388 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11389 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11390}
11391
11392TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11393 RawLightInfo infoRed = {.id = 1,
11394 .name = "red_keyboard_backlight",
11395 .maxBrightness = 255,
11396 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11397 InputLightClass::KEYBOARD_BACKLIGHT,
11398 .path = ""};
11399 RawLightInfo infoGreen = {.id = 2,
11400 .name = "green_keyboard_backlight",
11401 .maxBrightness = 255,
11402 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11403 InputLightClass::KEYBOARD_BACKLIGHT,
11404 .path = ""};
11405 RawLightInfo infoBlue = {.id = 3,
11406 .name = "blue_keyboard_backlight",
11407 .maxBrightness = 255,
11408 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11409 InputLightClass::KEYBOARD_BACKLIGHT,
11410 .path = ""};
11411 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11412 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11413 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11414
11415 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11416 InputDeviceInfo info;
11417 controller.populateDeviceInfo(&info);
11418 std::vector<InputDeviceLightInfo> lights = info.getLights();
11419 ASSERT_EQ(1U, lights.size());
11420 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11421 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11422 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11423
11424 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11425 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11426}
11427
11428TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11429 RawLightInfo infoRed = {.id = 1,
11430 .name = "red",
11431 .maxBrightness = 255,
11432 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11433 .path = ""};
11434 RawLightInfo infoGreen = {.id = 2,
11435 .name = "green",
11436 .maxBrightness = 255,
11437 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11438 .path = ""};
11439 RawLightInfo infoBlue = {.id = 3,
11440 .name = "blue",
11441 .maxBrightness = 255,
11442 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11443 .path = ""};
11444 RawLightInfo infoGlobal = {.id = 3,
11445 .name = "global_keyboard_backlight",
11446 .maxBrightness = 255,
11447 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11448 InputLightClass::KEYBOARD_BACKLIGHT,
11449 .path = ""};
11450 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11451 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11452 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11453 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11454
11455 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11456 InputDeviceInfo info;
11457 controller.populateDeviceInfo(&info);
11458 std::vector<InputDeviceLightInfo> lights = info.getLights();
11459 ASSERT_EQ(1U, lights.size());
11460 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11461 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11462 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011463
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011464 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11465 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011466}
11467
11468TEST_F(LightControllerTest, MultiColorRGBLight) {
11469 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011470 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011471 .maxBrightness = 255,
11472 .flags = InputLightClass::BRIGHTNESS |
11473 InputLightClass::MULTI_INTENSITY |
11474 InputLightClass::MULTI_INDEX,
11475 .path = ""};
11476
11477 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11478
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011479 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011480 InputDeviceInfo info;
11481 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011482 std::vector<InputDeviceLightInfo> lights = info.getLights();
11483 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011484 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11485 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11486 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11487
11488 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11489 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11490}
11491
11492TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11493 RawLightInfo infoColor = {.id = 1,
11494 .name = "multi_color_keyboard_backlight",
11495 .maxBrightness = 255,
11496 .flags = InputLightClass::BRIGHTNESS |
11497 InputLightClass::MULTI_INTENSITY |
11498 InputLightClass::MULTI_INDEX |
11499 InputLightClass::KEYBOARD_BACKLIGHT,
11500 .path = ""};
11501
11502 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11503
11504 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11505 InputDeviceInfo info;
11506 controller.populateDeviceInfo(&info);
11507 std::vector<InputDeviceLightInfo> lights = info.getLights();
11508 ASSERT_EQ(1U, lights.size());
11509 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11510 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11511 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011512
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011513 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11514 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011515}
11516
11517TEST_F(LightControllerTest, PlayerIdLight) {
11518 RawLightInfo info1 = {.id = 1,
11519 .name = "player1",
11520 .maxBrightness = 255,
11521 .flags = InputLightClass::BRIGHTNESS,
11522 .path = ""};
11523 RawLightInfo info2 = {.id = 2,
11524 .name = "player2",
11525 .maxBrightness = 255,
11526 .flags = InputLightClass::BRIGHTNESS,
11527 .path = ""};
11528 RawLightInfo info3 = {.id = 3,
11529 .name = "player3",
11530 .maxBrightness = 255,
11531 .flags = InputLightClass::BRIGHTNESS,
11532 .path = ""};
11533 RawLightInfo info4 = {.id = 4,
11534 .name = "player4",
11535 .maxBrightness = 255,
11536 .flags = InputLightClass::BRIGHTNESS,
11537 .path = ""};
11538 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11539 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11540 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11541 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11542
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011543 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011544 InputDeviceInfo info;
11545 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011546 std::vector<InputDeviceLightInfo> lights = info.getLights();
11547 ASSERT_EQ(1U, lights.size());
11548 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011549 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11550 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011551
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011552 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11553 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11554 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011555}
11556
Michael Wrightd02c5b62014-02-10 15:10:22 -080011557} // namespace android