blob: 461fd5d79b69f678315e21b24aa04a718cddda83 [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
Prabir Pradhanf9a41282022-10-25 17:15:50 +00002574TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
2575 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
2576 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
2577 // presses).
2578 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
2579 mDeviceInfo.getSources());
2580}
2581
Arthur Hungaab25622020-01-16 11:22:11 +08002582TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2583 NotifyMotionArgs args;
2584 const Point centerPoint = mDevice->getCenterPoint();
2585
2586 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002587 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002588 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002589 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002590 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2591 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2592
2593 // ACTION_MOVE
2594 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002595 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002596 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2597 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2598
2599 // ACTION_UP
2600 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002601 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002602 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2603 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2604}
2605
2606TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2607 NotifyMotionArgs args;
2608 const Point centerPoint = mDevice->getCenterPoint();
2609
2610 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002611 mDevice->sendSlot(FIRST_SLOT);
2612 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002613 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002614 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002615 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2616 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2617
2618 // ACTION_POINTER_DOWN (Second slot)
2619 const Point secondPoint = centerPoint + Point(100, 100);
2620 mDevice->sendSlot(SECOND_SLOT);
2621 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002622 mDevice->sendDown(secondPoint);
2623 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002624 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002625 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002626
2627 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002628 mDevice->sendMove(secondPoint + Point(1, 1));
2629 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002630 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2631 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2632
2633 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002634 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002635 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002636 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002637 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002638
2639 // ACTION_UP
2640 mDevice->sendSlot(FIRST_SLOT);
2641 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002642 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002643 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2644 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2645}
2646
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002647/**
2648 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2649 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2650 * data?
2651 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2652 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2653 * for Pointer 0 only is generated after.
2654 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2655 * events, we will not miss any information.
2656 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2657 * event generated afterwards that contains the newest movement of pointer 0.
2658 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2659 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2660 * losing information about non-palm pointers.
2661 */
2662TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2663 NotifyMotionArgs args;
2664 const Point centerPoint = mDevice->getCenterPoint();
2665
2666 // ACTION_DOWN
2667 mDevice->sendSlot(FIRST_SLOT);
2668 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2669 mDevice->sendDown(centerPoint);
2670 mDevice->sendSync();
2671 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2672
2673 // ACTION_POINTER_DOWN (Second slot)
2674 const Point secondPoint = centerPoint + Point(100, 100);
2675 mDevice->sendSlot(SECOND_SLOT);
2676 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2677 mDevice->sendDown(secondPoint);
2678 mDevice->sendSync();
2679 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2680
2681 // ACTION_MOVE (First slot)
2682 mDevice->sendSlot(FIRST_SLOT);
2683 mDevice->sendMove(centerPoint + Point(5, 5));
2684 // ACTION_POINTER_UP (Second slot)
2685 mDevice->sendSlot(SECOND_SLOT);
2686 mDevice->sendPointerUp();
2687 // Send a single sync for the above 2 pointer updates
2688 mDevice->sendSync();
2689
2690 // First, we should get POINTER_UP for the second pointer
2691 assertReceivedMotion(ACTION_POINTER_1_UP,
2692 {/*first pointer */ centerPoint + Point(5, 5),
2693 /*second pointer*/ secondPoint});
2694
2695 // Next, the MOVE event for the first pointer
2696 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2697}
2698
2699/**
2700 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2701 * move, and then it will go up, all in the same frame.
2702 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2703 * gets sent to the listener.
2704 */
2705TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2706 NotifyMotionArgs args;
2707 const Point centerPoint = mDevice->getCenterPoint();
2708
2709 // ACTION_DOWN
2710 mDevice->sendSlot(FIRST_SLOT);
2711 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2712 mDevice->sendDown(centerPoint);
2713 mDevice->sendSync();
2714 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2715
2716 // ACTION_POINTER_DOWN (Second slot)
2717 const Point secondPoint = centerPoint + Point(100, 100);
2718 mDevice->sendSlot(SECOND_SLOT);
2719 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2720 mDevice->sendDown(secondPoint);
2721 mDevice->sendSync();
2722 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2723
2724 // ACTION_MOVE (First slot)
2725 mDevice->sendSlot(FIRST_SLOT);
2726 mDevice->sendMove(centerPoint + Point(5, 5));
2727 // ACTION_POINTER_UP (Second slot)
2728 mDevice->sendSlot(SECOND_SLOT);
2729 mDevice->sendMove(secondPoint + Point(6, 6));
2730 mDevice->sendPointerUp();
2731 // Send a single sync for the above 2 pointer updates
2732 mDevice->sendSync();
2733
2734 // First, we should get POINTER_UP for the second pointer
2735 // The movement of the second pointer during the liftoff frame is ignored.
2736 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2737 assertReceivedMotion(ACTION_POINTER_1_UP,
2738 {/*first pointer */ centerPoint + Point(5, 5),
2739 /*second pointer*/ secondPoint});
2740
2741 // Next, the MOVE event for the first pointer
2742 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2743}
2744
Arthur Hungaab25622020-01-16 11:22:11 +08002745TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2746 NotifyMotionArgs args;
2747 const Point centerPoint = mDevice->getCenterPoint();
2748
2749 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002750 mDevice->sendSlot(FIRST_SLOT);
2751 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002752 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002753 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002754 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2755 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2756
arthurhungcc7f9802020-04-30 17:55:40 +08002757 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002758 const Point secondPoint = centerPoint + Point(100, 100);
2759 mDevice->sendSlot(SECOND_SLOT);
2760 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2761 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002762 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002763 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002764 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002765
arthurhungcc7f9802020-04-30 17:55:40 +08002766 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002767 mDevice->sendMove(secondPoint + Point(1, 1));
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));
2770 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2771
arthurhungcc7f9802020-04-30 17:55:40 +08002772 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2773 // a palm event.
2774 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002775 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002776 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002777 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002778 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002779 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002780
arthurhungcc7f9802020-04-30 17:55:40 +08002781 // Send up to second slot, expect first slot send moving.
2782 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002783 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002784 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2785 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002786
arthurhungcc7f9802020-04-30 17:55:40 +08002787 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002788 mDevice->sendSlot(FIRST_SLOT);
2789 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002790 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002791
arthurhungcc7f9802020-04-30 17:55:40 +08002792 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2793 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002794}
2795
Prabir Pradhanda20b172022-09-26 17:01:18 +00002796TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
2797 const Point centerPoint = mDevice->getCenterPoint();
2798
2799 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
2800 mDevice->sendSlot(FIRST_SLOT);
2801 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2802 mDevice->sendToolType(MT_TOOL_PEN);
2803 mDevice->sendDown(centerPoint);
2804 mDevice->sendSync();
2805 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2806 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2807 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2808
2809 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2810
2811 // Release the stylus touch.
2812 mDevice->sendUp();
2813 mDevice->sendSync();
2814 ASSERT_NO_FATAL_FAILURE(
2815 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2816
2817 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2818
2819 // Touch down with the finger, without the pen tool selected. The policy is not notified.
2820 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2821 mDevice->sendToolType(MT_TOOL_FINGER);
2822 mDevice->sendDown(centerPoint);
2823 mDevice->sendSync();
2824 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2825 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2826 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2827
2828 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2829
2830 mDevice->sendUp();
2831 mDevice->sendSync();
2832 ASSERT_NO_FATAL_FAILURE(
2833 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2834
2835 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
2836 // The policy should be notified of the stylus presence.
2837 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2838 mDevice->sendToolType(MT_TOOL_PEN);
2839 mDevice->sendMove(centerPoint);
2840 mDevice->sendSync();
2841 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2842 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2843 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2844
2845 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2846}
2847
Prabir Pradhan124ea442022-10-28 20:27:44 +00002848// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002849
Prabir Pradhan124ea442022-10-28 20:27:44 +00002850// Verify the behavior of button presses reported by various kinds of styluses, including buttons
2851// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
2852// stylus.
2853template <typename UinputStylusDevice>
2854class StylusButtonIntegrationTest : public TouchIntegrationTest {
2855protected:
2856 void SetUp() override {
2857#if !defined(__ANDROID__)
2858 GTEST_SKIP();
2859#endif
2860 TouchIntegrationTest::SetUp();
2861 mTouchscreen = mDevice.get();
2862 mTouchscreenInfo = mDeviceInfo;
2863
2864 setUpStylusDevice();
2865 }
2866
2867 UinputStylusDevice* mStylus{nullptr};
2868 InputDeviceInfo mStylusInfo{};
2869
2870 UinputTouchScreen* mTouchscreen{nullptr};
2871 InputDeviceInfo mTouchscreenInfo{};
2872
2873private:
2874 // When we are attempting to test stylus button events that are sent from the touchscreen,
2875 // use the same Uinput device for the touchscreen and the stylus.
2876 template <typename T = UinputStylusDevice>
2877 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2878 mStylus = mDevice.get();
2879 mStylusInfo = mDeviceInfo;
2880 }
2881
2882 // When we are attempting to stylus buttons from an external stylus being merged with touches
2883 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2884 template <typename T = UinputStylusDevice>
2885 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2886 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2887 mStylus = mStylusDeviceLifecycleTracker.get();
2888 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2889 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2890 const auto info = findDeviceByName(mStylus->getName());
2891 ASSERT_TRUE(info);
2892 mStylusInfo = *info;
2893 }
2894
2895 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2896
2897 // Hide the base class's device to expose it with a different name for readability.
2898 using TouchIntegrationTest::mDevice;
2899 using TouchIntegrationTest::mDeviceInfo;
2900};
2901
2902using StylusButtonIntegrationTestTypes =
2903 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2904TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2905
2906TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
2907 const auto stylusId = TestFixture::mStylusInfo.getId();
2908
2909 TestFixture::mStylus->pressKey(BTN_STYLUS);
2910 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2911 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2912 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2913
2914 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2915 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002916 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002917 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002918}
2919
Prabir Pradhan124ea442022-10-28 20:27:44 +00002920TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
2921 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2922 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2923 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002924
2925 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002926 TestFixture::mStylus->pressKey(BTN_STYLUS);
2927 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002928 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002929 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002930
2931 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002932 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2933 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2934 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2935 TestFixture::mTouchscreen->sendDown(centerPoint);
2936 TestFixture::mTouchscreen->sendSync();
2937 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002938 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2939 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002940 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2941 WithDeviceId(touchscreenId))));
2942 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002943 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2944 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002945 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2946 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002947
Prabir Pradhan124ea442022-10-28 20:27:44 +00002948 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2949 TestFixture::mTouchscreen->sendSync();
2950 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002951 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002952 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2953 WithDeviceId(touchscreenId))));
2954 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002955 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002956 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2957 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002958
2959 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002960 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2961 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002962 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002963 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002964}
2965
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002966TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
2967 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2968 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2969 const auto stylusId = TestFixture::mStylusInfo.getId();
2970 auto toolTypeDevice =
2971 AllOf(WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithDeviceId(touchscreenId));
2972
2973 // Press the stylus button.
2974 TestFixture::mStylus->pressKey(BTN_STYLUS);
2975 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2976 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2977 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2978
2979 // Start hovering with the stylus.
2980 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2981 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2982 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2983 TestFixture::mTouchscreen->sendMove(centerPoint);
2984 TestFixture::mTouchscreen->sendSync();
2985 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2986 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2987 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2988 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2989 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2990 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2991 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2992 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2993 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2994
2995 // Touch down with the stylus.
2996 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2997 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2998 TestFixture::mTouchscreen->sendDown(centerPoint);
2999 TestFixture::mTouchscreen->sendSync();
3000 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3001 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3002 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
3003
3004 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3005 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3006 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
3007
3008 // Stop touching with the stylus, and start hovering.
3009 TestFixture::mTouchscreen->sendUp();
3010 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
3011 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
3012 TestFixture::mTouchscreen->sendMove(centerPoint);
3013 TestFixture::mTouchscreen->sendSync();
3014 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3015 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
3016 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
3017 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3018 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3019 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
3020 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3021 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3022 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
3023
3024 // Stop hovering.
3025 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
3026 TestFixture::mTouchscreen->sendSync();
3027 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3028 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
3029 WithButtonState(0))));
3030 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
3031 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3032 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3033 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
3034
3035 // Release the stylus button.
3036 TestFixture::mStylus->releaseKey(BTN_STYLUS);
3037 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
3038 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
3039 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
3040}
3041
Prabir Pradhan124ea442022-10-28 20:27:44 +00003042TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
3043 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
3044 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
3045 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003046
3047 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00003048 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
3049 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
3050 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
3051 TestFixture::mTouchscreen->sendDown(centerPoint);
3052 TestFixture::mTouchscreen->sendSync();
3053 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003054 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003055 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3056 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003057
3058 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00003059 TestFixture::mStylus->pressKey(BTN_STYLUS);
3060 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003061 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003062 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
3063 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003064 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
3065 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003066 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
3067 WithDeviceId(touchscreenId))));
3068 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003069 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
3070 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003071 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
3072 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003073
Prabir Pradhan124ea442022-10-28 20:27:44 +00003074 TestFixture::mStylus->releaseKey(BTN_STYLUS);
3075 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003076 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003077 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
3078 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003079 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003080 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3081 WithDeviceId(touchscreenId))));
3082 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003083 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003084 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3085 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003086
3087 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00003088 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
3089 TestFixture::mTouchscreen->sendSync();
3090 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003091 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003092 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3093 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003094}
3095
Prabir Pradhan484d55a2022-10-14 23:17:16 +00003096// --- ExternalStylusIntegrationTest ---
3097
3098// Verify the behavior of an external stylus. An external stylus can report pressure or button
3099// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
3100// ongoing stylus gesture that is being emitted by the touchscreen.
3101using ExternalStylusIntegrationTest = TouchIntegrationTest;
3102
3103TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
3104 const Point centerPoint = mDevice->getCenterPoint();
3105
3106 // Create an external stylus capable of reporting pressure data that
3107 // should be fused with a touch pointer.
3108 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
3109 createUinputDevice<UinputExternalStylusWithPressure>();
3110 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
3111 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
3112 const auto stylusInfo = findDeviceByName(stylus->getName());
3113 ASSERT_TRUE(stylusInfo);
3114
3115 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
3116
3117 const auto touchscreenId = mDeviceInfo.getId();
3118
3119 // Set a pressure value on the stylus. It doesn't generate any events.
3120 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
3121 stylus->setPressure(100);
3122 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3123
3124 // Start a finger gesture, and ensure it shows up as stylus gesture
3125 // with the pressure set by the external stylus.
3126 mDevice->sendSlot(FIRST_SLOT);
3127 mDevice->sendTrackingId(FIRST_TRACKING_ID);
3128 mDevice->sendToolType(MT_TOOL_FINGER);
3129 mDevice->sendDown(centerPoint);
3130 mDevice->sendSync();
3131 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3132 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3133 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3134 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
3135
3136 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
3137 // event with the updated pressure.
3138 stylus->setPressure(200);
3139 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3140 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
3141 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3142 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
3143
3144 // The external stylus did not generate any events.
3145 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3146 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
3147}
3148
3149TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
3150 const Point centerPoint = mDevice->getCenterPoint();
3151
3152 // Create an external stylus capable of reporting pressure data that
3153 // should be fused with a touch pointer.
3154 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
3155 createUinputDevice<UinputExternalStylusWithPressure>();
3156 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
3157 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
3158 const auto stylusInfo = findDeviceByName(stylus->getName());
3159 ASSERT_TRUE(stylusInfo);
3160
3161 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
3162
3163 const auto touchscreenId = mDeviceInfo.getId();
3164
3165 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
3166 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00003167 // Send a non-zero value first to prevent the kernel from consuming the zero event.
3168 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00003169 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00003170 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00003171
3172 // Start a finger gesture. The touch device will withhold generating any touches for
3173 // up to 72 milliseconds while waiting for pressure data from the external stylus.
3174 mDevice->sendSlot(FIRST_SLOT);
3175 mDevice->sendTrackingId(FIRST_TRACKING_ID);
3176 mDevice->sendToolType(MT_TOOL_FINGER);
3177 mDevice->sendDown(centerPoint);
3178 auto waitUntil = std::chrono::system_clock::now() +
3179 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
3180 mDevice->sendSync();
3181 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
3182
3183 // Since the external stylus did not report a pressure value within the timeout,
3184 // it shows up as a finger pointer.
3185 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3186 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3187 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithDeviceId(touchscreenId),
3188 WithPressure(1.f))));
3189
3190 // Change the pressure on the external stylus. Since the pressure was not present at the start
3191 // of the gesture, it is ignored for now.
3192 stylus->setPressure(200);
3193 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3194
3195 // Finish the finger gesture.
3196 mDevice->sendTrackingId(INVALID_TRACKING_ID);
3197 mDevice->sendSync();
3198 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3199 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3200 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
3201
3202 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
3203 mDevice->sendTrackingId(FIRST_TRACKING_ID);
3204 mDevice->sendToolType(MT_TOOL_FINGER);
3205 mDevice->sendDown(centerPoint);
3206 mDevice->sendSync();
3207 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3208 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3209 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3210 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
3211
3212 // The external stylus did not generate any events.
3213 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3214 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
3215}
3216
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00003217TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
3218 const Point centerPoint = mDevice->getCenterPoint();
3219
3220 // Create an external stylus device that does not support pressure. It should not affect any
3221 // touch pointers.
3222 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
3223 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
3224 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
3225 const auto stylusInfo = findDeviceByName(stylus->getName());
3226 ASSERT_TRUE(stylusInfo);
3227
3228 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
3229
3230 const auto touchscreenId = mDeviceInfo.getId();
3231
3232 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
3233 // pressure data from the external stylus.
3234 mDevice->sendSlot(FIRST_SLOT);
3235 mDevice->sendTrackingId(FIRST_TRACKING_ID);
3236 mDevice->sendToolType(MT_TOOL_FINGER);
3237 mDevice->sendDown(centerPoint);
3238 auto waitUntil = std::chrono::system_clock::now() +
3239 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
3240 mDevice->sendSync();
3241 ASSERT_NO_FATAL_FAILURE(
3242 mTestListener
3243 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3244 WithToolType(
3245 AMOTION_EVENT_TOOL_TYPE_FINGER),
3246 WithButtonState(0),
3247 WithDeviceId(touchscreenId),
3248 WithPressure(1.f)),
3249 waitUntil));
3250
3251 // The external stylus did not generate any events.
3252 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3253 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
3254}
3255
Michael Wrightd02c5b62014-02-10 15:10:22 -08003256// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08003257class InputDeviceTest : public testing::Test {
3258protected:
3259 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08003260 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261 static const int32_t DEVICE_ID;
3262 static const int32_t DEVICE_GENERATION;
3263 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003264 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003265 static const int32_t EVENTHUB_ID;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003266 static const std::string DEVICE_BLUETOOTH_ADDRESS;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003267
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003268 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003269 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003270 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08003271 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00003272 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273
Chris Yea52ade12020-08-27 16:49:20 -07003274 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003275 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003276 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003277 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08003278 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003279 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003280 InputDeviceIdentifier identifier;
3281 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08003282 identifier.location = DEVICE_LOCATION;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003283 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
arthurhungdcef2dc2020-08-11 14:47:50 +08003284 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003285 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08003286 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003287 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003288 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003289 }
3290
Chris Yea52ade12020-08-27 16:49:20 -07003291 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003292 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003293 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003294 }
3295};
3296
3297const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003298const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003299const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
3301const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003302const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07003303 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003304const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003305const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
Michael Wrightd02c5b62014-02-10 15:10:22 -08003306
3307TEST_F(InputDeviceTest, ImmutableProperties) {
3308 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003309 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003310 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003311}
3312
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00003313TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
3314 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
3315
3316 // Configuration
3317 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
3318 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003319 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00003320
3321 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
3322}
3323
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003324TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
3325 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07003326}
3327
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
3329 // Configuration.
3330 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003331 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003332
3333 // Reset.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003334 unused += mDevice->reset(ARBITRARY_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003335
3336 NotifyDeviceResetArgs resetArgs;
3337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3338 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3339 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3340
3341 // Metadata.
3342 ASSERT_TRUE(mDevice->isIgnored());
3343 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
3344
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00003345 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003347 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
3349 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
3350
3351 // State queries.
3352 ASSERT_EQ(0, mDevice->getMetaState());
3353
3354 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
3355 << "Ignored device should return unknown key code state.";
3356 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
3357 << "Ignored device should return unknown scan code state.";
3358 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
3359 << "Ignored device should return unknown switch state.";
3360
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003361 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08003362 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003363 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364 << "Ignored device should never mark any key codes.";
3365 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
3366 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
3367}
3368
3369TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
3370 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003371 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08003372
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003373 FakeInputMapper& mapper1 =
3374 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003375 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3376 mapper1.setMetaState(AMETA_ALT_ON);
3377 mapper1.addSupportedKeyCode(AKEYCODE_A);
3378 mapper1.addSupportedKeyCode(AKEYCODE_B);
3379 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
3380 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
3381 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
3382 mapper1.setScanCodeState(3, AKEY_STATE_UP);
3383 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003384
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003385 FakeInputMapper& mapper2 =
3386 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003387 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003388
3389 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003390 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003391
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003392 std::string propertyValue;
3393 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003394 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003395 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003396
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003397 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
3398 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003399
3400 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003401 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003402 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
3403 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003404
3405 NotifyDeviceResetArgs resetArgs;
3406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3407 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3408 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3409
3410 // Metadata.
3411 ASSERT_FALSE(mDevice->isIgnored());
3412 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
3413
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00003414 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003415 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003416 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003417 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
3418 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
3419
3420 // State queries.
3421 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
3422 << "Should query mappers and combine meta states.";
3423
3424 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3425 << "Should return unknown key code state when source not supported.";
3426 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3427 << "Should return unknown scan code state when source not supported.";
3428 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3429 << "Should return unknown switch state when source not supported.";
3430
3431 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
3432 << "Should query mapper when source is supported.";
3433 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
3434 << "Should query mapper when source is supported.";
3435 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
3436 << "Should query mapper when source is supported.";
3437
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003438 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08003439 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003440 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441 << "Should do nothing when source is unsupported.";
3442 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
3443 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
3444 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
3445 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
3446
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003447 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448 << "Should query mapper when source is supported.";
3449 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
3450 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
3451 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
3452 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
3453
3454 // Event handling.
3455 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003456 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003457 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003458
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003459 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
3460 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003461}
3462
Arthur Hung2c9a3342019-07-23 14:18:59 +08003463// A single input device is associated with a specific display. Check that:
3464// 1. Device is disabled if the viewport corresponding to the associated display is not found
3465// 2. Device is disabled when setEnabled API is called
3466TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003467 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003468
3469 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003470 std::list<NotifyArgs> unused =
3471 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003472
3473 // Device should be enabled by default.
3474 ASSERT_TRUE(mDevice->isEnabled());
3475
3476 // Prepare associated info.
3477 constexpr uint8_t hdmi = 1;
3478 const std::string UNIQUE_ID = "local:1";
3479
3480 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003481 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3482 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003483 // Device should be disabled because it is associated with a specific display via
3484 // input port <-> display port association, but the corresponding display is not found
3485 ASSERT_FALSE(mDevice->isEnabled());
3486
3487 // Prepare displays.
3488 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003489 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
3490 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003491 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3492 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003493 ASSERT_TRUE(mDevice->isEnabled());
3494
3495 // Device should be disabled after set disable.
3496 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003497 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3498 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003499 ASSERT_FALSE(mDevice->isEnabled());
3500
3501 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003502 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3503 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003504 ASSERT_FALSE(mDevice->isEnabled());
3505}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003506
Christine Franks1ba71cc2021-04-07 14:37:42 -07003507TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
3508 // Device should be enabled by default.
3509 mFakePolicy->clearViewports();
3510 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003511 std::list<NotifyArgs> unused =
3512 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003513 ASSERT_TRUE(mDevice->isEnabled());
3514
3515 // Device should be disabled because it is associated with a specific display, but the
3516 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08003517 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003518 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3519 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003520 ASSERT_FALSE(mDevice->isEnabled());
3521
3522 // Device should be enabled when a display is found.
3523 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3524 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3525 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003526 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3527 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003528 ASSERT_TRUE(mDevice->isEnabled());
3529
3530 // Device should be disabled after set disable.
3531 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003532 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3533 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003534 ASSERT_FALSE(mDevice->isEnabled());
3535
3536 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003537 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3538 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003539 ASSERT_FALSE(mDevice->isEnabled());
3540}
3541
Christine Franks2a2293c2022-01-18 11:51:16 -08003542TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
3543 mFakePolicy->clearViewports();
3544 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003545 std::list<NotifyArgs> unused =
3546 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08003547
Christine Franks2a2293c2022-01-18 11:51:16 -08003548 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
3549 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3550 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3551 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003552 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3553 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08003554 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
3555}
3556
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07003557/**
3558 * This test reproduces a crash caused by a dangling reference that remains after device is added
3559 * and removed. The reference is accessed in InputDevice::dump(..);
3560 */
3561TEST_F(InputDeviceTest, DumpDoesNotCrash) {
3562 constexpr int32_t TEST_EVENTHUB_ID = 10;
3563 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
3564
3565 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
3566 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
3567 device.removeEventHubDevice(TEST_EVENTHUB_ID);
3568 std::string dumpStr, eventHubDevStr;
3569 device.dump(dumpStr, eventHubDevStr);
3570}
3571
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003572TEST_F(InputDeviceTest, GetBluetoothAddress) {
3573 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
3574 ASSERT_TRUE(address);
3575 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
3576}
3577
Michael Wrightd02c5b62014-02-10 15:10:22 -08003578// --- InputMapperTest ---
3579
3580class InputMapperTest : public testing::Test {
3581protected:
3582 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003583 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003584 static const int32_t DEVICE_ID;
3585 static const int32_t DEVICE_GENERATION;
3586 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003587 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003588 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003589
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003590 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003591 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003592 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08003593 std::unique_ptr<InstrumentedInputReader> mReader;
3594 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003595
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003596 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003597 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003598 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003599 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08003600 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003601 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08003602 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003603 // Consume the device reset notification generated when adding a new device.
3604 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003605 }
3606
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003607 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003608 SetUp(DEVICE_CLASSES);
3609 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003610
Chris Yea52ade12020-08-27 16:49:20 -07003611 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003612 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003613 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003614 }
3615
3616 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003617 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003618 }
3619
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003620 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003621 if (!changes ||
3622 (changes &
3623 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3624 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003625 mReader->requestRefreshConfiguration(changes);
3626 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003627 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003628 std::list<NotifyArgs> out =
3629 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003630 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003631 for (const NotifyArgs& args : out) {
3632 mFakeListener->notify(args);
3633 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003634 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003635 return out;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003636 }
3637
arthurhungdcef2dc2020-08-11 14:47:50 +08003638 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3639 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003640 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003641 InputDeviceIdentifier identifier;
3642 identifier.name = name;
3643 identifier.location = location;
3644 std::shared_ptr<InputDevice> device =
3645 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3646 identifier);
3647 mReader->pushNextDevice(device);
3648 mFakeEventHub->addDevice(eventHubId, name, classes);
3649 mReader->loopOnce();
3650 return device;
3651 }
3652
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003653 template <class T, typename... Args>
3654 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003655 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003656 configureDevice(0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003657 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
3658 resetArgList += mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003659 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003660 for (const NotifyArgs& loopArgs : resetArgList) {
3661 mFakeListener->notify(loopArgs);
3662 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003663 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003664 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003665 }
3666
3667 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003668 int32_t orientation, const std::string& uniqueId,
3669 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003670 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3671 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003672 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3673 }
3674
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003675 void clearViewports() {
3676 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003677 }
3678
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003679 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
3680 int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003681 RawEvent event;
3682 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003683 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003684 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003685 event.type = type;
3686 event.code = code;
3687 event.value = value;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003688 std::list<NotifyArgs> processArgList = mapper.process(&event);
3689 for (const NotifyArgs& args : processArgList) {
3690 mFakeListener->notify(args);
3691 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003692 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003693 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003694 return processArgList;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003695 }
3696
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00003697 void resetMapper(InputMapper& mapper, nsecs_t when) {
3698 const auto resetArgs = mapper.reset(when);
3699 for (const auto args : resetArgs) {
3700 mFakeListener->notify(args);
3701 }
3702 // Loop the reader to flush the input listener queue.
3703 mReader->loopOnce();
3704 }
3705
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00003706 std::list<NotifyArgs> handleTimeout(InputMapper& mapper, nsecs_t when) {
3707 std::list<NotifyArgs> generatedArgs = mapper.timeoutExpired(when);
3708 for (const NotifyArgs& args : generatedArgs) {
3709 mFakeListener->notify(args);
3710 }
3711 // Loop the reader to flush the input listener queue.
3712 mReader->loopOnce();
3713 return generatedArgs;
3714 }
3715
Michael Wrightd02c5b62014-02-10 15:10:22 -08003716 static void assertMotionRange(const InputDeviceInfo& info,
3717 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3718 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003719 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003720 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3721 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3722 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3723 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3724 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3725 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3726 }
3727
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003728 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3729 float size, float touchMajor, float touchMinor, float toolMajor,
3730 float toolMinor, float orientation, float distance,
3731 float scaledAxisEpsilon = 1.f) {
3732 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3733 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003734 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3735 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003736 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3737 scaledAxisEpsilon);
3738 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3739 scaledAxisEpsilon);
3740 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3741 scaledAxisEpsilon);
3742 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3743 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003744 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3745 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3746 }
3747
Michael Wright17db18e2020-06-26 20:51:44 +01003748 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003749 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003750 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003751 ASSERT_NEAR(x, actualX, 1);
3752 ASSERT_NEAR(y, actualY, 1);
3753 }
3754};
3755
3756const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003757const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003758const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003759const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3760const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003761const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3762 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003763const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003764
3765// --- SwitchInputMapperTest ---
3766
3767class SwitchInputMapperTest : public InputMapperTest {
3768protected:
3769};
3770
3771TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003772 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003773
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003774 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003775}
3776
3777TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003778 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003779
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003780 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003781 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003782
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003783 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003784 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003785}
3786
3787TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003788 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003789 std::list<NotifyArgs> out;
3790 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3791 ASSERT_TRUE(out.empty());
3792 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3793 ASSERT_TRUE(out.empty());
3794 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3795 ASSERT_TRUE(out.empty());
3796 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003798 ASSERT_EQ(1u, out.size());
3799 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003800 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003801 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3802 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003803 args.switchMask);
3804 ASSERT_EQ(uint32_t(0), args.policyFlags);
3805}
3806
Chris Ye87143712020-11-10 05:05:58 +00003807// --- VibratorInputMapperTest ---
3808class VibratorInputMapperTest : public InputMapperTest {
3809protected:
3810 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3811};
3812
3813TEST_F(VibratorInputMapperTest, GetSources) {
3814 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3815
3816 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3817}
3818
3819TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3820 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3821
3822 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3823}
3824
3825TEST_F(VibratorInputMapperTest, Vibrate) {
3826 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003827 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003828 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3829
3830 VibrationElement pattern(2);
3831 VibrationSequence sequence(2);
3832 pattern.duration = std::chrono::milliseconds(200);
3833 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3834 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3835 sequence.addElement(pattern);
3836 pattern.duration = std::chrono::milliseconds(500);
3837 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3838 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3839 sequence.addElement(pattern);
3840
3841 std::vector<int64_t> timings = {0, 1};
3842 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3843
3844 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003845 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003846 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003847 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003848 // Verify vibrator state listener was notified.
3849 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003850 ASSERT_EQ(1u, out.size());
3851 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3852 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3853 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003854 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003855 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003856 ASSERT_FALSE(mapper.isVibrating());
3857 // Verify vibrator state listener was notified.
3858 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003859 ASSERT_EQ(1u, out.size());
3860 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3861 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3862 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003863}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003864
Chris Yef59a2f42020-10-16 12:55:26 -07003865// --- SensorInputMapperTest ---
3866
3867class SensorInputMapperTest : public InputMapperTest {
3868protected:
3869 static const int32_t ACCEL_RAW_MIN;
3870 static const int32_t ACCEL_RAW_MAX;
3871 static const int32_t ACCEL_RAW_FUZZ;
3872 static const int32_t ACCEL_RAW_FLAT;
3873 static const int32_t ACCEL_RAW_RESOLUTION;
3874
3875 static const int32_t GYRO_RAW_MIN;
3876 static const int32_t GYRO_RAW_MAX;
3877 static const int32_t GYRO_RAW_FUZZ;
3878 static const int32_t GYRO_RAW_FLAT;
3879 static const int32_t GYRO_RAW_RESOLUTION;
3880
3881 static const float GRAVITY_MS2_UNIT;
3882 static const float DEGREE_RADIAN_UNIT;
3883
3884 void prepareAccelAxes();
3885 void prepareGyroAxes();
3886 void setAccelProperties();
3887 void setGyroProperties();
3888 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3889};
3890
3891const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3892const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3893const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3894const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3895const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3896
3897const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3898const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3899const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3900const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3901const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3902
3903const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3904const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3905
3906void SensorInputMapperTest::prepareAccelAxes() {
3907 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3908 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3909 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3910 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3911 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3912 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3913}
3914
3915void SensorInputMapperTest::prepareGyroAxes() {
3916 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3917 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3918 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3919 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3920 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3921 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3922}
3923
3924void SensorInputMapperTest::setAccelProperties() {
3925 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3926 /* sensorDataIndex */ 0);
3927 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3928 /* sensorDataIndex */ 1);
3929 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3930 /* sensorDataIndex */ 2);
3931 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3932 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3933 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3934 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3935 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3936}
3937
3938void SensorInputMapperTest::setGyroProperties() {
3939 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3940 /* sensorDataIndex */ 0);
3941 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3942 /* sensorDataIndex */ 1);
3943 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3944 /* sensorDataIndex */ 2);
3945 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3946 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3947 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3948 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3949 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3950}
3951
3952TEST_F(SensorInputMapperTest, GetSources) {
3953 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3954
3955 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3956}
3957
3958TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3959 setAccelProperties();
3960 prepareAccelAxes();
3961 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3962
3963 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3964 std::chrono::microseconds(10000),
3965 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003966 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003967 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3968 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3970 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3971 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003972
3973 NotifySensorArgs args;
3974 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3975 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3976 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3977
3978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3979 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3980 ASSERT_EQ(args.deviceId, DEVICE_ID);
3981 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3982 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3983 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3984 ASSERT_EQ(args.values, values);
3985 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3986}
3987
3988TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3989 setGyroProperties();
3990 prepareGyroAxes();
3991 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3992
3993 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3994 std::chrono::microseconds(10000),
3995 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003996 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003997 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3998 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
4000 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
4001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07004002
4003 NotifySensorArgs args;
4004 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
4005 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
4006 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
4007
4008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
4009 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
4010 ASSERT_EQ(args.deviceId, DEVICE_ID);
4011 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
4012 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
4013 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
4014 ASSERT_EQ(args.values, values);
4015 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
4016}
4017
Michael Wrightd02c5b62014-02-10 15:10:22 -08004018// --- KeyboardInputMapperTest ---
4019
4020class KeyboardInputMapperTest : public InputMapperTest {
4021protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004022 const std::string UNIQUE_ID = "local:0";
4023
4024 void prepareDisplay(int32_t orientation);
4025
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004026 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08004027 int32_t originalKeyCode, int32_t rotatedKeyCode,
4028 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004029};
4030
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004031/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
4032 * orientation.
4033 */
4034void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004035 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4036 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004037}
4038
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004039void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08004040 int32_t originalScanCode, int32_t originalKeyCode,
4041 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004042 NotifyKeyArgs args;
4043
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4046 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4047 ASSERT_EQ(originalScanCode, args.scanCode);
4048 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004049 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004050
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004051 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4053 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4054 ASSERT_EQ(originalScanCode, args.scanCode);
4055 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004056 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004057}
4058
Michael Wrightd02c5b62014-02-10 15:10:22 -08004059TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004060 KeyboardInputMapper& mapper =
4061 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4062 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004063
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004064 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004065}
4066
4067TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
4068 const int32_t USAGE_A = 0x070004;
4069 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004070 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4071 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07004072 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
4073 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
4074 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004076 KeyboardInputMapper& mapper =
4077 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4078 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
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 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004083 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004084 NotifyKeyArgs args;
4085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4086 ASSERT_EQ(DEVICE_ID, args.deviceId);
4087 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4088 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4089 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4090 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4091 ASSERT_EQ(KEY_HOME, args.scanCode);
4092 ASSERT_EQ(AMETA_NONE, args.metaState);
4093 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4094 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4095 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4096
4097 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004098 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4100 ASSERT_EQ(DEVICE_ID, args.deviceId);
4101 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4102 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4103 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4104 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4105 ASSERT_EQ(KEY_HOME, args.scanCode);
4106 ASSERT_EQ(AMETA_NONE, args.metaState);
4107 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4108 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4109 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4110
4111 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004112 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
4113 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4115 ASSERT_EQ(DEVICE_ID, args.deviceId);
4116 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4117 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4118 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4119 ASSERT_EQ(AKEYCODE_A, args.keyCode);
4120 ASSERT_EQ(0, args.scanCode);
4121 ASSERT_EQ(AMETA_NONE, args.metaState);
4122 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4123 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4124 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4125
4126 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004127 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
4128 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4130 ASSERT_EQ(DEVICE_ID, args.deviceId);
4131 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4132 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4133 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4134 ASSERT_EQ(AKEYCODE_A, args.keyCode);
4135 ASSERT_EQ(0, args.scanCode);
4136 ASSERT_EQ(AMETA_NONE, args.metaState);
4137 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4138 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4139 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4140
4141 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004142 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
4143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4145 ASSERT_EQ(DEVICE_ID, args.deviceId);
4146 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4147 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4148 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4149 ASSERT_EQ(0, args.keyCode);
4150 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
4151 ASSERT_EQ(AMETA_NONE, args.metaState);
4152 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4153 ASSERT_EQ(0U, args.policyFlags);
4154 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4155
4156 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
4158 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4160 ASSERT_EQ(DEVICE_ID, args.deviceId);
4161 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4162 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4163 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4164 ASSERT_EQ(0, args.keyCode);
4165 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
4166 ASSERT_EQ(AMETA_NONE, args.metaState);
4167 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4168 ASSERT_EQ(0U, args.policyFlags);
4169 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4170}
4171
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004172/**
4173 * Ensure that the readTime is set to the time when the EV_KEY is received.
4174 */
4175TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
4176 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4177
4178 KeyboardInputMapper& mapper =
4179 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4180 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4181 NotifyKeyArgs args;
4182
4183 // Key down
4184 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
4185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4186 ASSERT_EQ(12, args.readTime);
4187
4188 // Key up
4189 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
4190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4191 ASSERT_EQ(15, args.readTime);
4192}
4193
Michael Wrightd02c5b62014-02-10 15:10:22 -08004194TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004195 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
4196 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07004197 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
4198 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
4199 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004200
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004201 KeyboardInputMapper& mapper =
4202 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4203 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004204
Arthur Hung95f68612022-04-07 14:08:22 +08004205 // Initial metastate is AMETA_NONE.
4206 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004207
4208 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004209 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004210 NotifyKeyArgs args;
4211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4212 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004213 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08004214 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215
4216 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004217 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4219 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004220 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004221
4222 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004223 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4225 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004226 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004227
4228 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004229 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4231 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004232 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08004233 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004234}
4235
4236TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004237 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4238 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4239 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4240 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004242 KeyboardInputMapper& mapper =
4243 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4244 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004245
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004246 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004247 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4248 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
4249 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4250 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
4251 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4252 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
4253 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4254 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
4255}
4256
4257TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004258 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4259 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4260 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4261 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004262
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263 addConfigurationProperty("keyboard.orientationAware", "1");
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
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004268 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004269 ASSERT_NO_FATAL_FAILURE(
4270 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4271 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4272 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4273 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4274 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4275 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4276 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004278 clearViewports();
4279 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004280 ASSERT_NO_FATAL_FAILURE(
4281 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4282 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4283 AKEYCODE_DPAD_UP, DISPLAY_ID));
4284 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4285 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4286 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4287 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004288
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004289 clearViewports();
4290 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004291 ASSERT_NO_FATAL_FAILURE(
4292 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4293 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4294 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4295 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4296 AKEYCODE_DPAD_UP, DISPLAY_ID));
4297 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4298 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004299
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004300 clearViewports();
4301 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004302 ASSERT_NO_FATAL_FAILURE(
4303 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4304 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4305 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4306 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4307 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4308 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4309 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310
4311 // Special case: if orientation changes while key is down, we still emit the same keycode
4312 // in the key up as we did in the key down.
4313 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004314 clearViewports();
4315 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4318 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4319 ASSERT_EQ(KEY_UP, args.scanCode);
4320 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
4321
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004322 clearViewports();
4323 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004324 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4326 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4327 ASSERT_EQ(KEY_UP, args.scanCode);
4328 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
4329}
4330
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004331TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
4332 // If the keyboard is not orientation aware,
4333 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004334 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004335
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004336 KeyboardInputMapper& mapper =
4337 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4338 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004339 NotifyKeyArgs args;
4340
4341 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4346 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
4347
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004348 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004349 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4353 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
4354}
4355
4356TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
4357 // If the keyboard is orientation aware,
4358 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004359 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004360
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004361 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004362 KeyboardInputMapper& mapper =
4363 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4364 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004365 NotifyKeyArgs args;
4366
4367 // Display id should be ADISPLAY_ID_NONE without any display configuration.
4368 // ^--- already checked by the previous test
4369
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004370 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004371 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004372 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004374 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4376 ASSERT_EQ(DISPLAY_ID, args.displayId);
4377
4378 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004379 clearViewports();
4380 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004381 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004382 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004384 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4386 ASSERT_EQ(newDisplayId, args.displayId);
4387}
4388
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004390 KeyboardInputMapper& mapper =
4391 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4392 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004393
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004394 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004395 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004396
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004397 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004398 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004399}
4400
Philip Junker4af3b3d2021-12-14 10:36:55 +01004401TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
4402 KeyboardInputMapper& mapper =
4403 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4404 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4405
4406 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
4407 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
4408 << "If a mapping is available, the result is equal to the mapping";
4409
4410 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
4411 << "If no mapping is available, the result is the key location";
4412}
4413
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004415 KeyboardInputMapper& mapper =
4416 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4417 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004418
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004419 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004420 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004422 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004423 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424}
4425
4426TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004427 KeyboardInputMapper& mapper =
4428 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4429 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004430
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004431 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432
Michael Wrightd02c5b62014-02-10 15:10:22 -08004433 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004434 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435 ASSERT_TRUE(flags[0]);
4436 ASSERT_FALSE(flags[1]);
4437}
4438
4439TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004440 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4441 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4442 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4443 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4444 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4445 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004446
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004447 KeyboardInputMapper& mapper =
4448 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4449 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004450 // Initial metastate is AMETA_NONE.
4451 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452
4453 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004454 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4455 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4456 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004457
4458 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4460 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004461 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4462 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4463 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004464 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004465
4466 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004467 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004469 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4470 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4471 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004472 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473
4474 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004477 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4478 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4479 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004480 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004481
4482 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004483 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4484 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004485 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4486 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4487 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004488 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004489
4490 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4492 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004493 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4494 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4495 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004496 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497
4498 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004499 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004501 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4502 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4503 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004504 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004505}
4506
Chris Yea52ade12020-08-27 16:49:20 -07004507TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
4508 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
4509 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
4510 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
4511 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
4512
4513 KeyboardInputMapper& mapper =
4514 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4515 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4516
Chris Yea52ade12020-08-27 16:49:20 -07004517 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004518 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07004519 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4520 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
4521 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
4522 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4523
4524 NotifyKeyArgs args;
4525 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07004527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4528 ASSERT_EQ(AMETA_NONE, args.metaState);
4529 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4530 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4531 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4532
4533 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004534 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07004535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4536 ASSERT_EQ(AMETA_NONE, args.metaState);
4537 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4538 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4539 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4540}
4541
Arthur Hung2c9a3342019-07-23 14:18:59 +08004542TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
4543 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004544 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4545 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4546 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4547 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004548
4549 // keyboard 2.
4550 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08004551 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08004552 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004553 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08004554 std::shared_ptr<InputDevice> device2 =
4555 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004556 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08004557
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004558 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4559 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4560 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4561 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004562
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004563 KeyboardInputMapper& mapper =
4564 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4565 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004566
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004567 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004568 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004569 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004570 std::list<NotifyArgs> unused =
4571 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4572 0 /*changes*/);
4573 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004574
4575 // Prepared displays and associated info.
4576 constexpr uint8_t hdmi1 = 0;
4577 constexpr uint8_t hdmi2 = 1;
4578 const std::string SECONDARY_UNIQUE_ID = "local:1";
4579
4580 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
4581 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
4582
4583 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004584 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4585 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004586 ASSERT_FALSE(device2->isEnabled());
4587
4588 // Prepare second display.
4589 constexpr int32_t newDisplayId = 2;
4590 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004591 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004592 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004593 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004594 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004595 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4596 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004597
4598 // Device should be enabled after the associated display is found.
4599 ASSERT_TRUE(mDevice->isEnabled());
4600 ASSERT_TRUE(device2->isEnabled());
4601
4602 // Test pad key events
4603 ASSERT_NO_FATAL_FAILURE(
4604 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4605 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4606 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4607 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4608 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4609 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4610 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4611
4612 ASSERT_NO_FATAL_FAILURE(
4613 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
4614 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4615 AKEYCODE_DPAD_RIGHT, newDisplayId));
4616 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4617 AKEYCODE_DPAD_DOWN, newDisplayId));
4618 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4619 AKEYCODE_DPAD_LEFT, newDisplayId));
4620}
Michael Wrightd02c5b62014-02-10 15:10:22 -08004621
arthurhungc903df12020-08-11 15:08:42 +08004622TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
4623 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4624 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4625 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4626 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4627 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4628 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4629
4630 KeyboardInputMapper& mapper =
4631 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4632 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004633 // Initial metastate is AMETA_NONE.
4634 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004635
4636 // Initialization should have turned all of the lights off.
4637 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4638 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4639 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4640
4641 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004642 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4643 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004644 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4645 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4646
4647 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004648 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4649 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004650 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4651 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
4652
4653 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4655 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004656 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4657 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4658
4659 mFakeEventHub->removeDevice(EVENTHUB_ID);
4660 mReader->loopOnce();
4661
4662 // keyboard 2 should default toggle keys.
4663 const std::string USB2 = "USB2";
4664 const std::string DEVICE_NAME2 = "KEYBOARD2";
4665 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4666 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4667 std::shared_ptr<InputDevice> device2 =
4668 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004669 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004670 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4671 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4672 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4673 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4674 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4675 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4676
arthurhung6fe95782020-10-05 22:41:16 +08004677 KeyboardInputMapper& mapper2 =
4678 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4679 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004680 std::list<NotifyArgs> unused =
4681 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4682 0 /*changes*/);
4683 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08004684
4685 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4686 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4687 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004688 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4689 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004690}
4691
Arthur Hungcb40a002021-08-03 14:31:01 +00004692TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4693 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4694 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4695 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4696
4697 // Suppose we have two mappers. (DPAD + KEYBOARD)
4698 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4699 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4700 KeyboardInputMapper& mapper =
4701 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4702 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004703 // Initial metastate is AMETA_NONE.
4704 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004705
4706 mReader->toggleCapsLockState(DEVICE_ID);
4707 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4708}
4709
Arthur Hungfb3cc112022-04-13 07:39:50 +00004710TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4711 // keyboard 1.
4712 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4713 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4714 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4715 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4716 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4717 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4718
4719 KeyboardInputMapper& mapper1 =
4720 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4721 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4722
4723 // keyboard 2.
4724 const std::string USB2 = "USB2";
4725 const std::string DEVICE_NAME2 = "KEYBOARD2";
4726 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4727 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4728 std::shared_ptr<InputDevice> device2 =
4729 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4730 ftl::Flags<InputDeviceClass>(0));
4731 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4732 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4733 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4734 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4735 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4736 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4737
4738 KeyboardInputMapper& mapper2 =
4739 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4740 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004741 std::list<NotifyArgs> unused =
4742 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4743 0 /*changes*/);
4744 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004745
Arthur Hung95f68612022-04-07 14:08:22 +08004746 // Initial metastate is AMETA_NONE.
4747 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4748 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4749
4750 // Toggle num lock on and off.
4751 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4752 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004753 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4754 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4755 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4756
4757 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4758 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4759 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4760 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4761 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4762
4763 // Toggle caps lock on and off.
4764 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4765 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4766 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4767 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4768 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4769
4770 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4771 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4772 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4773 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4774 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4775
4776 // Toggle scroll lock on and off.
4777 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4778 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4779 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4780 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4781 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4782
4783 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4784 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4785 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4786 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4787 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4788}
4789
Arthur Hung2141d542022-08-23 07:45:21 +00004790TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4791 const int32_t USAGE_A = 0x070004;
4792 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4793 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4794
4795 KeyboardInputMapper& mapper =
4796 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4797 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4798 // Key down by scan code.
4799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4800 NotifyKeyArgs args;
4801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4802 ASSERT_EQ(DEVICE_ID, args.deviceId);
4803 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4804 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4805 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4806 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4807 ASSERT_EQ(KEY_HOME, args.scanCode);
4808 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4809
4810 // Disable device, it should synthesize cancellation events for down events.
4811 mFakePolicy->addDisabledDevice(DEVICE_ID);
4812 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4813
4814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4815 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4816 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4817 ASSERT_EQ(KEY_HOME, args.scanCode);
4818 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4819}
4820
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004821// --- KeyboardInputMapperTest_ExternalDevice ---
4822
4823class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4824protected:
Chris Yea52ade12020-08-27 16:49:20 -07004825 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004826};
4827
4828TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004829 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4830 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004831
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004832 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4833 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4834 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4835 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004836
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004837 KeyboardInputMapper& mapper =
4838 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4839 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004840
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004841 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004842 NotifyKeyArgs args;
4843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4844 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4845
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004846 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4848 ASSERT_EQ(uint32_t(0), args.policyFlags);
4849
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004850 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4852 ASSERT_EQ(uint32_t(0), args.policyFlags);
4853
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004854 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4856 ASSERT_EQ(uint32_t(0), args.policyFlags);
4857
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004858 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4860 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4861
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004862 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4864 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4865}
4866
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004867TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004868 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004869
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004870 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4871 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4872 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004873
Powei Fengd041c5d2019-05-03 17:11:33 -07004874 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004875 KeyboardInputMapper& mapper =
4876 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4877 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004878
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004879 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004880 NotifyKeyArgs args;
4881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4882 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4883
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004884 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4886 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4887
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004888 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4890 ASSERT_EQ(uint32_t(0), args.policyFlags);
4891
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004892 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4894 ASSERT_EQ(uint32_t(0), args.policyFlags);
4895
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004896 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4898 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4899
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004900 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4902 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4903}
4904
Michael Wrightd02c5b62014-02-10 15:10:22 -08004905// --- CursorInputMapperTest ---
4906
4907class CursorInputMapperTest : public InputMapperTest {
4908protected:
4909 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4910
Michael Wright17db18e2020-06-26 20:51:44 +01004911 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912
Chris Yea52ade12020-08-27 16:49:20 -07004913 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004914 InputMapperTest::SetUp();
4915
Michael Wright17db18e2020-06-26 20:51:44 +01004916 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004917 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004918 }
4919
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004920 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4921 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004922
4923 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004924 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4925 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4926 }
4927
4928 void prepareSecondaryDisplay() {
4929 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4930 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4931 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004932 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004933
4934 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4935 float pressure) {
4936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4937 0.0f, 0.0f, 0.0f, EPSILON));
4938 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004939};
4940
4941const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4942
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004943void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4944 int32_t originalY, int32_t rotatedX,
4945 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004946 NotifyMotionArgs args;
4947
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4952 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004953 ASSERT_NO_FATAL_FAILURE(
4954 assertCursorPointerCoords(args.pointerCoords[0],
4955 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4956 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004957}
4958
4959TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004960 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004961 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004962
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004963 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964}
4965
4966TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004967 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004968 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004969
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004970 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004971}
4972
4973TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004975 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976
4977 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004978 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004979
4980 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004981 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4982 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004983 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4984 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4985
4986 // When the bounds are set, then there should be a valid motion range.
4987 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4988
4989 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004990 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004991
4992 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4993 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4994 1, 800 - 1, 0.0f, 0.0f));
4995 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4996 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4997 2, 480 - 1, 0.0f, 0.0f));
4998 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4999 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
5000 0.0f, 1.0f, 0.0f, 0.0f));
5001}
5002
5003TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005005 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005006
5007 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005008 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009
5010 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5011 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
5012 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
5013 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5014 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
5015 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
5016 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5017 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
5018 0.0f, 1.0f, 0.0f, 0.0f));
5019}
5020
5021TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005022 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005023 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005024
arthurhungdcef2dc2020-08-11 14:47:50 +08005025 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005026
5027 NotifyMotionArgs args;
5028
5029 // Button press.
5030 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5032 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5034 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5035 ASSERT_EQ(DEVICE_ID, args.deviceId);
5036 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
5037 ASSERT_EQ(uint32_t(0), args.policyFlags);
5038 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5039 ASSERT_EQ(0, args.flags);
5040 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5041 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
5042 ASSERT_EQ(0, args.edgeFlags);
5043 ASSERT_EQ(uint32_t(1), args.pointerCount);
5044 ASSERT_EQ(0, args.pointerProperties[0].id);
5045 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005046 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005047 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
5048 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
5049 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5050
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5052 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5053 ASSERT_EQ(DEVICE_ID, args.deviceId);
5054 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
5055 ASSERT_EQ(uint32_t(0), args.policyFlags);
5056 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
5057 ASSERT_EQ(0, args.flags);
5058 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5059 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
5060 ASSERT_EQ(0, args.edgeFlags);
5061 ASSERT_EQ(uint32_t(1), args.pointerCount);
5062 ASSERT_EQ(0, args.pointerProperties[0].id);
5063 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005064 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005065 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
5066 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
5067 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5068
Michael Wrightd02c5b62014-02-10 15:10:22 -08005069 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005070 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5071 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5073 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
5074 ASSERT_EQ(DEVICE_ID, args.deviceId);
5075 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
5076 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005077 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
5078 ASSERT_EQ(0, args.flags);
5079 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5080 ASSERT_EQ(0, args.buttonState);
5081 ASSERT_EQ(0, args.edgeFlags);
5082 ASSERT_EQ(uint32_t(1), args.pointerCount);
5083 ASSERT_EQ(0, args.pointerProperties[0].id);
5084 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005085 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005086 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
5087 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
5088 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5089
5090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5091 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
5092 ASSERT_EQ(DEVICE_ID, args.deviceId);
5093 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
5094 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005095 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
5096 ASSERT_EQ(0, args.flags);
5097 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5098 ASSERT_EQ(0, args.buttonState);
5099 ASSERT_EQ(0, args.edgeFlags);
5100 ASSERT_EQ(uint32_t(1), args.pointerCount);
5101 ASSERT_EQ(0, args.pointerProperties[0].id);
5102 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005103 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005104 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
5105 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
5106 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5107}
5108
5109TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005110 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005111 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005112
5113 NotifyMotionArgs args;
5114
5115 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005116 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5117 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5119 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005120 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
5121 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
5122 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123
5124 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
5126 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005129 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
5130 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005131}
5132
5133TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005134 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005135 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005136
5137 NotifyMotionArgs args;
5138
5139 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5143 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005144 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005145
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5147 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005148 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005149
Michael Wrightd02c5b62014-02-10 15:10:22 -08005150 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5152 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005154 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005155 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005156
5157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005158 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005159 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005160}
5161
5162TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005163 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005164 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005165
5166 NotifyMotionArgs args;
5167
5168 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
5171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5174 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005175 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
5176 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
5177 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005178
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5180 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005181 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
5182 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
5183 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005184
Michael Wrightd02c5b62014-02-10 15:10:22 -08005185 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
5187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
5188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005191 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
5192 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
5193 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005194
5195 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005196 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005199 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005200 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005201
5202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005203 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005204 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005205}
5206
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005207TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005208 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005209 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005210 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5211 // need to be rotated.
5212 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005213 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005214
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005215 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005216 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
5217 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
5218 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
5219 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
5220 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
5221 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
5222 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
5223 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
5224}
5225
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005226TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005227 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005228 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005229 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5230 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005231 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005232
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005233 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005234 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005235 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
5236 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
5237 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
5238 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
5239 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
5240 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
5241 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
5242 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
5243
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005244 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005245 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005246 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
5247 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
5248 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
5249 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
5250 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
5251 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
5252 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
5253 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005254
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005255 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005256 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005257 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
5258 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
5259 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
5260 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
5261 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
5262 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
5263 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
5264 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
5265
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005266 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005267 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005268 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
5269 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
5270 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
5271 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
5272 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
5273 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
5274 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
5275 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005276}
5277
5278TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005279 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005280 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005281
5282 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5283 mFakePointerController->setPosition(100, 200);
5284 mFakePointerController->setButtonState(0);
5285
5286 NotifyMotionArgs motionArgs;
5287 NotifyKeyArgs keyArgs;
5288
5289 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
5291 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5293 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5294 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5295 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005296 ASSERT_NO_FATAL_FAILURE(
5297 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005298
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5300 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5301 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5302 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005303 ASSERT_NO_FATAL_FAILURE(
5304 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005305
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005306 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
5307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005309 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005310 ASSERT_EQ(0, motionArgs.buttonState);
5311 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005312 ASSERT_NO_FATAL_FAILURE(
5313 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005314
5315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005316 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005317 ASSERT_EQ(0, motionArgs.buttonState);
5318 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005319 ASSERT_NO_FATAL_FAILURE(
5320 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005321
5322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005323 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005324 ASSERT_EQ(0, motionArgs.buttonState);
5325 ASSERT_EQ(0, 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005328
5329 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005330 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
5331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
5332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5334 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5335 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5336 motionArgs.buttonState);
5337 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5338 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005339 ASSERT_NO_FATAL_FAILURE(
5340 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005341
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5343 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5344 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5345 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5346 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005347 ASSERT_NO_FATAL_FAILURE(
5348 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005349
5350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5351 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5352 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5353 motionArgs.buttonState);
5354 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5355 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005356 ASSERT_NO_FATAL_FAILURE(
5357 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005358
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005359 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
5360 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005362 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005363 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5364 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005365 ASSERT_NO_FATAL_FAILURE(
5366 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005367
5368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005370 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5371 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005372 ASSERT_NO_FATAL_FAILURE(
5373 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005375 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005378 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5379 ASSERT_EQ(0, motionArgs.buttonState);
5380 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005381 ASSERT_NO_FATAL_FAILURE(
5382 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5384 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005385
5386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005387 ASSERT_EQ(0, motionArgs.buttonState);
5388 ASSERT_EQ(0, mFakePointerController->getButtonState());
5389 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
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->assertNotifyMotionWasCalled(&motionArgs));
5394 ASSERT_EQ(0, motionArgs.buttonState);
5395 ASSERT_EQ(0, mFakePointerController->getButtonState());
5396 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005397 ASSERT_NO_FATAL_FAILURE(
5398 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005399
5400 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
5402 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5404 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5405 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005406
Michael Wrightd02c5b62014-02-10 15:10:22 -08005407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005408 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005409 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5410 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005411 ASSERT_NO_FATAL_FAILURE(
5412 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005413
5414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5415 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5416 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5417 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005418 ASSERT_NO_FATAL_FAILURE(
5419 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005420
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005421 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
5422 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005424 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005425 ASSERT_EQ(0, motionArgs.buttonState);
5426 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005427 ASSERT_NO_FATAL_FAILURE(
5428 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005429
5430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005431 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005432 ASSERT_EQ(0, motionArgs.buttonState);
5433 ASSERT_EQ(0, mFakePointerController->getButtonState());
5434
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005435 ASSERT_NO_FATAL_FAILURE(
5436 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5438 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5439 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5440
5441 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
5443 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5445 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5446 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005447
Michael Wrightd02c5b62014-02-10 15:10:22 -08005448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005449 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005450 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5451 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005452 ASSERT_NO_FATAL_FAILURE(
5453 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005454
5455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5456 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5457 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5458 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005459 ASSERT_NO_FATAL_FAILURE(
5460 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005461
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005462 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
5463 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005465 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005466 ASSERT_EQ(0, motionArgs.buttonState);
5467 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005468 ASSERT_NO_FATAL_FAILURE(
5469 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005470
5471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5472 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5473 ASSERT_EQ(0, motionArgs.buttonState);
5474 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005475 ASSERT_NO_FATAL_FAILURE(
5476 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005477
Michael Wrightd02c5b62014-02-10 15:10:22 -08005478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5479 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5480 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5481
5482 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005483 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
5484 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5486 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5487 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005488
Michael Wrightd02c5b62014-02-10 15:10:22 -08005489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005490 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005491 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5492 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005493 ASSERT_NO_FATAL_FAILURE(
5494 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005495
5496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5498 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5499 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005500 ASSERT_NO_FATAL_FAILURE(
5501 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005502
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005503 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
5504 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005506 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005507 ASSERT_EQ(0, motionArgs.buttonState);
5508 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005509 ASSERT_NO_FATAL_FAILURE(
5510 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005511
5512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5513 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5514 ASSERT_EQ(0, motionArgs.buttonState);
5515 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005516 ASSERT_NO_FATAL_FAILURE(
5517 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005518
Michael Wrightd02c5b62014-02-10 15:10:22 -08005519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5520 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5521 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5522
5523 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005524 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
5525 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5527 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5528 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005529
Michael Wrightd02c5b62014-02-10 15:10:22 -08005530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005531 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005532 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5533 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005534 ASSERT_NO_FATAL_FAILURE(
5535 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005536
5537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5538 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5539 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5540 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005541 ASSERT_NO_FATAL_FAILURE(
5542 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005543
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
5545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005547 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005548 ASSERT_EQ(0, motionArgs.buttonState);
5549 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005550 ASSERT_NO_FATAL_FAILURE(
5551 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005552
5553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5554 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5555 ASSERT_EQ(0, motionArgs.buttonState);
5556 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005557 ASSERT_NO_FATAL_FAILURE(
5558 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005559
Michael Wrightd02c5b62014-02-10 15:10:22 -08005560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5561 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5562 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5563}
5564
5565TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005566 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005567 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005568
5569 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5570 mFakePointerController->setPosition(100, 200);
5571 mFakePointerController->setButtonState(0);
5572
5573 NotifyMotionArgs args;
5574
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005575 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5576 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005579 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5580 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5582 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 +01005583 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005584}
5585
5586TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005587 addConfigurationProperty("cursor.mode", "pointer");
5588 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005589 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005590
5591 NotifyDeviceResetArgs resetArgs;
5592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5593 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5594 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5595
5596 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5597 mFakePointerController->setPosition(100, 200);
5598 mFakePointerController->setButtonState(0);
5599
5600 NotifyMotionArgs args;
5601
5602 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005603 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5604 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5605 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5607 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5608 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5609 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5610 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 +01005611 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005612
5613 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5615 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5617 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5618 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5620 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5622 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5623 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
5624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5625 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5626
5627 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005628 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5629 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5631 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5632 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
5633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5634 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5636 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5637 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
5638 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5639 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5640
5641 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005642 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
5643 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
5644 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5646 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5647 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5649 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 +01005650 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005651
5652 // Disable pointer capture and check that the device generation got bumped
5653 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08005654 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005655 mFakePolicy->setPointerCapture(false);
5656 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08005657 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005658
5659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005660 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5661
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005662 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5666 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005667 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5669 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 +01005670 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005671}
5672
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00005673/**
5674 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
5675 * pointer acceleration or speed processing should not be applied.
5676 */
5677TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5678 addConfigurationProperty("cursor.mode", "pointer");
5679 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5680 100.f /*high threshold*/, 10.f /*acceleration*/);
5681 mFakePolicy->setVelocityControlParams(testParams);
5682 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5683
5684 NotifyDeviceResetArgs resetArgs;
5685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5686 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5687 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5688
5689 NotifyMotionArgs args;
5690
5691 // Move and verify scale is applied.
5692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5693 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5694 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5696 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5697 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5698 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5699 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5700 ASSERT_GT(relX, 10);
5701 ASSERT_GT(relY, 20);
5702
5703 // Enable Pointer Capture
5704 mFakePolicy->setPointerCapture(true);
5705 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5706 NotifyPointerCaptureChangedArgs captureArgs;
5707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5708 ASSERT_TRUE(captureArgs.request.enable);
5709
5710 // Move and verify scale is not applied.
5711 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5715 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5717 ASSERT_EQ(10, args.pointerCoords[0].getX());
5718 ASSERT_EQ(20, args.pointerCoords[0].getY());
5719}
5720
Prabir Pradhan208360b2022-06-24 18:37:04 +00005721TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5722 addConfigurationProperty("cursor.mode", "pointer");
5723 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5724
5725 NotifyDeviceResetArgs resetArgs;
5726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5727 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5728 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5729
5730 // Ensure the display is rotated.
5731 prepareDisplay(DISPLAY_ORIENTATION_90);
5732
5733 NotifyMotionArgs args;
5734
5735 // Verify that the coordinates are rotated.
5736 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5737 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5738 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5740 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5741 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5742 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5743 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5744
5745 // Enable Pointer Capture.
5746 mFakePolicy->setPointerCapture(true);
5747 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5748 NotifyPointerCaptureChangedArgs captureArgs;
5749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5750 ASSERT_TRUE(captureArgs.request.enable);
5751
5752 // Move and verify rotation is not applied.
5753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5754 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5755 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5757 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5758 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5759 ASSERT_EQ(10, args.pointerCoords[0].getX());
5760 ASSERT_EQ(20, args.pointerCoords[0].getY());
5761}
5762
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005763TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005764 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005765
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005766 // Set up the default display.
5767 prepareDisplay(DISPLAY_ORIENTATION_90);
5768
5769 // Set up the secondary display as the display on which the pointer should be shown.
5770 // The InputDevice is not associated with any display.
5771 prepareSecondaryDisplay();
5772 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005773 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5774
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005775 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005776 mFakePointerController->setPosition(100, 200);
5777 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005778
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005779 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5782 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005784 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5785 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5786 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005787 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005788}
5789
5790TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5791 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5792
5793 // Set up the default display.
5794 prepareDisplay(DISPLAY_ORIENTATION_90);
5795
5796 // Set up the secondary display as the display on which the pointer should be shown,
5797 // and associate the InputDevice with the secondary display.
5798 prepareSecondaryDisplay();
5799 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5800 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5801 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5802
5803 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5804 mFakePointerController->setPosition(100, 200);
5805 mFakePointerController->setButtonState(0);
5806
5807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5809 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005811 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5812 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5813 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005814 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5815}
5816
5817TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5818 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5819
5820 // Set up the default display as the display on which the pointer should be shown.
5821 prepareDisplay(DISPLAY_ORIENTATION_90);
5822 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5823
5824 // Associate the InputDevice with the secondary display.
5825 prepareSecondaryDisplay();
5826 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5827 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5828
5829 // The mapper should not generate any events because it is associated with a display that is
5830 // different from the pointer display.
5831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005835}
5836
Michael Wrightd02c5b62014-02-10 15:10:22 -08005837// --- TouchInputMapperTest ---
5838
5839class TouchInputMapperTest : public InputMapperTest {
5840protected:
5841 static const int32_t RAW_X_MIN;
5842 static const int32_t RAW_X_MAX;
5843 static const int32_t RAW_Y_MIN;
5844 static const int32_t RAW_Y_MAX;
5845 static const int32_t RAW_TOUCH_MIN;
5846 static const int32_t RAW_TOUCH_MAX;
5847 static const int32_t RAW_TOOL_MIN;
5848 static const int32_t RAW_TOOL_MAX;
5849 static const int32_t RAW_PRESSURE_MIN;
5850 static const int32_t RAW_PRESSURE_MAX;
5851 static const int32_t RAW_ORIENTATION_MIN;
5852 static const int32_t RAW_ORIENTATION_MAX;
5853 static const int32_t RAW_DISTANCE_MIN;
5854 static const int32_t RAW_DISTANCE_MAX;
5855 static const int32_t RAW_TILT_MIN;
5856 static const int32_t RAW_TILT_MAX;
5857 static const int32_t RAW_ID_MIN;
5858 static const int32_t RAW_ID_MAX;
5859 static const int32_t RAW_SLOT_MIN;
5860 static const int32_t RAW_SLOT_MAX;
5861 static const float X_PRECISION;
5862 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005863 static const float X_PRECISION_VIRTUAL;
5864 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005865
5866 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005867 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005868
5869 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5870
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005871 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005872 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005873
Michael Wrightd02c5b62014-02-10 15:10:22 -08005874 enum Axes {
5875 POSITION = 1 << 0,
5876 TOUCH = 1 << 1,
5877 TOOL = 1 << 2,
5878 PRESSURE = 1 << 3,
5879 ORIENTATION = 1 << 4,
5880 MINOR = 1 << 5,
5881 ID = 1 << 6,
5882 DISTANCE = 1 << 7,
5883 TILT = 1 << 8,
5884 SLOT = 1 << 9,
5885 TOOL_TYPE = 1 << 10,
5886 };
5887
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005888 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5889 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005890 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005891 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005892 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005893 int32_t toRawX(float displayX);
5894 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005895 int32_t toRotatedRawX(float displayX);
5896 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005897 float toCookedX(float rawX, float rawY);
5898 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005899 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005900 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005901 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005902 float toDisplayY(int32_t rawY, int32_t displayHeight);
5903
Michael Wrightd02c5b62014-02-10 15:10:22 -08005904};
5905
5906const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5907const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5908const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5909const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5910const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5911const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5912const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5913const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005914const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5915const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005916const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5917const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5918const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5919const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5920const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5921const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5922const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5923const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5924const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5925const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5926const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5927const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005928const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5929 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5930const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5931 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005932const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5933 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005934
5935const float TouchInputMapperTest::GEOMETRIC_SCALE =
5936 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5937 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5938
5939const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5940 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5941 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5942};
5943
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005944void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005945 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5946 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005947}
5948
5949void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5950 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5951 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005952}
5953
Santos Cordonfa5cf462017-04-05 10:37:00 -07005954void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005955 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5956 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5957 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005958}
5959
Michael Wrightd02c5b62014-02-10 15:10:22 -08005960void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005961 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5962 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5963 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5964 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005965}
5966
Jason Gerecke489fda82012-09-07 17:19:40 -07005967void TouchInputMapperTest::prepareLocationCalibration() {
5968 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5969}
5970
Michael Wrightd02c5b62014-02-10 15:10:22 -08005971int32_t TouchInputMapperTest::toRawX(float displayX) {
5972 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5973}
5974
5975int32_t TouchInputMapperTest::toRawY(float displayY) {
5976 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5977}
5978
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005979int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5980 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5981}
5982
5983int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5984 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5985}
5986
Jason Gerecke489fda82012-09-07 17:19:40 -07005987float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5988 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5989 return rawX;
5990}
5991
5992float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5993 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5994 return rawY;
5995}
5996
Michael Wrightd02c5b62014-02-10 15:10:22 -08005997float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005998 return toDisplayX(rawX, DISPLAY_WIDTH);
5999}
6000
6001float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
6002 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006003}
6004
6005float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07006006 return toDisplayY(rawY, DISPLAY_HEIGHT);
6007}
6008
6009float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
6010 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006011}
6012
6013
6014// --- SingleTouchInputMapperTest ---
6015
6016class SingleTouchInputMapperTest : public TouchInputMapperTest {
6017protected:
6018 void prepareButtons();
6019 void prepareAxes(int axes);
6020
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006021 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
6022 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
6023 void processUp(SingleTouchInputMapper& mappery);
6024 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
6025 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
6026 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
6027 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
6028 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
6029 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006030};
6031
6032void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006033 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006034}
6035
6036void SingleTouchInputMapperTest::prepareAxes(int axes) {
6037 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006038 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6039 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006040 }
6041 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006042 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
6043 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006044 }
6045 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006046 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
6047 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006048 }
6049 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006050 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
6051 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006052 }
6053 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006054 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
6055 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006056 }
6057}
6058
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006059void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006060 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
6062 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006063}
6064
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006065void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006066 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
6067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006068}
6069
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006070void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006072}
6073
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006074void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006075 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006076}
6077
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006078void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
6079 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006081}
6082
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006083void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006084 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006085}
6086
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006087void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
6088 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006089 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
6090 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006091}
6092
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006093void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
6094 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006095 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006096}
6097
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006098void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006099 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006100}
6101
Michael Wrightd02c5b62014-02-10 15:10:22 -08006102TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006103 prepareButtons();
6104 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006105 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006106
Harry Cutts16a24cc2022-10-26 15:22:19 +00006107 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006108}
6109
Michael Wrightd02c5b62014-02-10 15:10:22 -08006110TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006111 prepareButtons();
6112 prepareAxes(POSITION);
6113 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006114 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006115
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006116 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006117}
6118
6119TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006120 addConfigurationProperty("touch.deviceType", "touchScreen");
6121 prepareDisplay(DISPLAY_ORIENTATION_0);
6122 prepareButtons();
6123 prepareAxes(POSITION);
6124 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006125 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126
6127 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006128 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129
6130 // Virtual key is down.
6131 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6132 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6133 processDown(mapper, x, y);
6134 processSync(mapper);
6135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
6136
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006137 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006138
6139 // Virtual key is up.
6140 processUp(mapper);
6141 processSync(mapper);
6142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
6143
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006144 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006145}
6146
6147TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006148 addConfigurationProperty("touch.deviceType", "touchScreen");
6149 prepareDisplay(DISPLAY_ORIENTATION_0);
6150 prepareButtons();
6151 prepareAxes(POSITION);
6152 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006153 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006154
6155 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006156 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006157
6158 // Virtual key is down.
6159 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6160 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6161 processDown(mapper, x, y);
6162 processSync(mapper);
6163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
6164
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006165 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006166
6167 // Virtual key is up.
6168 processUp(mapper);
6169 processSync(mapper);
6170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
6171
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006172 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006173}
6174
6175TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006176 addConfigurationProperty("touch.deviceType", "touchScreen");
6177 prepareDisplay(DISPLAY_ORIENTATION_0);
6178 prepareButtons();
6179 prepareAxes(POSITION);
6180 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006181 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182
Michael Wrightd02c5b62014-02-10 15:10:22 -08006183 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07006184 ASSERT_TRUE(
6185 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006186 ASSERT_TRUE(flags[0]);
6187 ASSERT_FALSE(flags[1]);
6188}
6189
6190TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006191 addConfigurationProperty("touch.deviceType", "touchScreen");
6192 prepareDisplay(DISPLAY_ORIENTATION_0);
6193 prepareButtons();
6194 prepareAxes(POSITION);
6195 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006196 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006197
arthurhungdcef2dc2020-08-11 14:47:50 +08006198 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006199
6200 NotifyKeyArgs args;
6201
6202 // Press virtual key.
6203 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6204 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6205 processDown(mapper, x, y);
6206 processSync(mapper);
6207
6208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
6209 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
6210 ASSERT_EQ(DEVICE_ID, args.deviceId);
6211 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
6212 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
6213 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
6214 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
6215 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
6216 ASSERT_EQ(KEY_HOME, args.scanCode);
6217 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
6218 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
6219
6220 // Release virtual key.
6221 processUp(mapper);
6222 processSync(mapper);
6223
6224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
6225 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
6226 ASSERT_EQ(DEVICE_ID, args.deviceId);
6227 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
6228 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
6229 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
6230 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
6231 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
6232 ASSERT_EQ(KEY_HOME, args.scanCode);
6233 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
6234 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
6235
6236 // Should not have sent any motions.
6237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6238}
6239
6240TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006241 addConfigurationProperty("touch.deviceType", "touchScreen");
6242 prepareDisplay(DISPLAY_ORIENTATION_0);
6243 prepareButtons();
6244 prepareAxes(POSITION);
6245 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006246 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006247
arthurhungdcef2dc2020-08-11 14:47:50 +08006248 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006249
6250 NotifyKeyArgs keyArgs;
6251
6252 // Press virtual key.
6253 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6254 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6255 processDown(mapper, x, y);
6256 processSync(mapper);
6257
6258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6259 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
6260 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
6261 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
6262 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
6263 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6264 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
6265 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
6266 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
6267 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
6268 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
6269
6270 // Move out of bounds. This should generate a cancel and a pointer down since we moved
6271 // into the display area.
6272 y -= 100;
6273 processMove(mapper, x, y);
6274 processSync(mapper);
6275
6276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6277 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
6278 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
6279 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
6280 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
6281 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6282 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
6283 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
6284 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
6285 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
6286 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
6287 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
6288
6289 NotifyMotionArgs motionArgs;
6290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6291 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6292 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6293 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6294 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6295 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6296 ASSERT_EQ(0, motionArgs.flags);
6297 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6298 ASSERT_EQ(0, motionArgs.buttonState);
6299 ASSERT_EQ(0, motionArgs.edgeFlags);
6300 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6301 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6302 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6303 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6304 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6305 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6306 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6307 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6308
6309 // Keep moving out of bounds. Should generate a pointer move.
6310 y -= 50;
6311 processMove(mapper, x, y);
6312 processSync(mapper);
6313
6314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6315 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6316 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6317 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6318 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6320 ASSERT_EQ(0, motionArgs.flags);
6321 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6322 ASSERT_EQ(0, motionArgs.buttonState);
6323 ASSERT_EQ(0, motionArgs.edgeFlags);
6324 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6325 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6326 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6327 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6328 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6329 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6330 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6331 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6332
6333 // Release out of bounds. Should generate a pointer up.
6334 processUp(mapper);
6335 processSync(mapper);
6336
6337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6338 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6339 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6340 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6341 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6342 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6343 ASSERT_EQ(0, motionArgs.flags);
6344 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6345 ASSERT_EQ(0, motionArgs.buttonState);
6346 ASSERT_EQ(0, motionArgs.edgeFlags);
6347 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6348 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6349 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6350 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6351 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6352 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6353 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6354 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6355
6356 // Should not have sent any more keys or motions.
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6359}
6360
6361TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006362 addConfigurationProperty("touch.deviceType", "touchScreen");
6363 prepareDisplay(DISPLAY_ORIENTATION_0);
6364 prepareButtons();
6365 prepareAxes(POSITION);
6366 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006367 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006368
arthurhungdcef2dc2020-08-11 14:47:50 +08006369 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006370
6371 NotifyMotionArgs motionArgs;
6372
6373 // Initially go down out of bounds.
6374 int32_t x = -10;
6375 int32_t y = -10;
6376 processDown(mapper, x, y);
6377 processSync(mapper);
6378
6379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6380
6381 // Move into the display area. Should generate a pointer down.
6382 x = 50;
6383 y = 75;
6384 processMove(mapper, x, y);
6385 processSync(mapper);
6386
6387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6388 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6389 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6390 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6391 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6392 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6393 ASSERT_EQ(0, motionArgs.flags);
6394 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6395 ASSERT_EQ(0, motionArgs.buttonState);
6396 ASSERT_EQ(0, motionArgs.edgeFlags);
6397 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6398 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6399 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6401 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6402 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6403 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6404 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6405
6406 // Release. Should generate a pointer up.
6407 processUp(mapper);
6408 processSync(mapper);
6409
6410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6411 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6412 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6413 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6414 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6415 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6416 ASSERT_EQ(0, motionArgs.flags);
6417 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6418 ASSERT_EQ(0, motionArgs.buttonState);
6419 ASSERT_EQ(0, motionArgs.edgeFlags);
6420 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6421 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6422 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6424 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6425 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6426 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6427 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6428
6429 // Should not have sent any more keys or motions.
6430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6432}
6433
Santos Cordonfa5cf462017-04-05 10:37:00 -07006434TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07006435 addConfigurationProperty("touch.deviceType", "touchScreen");
6436 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
6437
6438 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
6439 prepareButtons();
6440 prepareAxes(POSITION);
6441 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006442 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07006443
arthurhungdcef2dc2020-08-11 14:47:50 +08006444 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006445
6446 NotifyMotionArgs motionArgs;
6447
6448 // Down.
6449 int32_t x = 100;
6450 int32_t y = 125;
6451 processDown(mapper, x, y);
6452 processSync(mapper);
6453
6454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6455 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6456 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6457 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6458 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6459 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6460 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6461 ASSERT_EQ(0, motionArgs.flags);
6462 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6463 ASSERT_EQ(0, motionArgs.buttonState);
6464 ASSERT_EQ(0, motionArgs.edgeFlags);
6465 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6466 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6467 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6468 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6469 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6470 1, 0, 0, 0, 0, 0, 0, 0));
6471 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6472 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6473 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6474
6475 // Move.
6476 x += 50;
6477 y += 75;
6478 processMove(mapper, x, y);
6479 processSync(mapper);
6480
6481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6482 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6483 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6484 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6485 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6486 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6488 ASSERT_EQ(0, motionArgs.flags);
6489 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6490 ASSERT_EQ(0, motionArgs.buttonState);
6491 ASSERT_EQ(0, motionArgs.edgeFlags);
6492 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6493 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6494 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6496 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6497 1, 0, 0, 0, 0, 0, 0, 0));
6498 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6499 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6500 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6501
6502 // Up.
6503 processUp(mapper);
6504 processSync(mapper);
6505
6506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6507 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6508 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6509 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6510 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6511 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6512 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6513 ASSERT_EQ(0, motionArgs.flags);
6514 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6515 ASSERT_EQ(0, motionArgs.buttonState);
6516 ASSERT_EQ(0, motionArgs.edgeFlags);
6517 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6518 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6519 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6521 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6522 1, 0, 0, 0, 0, 0, 0, 0));
6523 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6524 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6525 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6526
6527 // Should not have sent any more keys or motions.
6528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6530}
6531
Michael Wrightd02c5b62014-02-10 15:10:22 -08006532TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006533 addConfigurationProperty("touch.deviceType", "touchScreen");
6534 prepareDisplay(DISPLAY_ORIENTATION_0);
6535 prepareButtons();
6536 prepareAxes(POSITION);
6537 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006538 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006539
arthurhungdcef2dc2020-08-11 14:47:50 +08006540 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006541
6542 NotifyMotionArgs motionArgs;
6543
6544 // Down.
6545 int32_t x = 100;
6546 int32_t y = 125;
6547 processDown(mapper, x, y);
6548 processSync(mapper);
6549
6550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6551 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6552 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6553 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6554 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6555 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6556 ASSERT_EQ(0, motionArgs.flags);
6557 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6558 ASSERT_EQ(0, motionArgs.buttonState);
6559 ASSERT_EQ(0, motionArgs.edgeFlags);
6560 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6561 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6562 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6564 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6565 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6566 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6567 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6568
6569 // Move.
6570 x += 50;
6571 y += 75;
6572 processMove(mapper, x, y);
6573 processSync(mapper);
6574
6575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6576 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6577 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6578 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6579 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6581 ASSERT_EQ(0, motionArgs.flags);
6582 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6583 ASSERT_EQ(0, motionArgs.buttonState);
6584 ASSERT_EQ(0, motionArgs.edgeFlags);
6585 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6586 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6587 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6589 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6590 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6591 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6592 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6593
6594 // Up.
6595 processUp(mapper);
6596 processSync(mapper);
6597
6598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6599 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6600 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6601 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6602 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6603 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6604 ASSERT_EQ(0, motionArgs.flags);
6605 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6606 ASSERT_EQ(0, motionArgs.buttonState);
6607 ASSERT_EQ(0, motionArgs.edgeFlags);
6608 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6609 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6610 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6612 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6613 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6614 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6615 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6616
6617 // Should not have sent any more keys or motions.
6618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6620}
6621
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006622TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006623 addConfigurationProperty("touch.deviceType", "touchScreen");
6624 prepareButtons();
6625 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006626 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
6627 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006628 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006629
6630 NotifyMotionArgs args;
6631
6632 // Rotation 90.
6633 prepareDisplay(DISPLAY_ORIENTATION_90);
6634 processDown(mapper, toRawX(50), toRawY(75));
6635 processSync(mapper);
6636
6637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6638 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6639 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6640
6641 processUp(mapper);
6642 processSync(mapper);
6643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6644}
6645
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006646TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006647 addConfigurationProperty("touch.deviceType", "touchScreen");
6648 prepareButtons();
6649 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006650 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6651 // orientation-aware are affected by display rotation.
6652 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006653 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006654
6655 NotifyMotionArgs args;
6656
6657 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006658 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006659 prepareDisplay(DISPLAY_ORIENTATION_0);
6660 processDown(mapper, toRawX(50), toRawY(75));
6661 processSync(mapper);
6662
6663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6664 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6665 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6666
6667 processUp(mapper);
6668 processSync(mapper);
6669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6670
6671 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006672 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006673 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006674 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006675 processSync(mapper);
6676
6677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6678 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6679 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6680
6681 processUp(mapper);
6682 processSync(mapper);
6683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6684
6685 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006686 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006687 prepareDisplay(DISPLAY_ORIENTATION_180);
6688 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6689 processSync(mapper);
6690
6691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6692 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6693 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6694
6695 processUp(mapper);
6696 processSync(mapper);
6697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6698
6699 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006700 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006701 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006702 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006703 processSync(mapper);
6704
6705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6706 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6707 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6708
6709 processUp(mapper);
6710 processSync(mapper);
6711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6712}
6713
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006714TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6715 addConfigurationProperty("touch.deviceType", "touchScreen");
6716 prepareButtons();
6717 prepareAxes(POSITION);
6718 addConfigurationProperty("touch.orientationAware", "1");
6719 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6720 clearViewports();
6721 prepareDisplay(DISPLAY_ORIENTATION_0);
6722 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6723 NotifyMotionArgs args;
6724
6725 // Orientation 0.
6726 processDown(mapper, toRawX(50), toRawY(75));
6727 processSync(mapper);
6728
6729 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6730 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6731 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6732
6733 processUp(mapper);
6734 processSync(mapper);
6735 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6736}
6737
6738TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6739 addConfigurationProperty("touch.deviceType", "touchScreen");
6740 prepareButtons();
6741 prepareAxes(POSITION);
6742 addConfigurationProperty("touch.orientationAware", "1");
6743 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6744 clearViewports();
6745 prepareDisplay(DISPLAY_ORIENTATION_0);
6746 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6747 NotifyMotionArgs args;
6748
6749 // Orientation 90.
6750 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6751 processSync(mapper);
6752
6753 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6754 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6755 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6756
6757 processUp(mapper);
6758 processSync(mapper);
6759 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6760}
6761
6762TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6763 addConfigurationProperty("touch.deviceType", "touchScreen");
6764 prepareButtons();
6765 prepareAxes(POSITION);
6766 addConfigurationProperty("touch.orientationAware", "1");
6767 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6768 clearViewports();
6769 prepareDisplay(DISPLAY_ORIENTATION_0);
6770 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6771 NotifyMotionArgs args;
6772
6773 // Orientation 180.
6774 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6775 processSync(mapper);
6776
6777 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6778 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6779 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6780
6781 processUp(mapper);
6782 processSync(mapper);
6783 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6784}
6785
6786TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6787 addConfigurationProperty("touch.deviceType", "touchScreen");
6788 prepareButtons();
6789 prepareAxes(POSITION);
6790 addConfigurationProperty("touch.orientationAware", "1");
6791 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6792 clearViewports();
6793 prepareDisplay(DISPLAY_ORIENTATION_0);
6794 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6795 NotifyMotionArgs args;
6796
6797 // Orientation 270.
6798 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6799 processSync(mapper);
6800
6801 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6802 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6803 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6804
6805 processUp(mapper);
6806 processSync(mapper);
6807 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6808}
6809
6810TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6811 addConfigurationProperty("touch.deviceType", "touchScreen");
6812 prepareButtons();
6813 prepareAxes(POSITION);
6814 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6815 // orientation-aware are affected by display rotation.
6816 addConfigurationProperty("touch.orientationAware", "0");
6817 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6818 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6819
6820 NotifyMotionArgs args;
6821
6822 // Orientation 90, Rotation 0.
6823 clearViewports();
6824 prepareDisplay(DISPLAY_ORIENTATION_0);
6825 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6826 processSync(mapper);
6827
6828 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6829 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6830 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6831
6832 processUp(mapper);
6833 processSync(mapper);
6834 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6835
6836 // Orientation 90, Rotation 90.
6837 clearViewports();
6838 prepareDisplay(DISPLAY_ORIENTATION_90);
6839 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6840 processSync(mapper);
6841
6842 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6843 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6844 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6845
6846 processUp(mapper);
6847 processSync(mapper);
6848 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6849
6850 // Orientation 90, Rotation 180.
6851 clearViewports();
6852 prepareDisplay(DISPLAY_ORIENTATION_180);
6853 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6854 processSync(mapper);
6855
6856 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6857 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6858 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6859
6860 processUp(mapper);
6861 processSync(mapper);
6862 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6863
6864 // Orientation 90, Rotation 270.
6865 clearViewports();
6866 prepareDisplay(DISPLAY_ORIENTATION_270);
6867 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6868 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6869 processSync(mapper);
6870
6871 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6872 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6873 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6874
6875 processUp(mapper);
6876 processSync(mapper);
6877 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6878}
6879
Michael Wrightd02c5b62014-02-10 15:10:22 -08006880TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006881 addConfigurationProperty("touch.deviceType", "touchScreen");
6882 prepareDisplay(DISPLAY_ORIENTATION_0);
6883 prepareButtons();
6884 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006885 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006886
6887 // These calculations are based on the input device calibration documentation.
6888 int32_t rawX = 100;
6889 int32_t rawY = 200;
6890 int32_t rawPressure = 10;
6891 int32_t rawToolMajor = 12;
6892 int32_t rawDistance = 2;
6893 int32_t rawTiltX = 30;
6894 int32_t rawTiltY = 110;
6895
6896 float x = toDisplayX(rawX);
6897 float y = toDisplayY(rawY);
6898 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6899 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6900 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6901 float distance = float(rawDistance);
6902
6903 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6904 float tiltScale = M_PI / 180;
6905 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6906 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6907 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6908 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6909
6910 processDown(mapper, rawX, rawY);
6911 processPressure(mapper, rawPressure);
6912 processToolMajor(mapper, rawToolMajor);
6913 processDistance(mapper, rawDistance);
6914 processTilt(mapper, rawTiltX, rawTiltY);
6915 processSync(mapper);
6916
6917 NotifyMotionArgs args;
6918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6920 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6921 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6922}
6923
Jason Gerecke489fda82012-09-07 17:19:40 -07006924TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006925 addConfigurationProperty("touch.deviceType", "touchScreen");
6926 prepareDisplay(DISPLAY_ORIENTATION_0);
6927 prepareLocationCalibration();
6928 prepareButtons();
6929 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006930 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006931
6932 int32_t rawX = 100;
6933 int32_t rawY = 200;
6934
6935 float x = toDisplayX(toCookedX(rawX, rawY));
6936 float y = toDisplayY(toCookedY(rawX, rawY));
6937
6938 processDown(mapper, rawX, rawY);
6939 processSync(mapper);
6940
6941 NotifyMotionArgs args;
6942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6944 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6945}
6946
Michael Wrightd02c5b62014-02-10 15:10:22 -08006947TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006948 addConfigurationProperty("touch.deviceType", "touchScreen");
6949 prepareDisplay(DISPLAY_ORIENTATION_0);
6950 prepareButtons();
6951 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006952 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006953
6954 NotifyMotionArgs motionArgs;
6955 NotifyKeyArgs keyArgs;
6956
6957 processDown(mapper, 100, 200);
6958 processSync(mapper);
6959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6960 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6961 ASSERT_EQ(0, motionArgs.buttonState);
6962
6963 // press BTN_LEFT, release BTN_LEFT
6964 processKey(mapper, BTN_LEFT, 1);
6965 processSync(mapper);
6966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6967 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6968 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6969
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6971 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6972 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6973
Michael Wrightd02c5b62014-02-10 15:10:22 -08006974 processKey(mapper, BTN_LEFT, 0);
6975 processSync(mapper);
6976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006977 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006978 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006979
6980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006981 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006982 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006983
6984 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6985 processKey(mapper, BTN_RIGHT, 1);
6986 processKey(mapper, BTN_MIDDLE, 1);
6987 processSync(mapper);
6988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6989 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6990 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6991 motionArgs.buttonState);
6992
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6994 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6995 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6996
6997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6998 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6999 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7000 motionArgs.buttonState);
7001
Michael Wrightd02c5b62014-02-10 15:10:22 -08007002 processKey(mapper, BTN_RIGHT, 0);
7003 processSync(mapper);
7004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007005 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007006 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007007
7008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007009 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007010 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007011
7012 processKey(mapper, BTN_MIDDLE, 0);
7013 processSync(mapper);
7014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007015 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007016 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007017
7018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007020 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007021
7022 // press BTN_BACK, release BTN_BACK
7023 processKey(mapper, BTN_BACK, 1);
7024 processSync(mapper);
7025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7026 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7027 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007028
Michael Wrightd02c5b62014-02-10 15:10:22 -08007029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007030 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007031 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7032
7033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7034 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7035 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007036
7037 processKey(mapper, BTN_BACK, 0);
7038 processSync(mapper);
7039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007040 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007041 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007042
7043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007044 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007045 ASSERT_EQ(0, motionArgs.buttonState);
7046
Michael Wrightd02c5b62014-02-10 15:10:22 -08007047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7048 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7049 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7050
7051 // press BTN_SIDE, release BTN_SIDE
7052 processKey(mapper, BTN_SIDE, 1);
7053 processSync(mapper);
7054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7055 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7056 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007057
Michael Wrightd02c5b62014-02-10 15:10:22 -08007058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007060 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7061
7062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7063 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7064 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007065
7066 processKey(mapper, BTN_SIDE, 0);
7067 processSync(mapper);
7068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007069 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007070 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007071
7072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007074 ASSERT_EQ(0, motionArgs.buttonState);
7075
Michael Wrightd02c5b62014-02-10 15:10:22 -08007076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7077 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7078 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7079
7080 // press BTN_FORWARD, release BTN_FORWARD
7081 processKey(mapper, BTN_FORWARD, 1);
7082 processSync(mapper);
7083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7084 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7085 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007086
Michael Wrightd02c5b62014-02-10 15:10:22 -08007087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007088 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007089 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7090
7091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7092 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7093 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007094
7095 processKey(mapper, BTN_FORWARD, 0);
7096 processSync(mapper);
7097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007098 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007099 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007100
7101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007103 ASSERT_EQ(0, motionArgs.buttonState);
7104
Michael Wrightd02c5b62014-02-10 15:10:22 -08007105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7106 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7107 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7108
7109 // press BTN_EXTRA, release BTN_EXTRA
7110 processKey(mapper, BTN_EXTRA, 1);
7111 processSync(mapper);
7112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7113 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7114 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007115
Michael Wrightd02c5b62014-02-10 15:10:22 -08007116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007118 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7119
7120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7121 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7122 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007123
7124 processKey(mapper, BTN_EXTRA, 0);
7125 processSync(mapper);
7126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007127 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007128 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007129
7130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007132 ASSERT_EQ(0, motionArgs.buttonState);
7133
Michael Wrightd02c5b62014-02-10 15:10:22 -08007134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7135 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7136 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7137
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7139
Michael Wrightd02c5b62014-02-10 15:10:22 -08007140 // press BTN_STYLUS, release BTN_STYLUS
7141 processKey(mapper, BTN_STYLUS, 1);
7142 processSync(mapper);
7143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007145 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7146
7147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7148 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7149 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007150
7151 processKey(mapper, BTN_STYLUS, 0);
7152 processSync(mapper);
7153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007154 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007155 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007156
7157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007158 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007159 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007160
7161 // press BTN_STYLUS2, release BTN_STYLUS2
7162 processKey(mapper, BTN_STYLUS2, 1);
7163 processSync(mapper);
7164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007166 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7167
7168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7169 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7170 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007171
7172 processKey(mapper, BTN_STYLUS2, 0);
7173 processSync(mapper);
7174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007175 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007176 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007177
7178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007180 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007181
7182 // release touch
7183 processUp(mapper);
7184 processSync(mapper);
7185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7186 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7187 ASSERT_EQ(0, motionArgs.buttonState);
7188}
7189
7190TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007191 addConfigurationProperty("touch.deviceType", "touchScreen");
7192 prepareDisplay(DISPLAY_ORIENTATION_0);
7193 prepareButtons();
7194 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007195 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007196
7197 NotifyMotionArgs motionArgs;
7198
7199 // default tool type is finger
7200 processDown(mapper, 100, 200);
7201 processSync(mapper);
7202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7203 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7204 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7205
7206 // eraser
7207 processKey(mapper, BTN_TOOL_RUBBER, 1);
7208 processSync(mapper);
7209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7210 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7211 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7212
7213 // stylus
7214 processKey(mapper, BTN_TOOL_RUBBER, 0);
7215 processKey(mapper, BTN_TOOL_PEN, 1);
7216 processSync(mapper);
7217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7219 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7220
7221 // brush
7222 processKey(mapper, BTN_TOOL_PEN, 0);
7223 processKey(mapper, BTN_TOOL_BRUSH, 1);
7224 processSync(mapper);
7225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7226 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7227 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7228
7229 // pencil
7230 processKey(mapper, BTN_TOOL_BRUSH, 0);
7231 processKey(mapper, BTN_TOOL_PENCIL, 1);
7232 processSync(mapper);
7233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7235 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7236
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007237 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007238 processKey(mapper, BTN_TOOL_PENCIL, 0);
7239 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7240 processSync(mapper);
7241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7243 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7244
7245 // mouse
7246 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7247 processKey(mapper, BTN_TOOL_MOUSE, 1);
7248 processSync(mapper);
7249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7251 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7252
7253 // lens
7254 processKey(mapper, BTN_TOOL_MOUSE, 0);
7255 processKey(mapper, BTN_TOOL_LENS, 1);
7256 processSync(mapper);
7257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7258 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7259 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7260
7261 // double-tap
7262 processKey(mapper, BTN_TOOL_LENS, 0);
7263 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7264 processSync(mapper);
7265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7266 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7267 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7268
7269 // triple-tap
7270 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7271 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7272 processSync(mapper);
7273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7274 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7275 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7276
7277 // quad-tap
7278 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7279 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7280 processSync(mapper);
7281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7283 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7284
7285 // finger
7286 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7287 processKey(mapper, BTN_TOOL_FINGER, 1);
7288 processSync(mapper);
7289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7291 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7292
7293 // stylus trumps finger
7294 processKey(mapper, BTN_TOOL_PEN, 1);
7295 processSync(mapper);
7296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7298 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7299
7300 // eraser trumps stylus
7301 processKey(mapper, BTN_TOOL_RUBBER, 1);
7302 processSync(mapper);
7303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7305 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7306
7307 // mouse trumps eraser
7308 processKey(mapper, BTN_TOOL_MOUSE, 1);
7309 processSync(mapper);
7310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7312 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7313
7314 // back to default tool type
7315 processKey(mapper, BTN_TOOL_MOUSE, 0);
7316 processKey(mapper, BTN_TOOL_RUBBER, 0);
7317 processKey(mapper, BTN_TOOL_PEN, 0);
7318 processKey(mapper, BTN_TOOL_FINGER, 0);
7319 processSync(mapper);
7320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7321 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7322 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7323}
7324
7325TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007326 addConfigurationProperty("touch.deviceType", "touchScreen");
7327 prepareDisplay(DISPLAY_ORIENTATION_0);
7328 prepareButtons();
7329 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007330 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007331 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007332
7333 NotifyMotionArgs motionArgs;
7334
7335 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7336 processKey(mapper, BTN_TOOL_FINGER, 1);
7337 processMove(mapper, 100, 200);
7338 processSync(mapper);
7339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7340 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7342 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7343
7344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7345 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7347 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7348
7349 // move a little
7350 processMove(mapper, 150, 250);
7351 processSync(mapper);
7352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7353 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7355 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7356
7357 // down when BTN_TOUCH is pressed, pressure defaults to 1
7358 processKey(mapper, BTN_TOUCH, 1);
7359 processSync(mapper);
7360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7361 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7363 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7364
7365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7366 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7367 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7368 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7369
7370 // up when BTN_TOUCH is released, hover restored
7371 processKey(mapper, BTN_TOUCH, 0);
7372 processSync(mapper);
7373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7374 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7376 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7377
7378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7379 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7381 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7382
7383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7384 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7386 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7387
7388 // exit hover when pointer goes away
7389 processKey(mapper, BTN_TOOL_FINGER, 0);
7390 processSync(mapper);
7391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7392 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7394 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7395}
7396
7397TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007398 addConfigurationProperty("touch.deviceType", "touchScreen");
7399 prepareDisplay(DISPLAY_ORIENTATION_0);
7400 prepareButtons();
7401 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007402 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007403
7404 NotifyMotionArgs motionArgs;
7405
7406 // initially hovering because pressure is 0
7407 processDown(mapper, 100, 200);
7408 processPressure(mapper, 0);
7409 processSync(mapper);
7410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7411 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7413 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7414
7415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7416 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7417 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7418 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7419
7420 // move a little
7421 processMove(mapper, 150, 250);
7422 processSync(mapper);
7423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7424 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7425 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7426 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7427
7428 // down when pressure is non-zero
7429 processPressure(mapper, RAW_PRESSURE_MAX);
7430 processSync(mapper);
7431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7432 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7434 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7435
7436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7437 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7439 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7440
7441 // up when pressure becomes 0, hover restored
7442 processPressure(mapper, 0);
7443 processSync(mapper);
7444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7445 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7446 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7447 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7448
7449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7450 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7452 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7453
7454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7455 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7457 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7458
7459 // exit hover when pointer goes away
7460 processUp(mapper);
7461 processSync(mapper);
7462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7463 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7465 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7466}
7467
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007468TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
7469 addConfigurationProperty("touch.deviceType", "touchScreen");
7470 prepareDisplay(DISPLAY_ORIENTATION_0);
7471 prepareButtons();
7472 prepareAxes(POSITION | PRESSURE);
7473 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7474
7475 // Touch down.
7476 processDown(mapper, 100, 200);
7477 processPressure(mapper, 1);
7478 processSync(mapper);
7479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7480 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
7481
7482 // Reset the mapper. This should cancel the ongoing gesture.
7483 resetMapper(mapper, ARBITRARY_TIME);
7484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7485 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
7486
7487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7488}
7489
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007490TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
7491 addConfigurationProperty("touch.deviceType", "touchScreen");
7492 prepareDisplay(DISPLAY_ORIENTATION_0);
7493 prepareButtons();
7494 prepareAxes(POSITION | PRESSURE);
7495 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7496
7497 // Set the initial state for the touch pointer.
7498 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
7499 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
7500 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
7501 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7502
7503 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007504 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
7505 // does not generate any events.
7506 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007507
7508 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
7509 // the recreated touch state to generate a down event.
7510 processSync(mapper);
7511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7512 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
7513
7514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7515}
7516
lilinnan687e58f2022-07-19 16:00:50 +08007517TEST_F(SingleTouchInputMapperTest,
7518 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
7519 addConfigurationProperty("touch.deviceType", "touchScreen");
7520 prepareDisplay(DISPLAY_ORIENTATION_0);
7521 prepareButtons();
7522 prepareAxes(POSITION);
7523 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7524 NotifyMotionArgs motionArgs;
7525
7526 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00007527 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08007528 processSync(mapper);
7529
7530 // We should receive a down event
7531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7532 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7533
7534 // Change display id
7535 clearViewports();
7536 prepareSecondaryDisplay(ViewportType::INTERNAL);
7537
7538 // We should receive a cancel event
7539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7540 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7541 // Then receive reset called
7542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7543}
7544
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007545TEST_F(SingleTouchInputMapperTest,
7546 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
7547 addConfigurationProperty("touch.deviceType", "touchScreen");
7548 prepareDisplay(DISPLAY_ORIENTATION_0);
7549 prepareButtons();
7550 prepareAxes(POSITION);
7551 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7553 NotifyMotionArgs motionArgs;
7554
7555 // Start a new gesture.
7556 processDown(mapper, 100, 200);
7557 processSync(mapper);
7558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7559 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7560
7561 // Make the viewport inactive. This will put the device in disabled mode.
7562 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7563 viewport->isActive = false;
7564 mFakePolicy->updateViewport(*viewport);
7565 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7566
7567 // We should receive a cancel event for the ongoing gesture.
7568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7569 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7570 // Then we should be notified that the device was reset.
7571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7572
7573 // No events are generated while the viewport is inactive.
7574 processMove(mapper, 101, 201);
7575 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007576 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007577 processSync(mapper);
7578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7579
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007580 // Start a new gesture while the viewport is still inactive.
7581 processDown(mapper, 300, 400);
7582 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
7583 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
7584 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7585 processSync(mapper);
7586
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007587 // Make the viewport active again. The device should resume processing events.
7588 viewport->isActive = true;
7589 mFakePolicy->updateViewport(*viewport);
7590 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7591
7592 // The device is reset because it changes back to direct mode, without generating any events.
7593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7595
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007596 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007597 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7599 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007600
7601 // No more events.
7602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
7604}
7605
Prabir Pradhan211ba622022-10-31 21:09:21 +00007606TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
7607 addConfigurationProperty("touch.deviceType", "touchScreen");
7608 prepareDisplay(DISPLAY_ORIENTATION_0);
7609 prepareButtons();
7610 prepareAxes(POSITION);
7611 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7613
7614 // Press a stylus button.
7615 processKey(mapper, BTN_STYLUS, 1);
7616 processSync(mapper);
7617
7618 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
7619 processDown(mapper, 100, 200);
7620 processSync(mapper);
7621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7622 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7623 WithCoords(toDisplayX(100), toDisplayY(200)),
7624 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7626 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7627 WithCoords(toDisplayX(100), toDisplayY(200)),
7628 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7629
7630 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
7631 // the button has not actually been released, since there will be no pointers through which the
7632 // button state can be reported. The event is generated at the location of the pointer before
7633 // it went up.
7634 processUp(mapper);
7635 processSync(mapper);
7636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7637 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7638 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7640 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7641 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7642}
7643
Prabir Pradhan5632d622021-09-06 07:57:20 -07007644// --- TouchDisplayProjectionTest ---
7645
7646class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7647public:
7648 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7649 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7650 // rotated equivalent of the given un-rotated physical display bounds.
7651 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
7652 uint32_t inverseRotationFlags;
7653 auto width = DISPLAY_WIDTH;
7654 auto height = DISPLAY_HEIGHT;
7655 switch (orientation) {
7656 case DISPLAY_ORIENTATION_90:
7657 inverseRotationFlags = ui::Transform::ROT_270;
7658 std::swap(width, height);
7659 break;
7660 case DISPLAY_ORIENTATION_180:
7661 inverseRotationFlags = ui::Transform::ROT_180;
7662 break;
7663 case DISPLAY_ORIENTATION_270:
7664 inverseRotationFlags = ui::Transform::ROT_90;
7665 std::swap(width, height);
7666 break;
7667 case DISPLAY_ORIENTATION_0:
7668 inverseRotationFlags = ui::Transform::ROT_0;
7669 break;
7670 default:
7671 FAIL() << "Invalid orientation: " << orientation;
7672 }
7673
7674 const ui::Transform rotation(inverseRotationFlags, width, height);
7675 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7676
7677 std::optional<DisplayViewport> internalViewport =
7678 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7679 DisplayViewport& v = *internalViewport;
7680 v.displayId = DISPLAY_ID;
7681 v.orientation = orientation;
7682
7683 v.logicalLeft = 0;
7684 v.logicalTop = 0;
7685 v.logicalRight = 100;
7686 v.logicalBottom = 100;
7687
7688 v.physicalLeft = rotatedPhysicalDisplay.left;
7689 v.physicalTop = rotatedPhysicalDisplay.top;
7690 v.physicalRight = rotatedPhysicalDisplay.right;
7691 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7692
7693 v.deviceWidth = width;
7694 v.deviceHeight = height;
7695
7696 v.isActive = true;
7697 v.uniqueId = UNIQUE_ID;
7698 v.type = ViewportType::INTERNAL;
7699 mFakePolicy->updateViewport(v);
7700 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7701 }
7702
7703 void assertReceivedMove(const Point& point) {
7704 NotifyMotionArgs motionArgs;
7705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7706 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7707 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7708 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7709 1, 0, 0, 0, 0, 0, 0, 0));
7710 }
7711};
7712
7713TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7714 addConfigurationProperty("touch.deviceType", "touchScreen");
7715 prepareDisplay(DISPLAY_ORIENTATION_0);
7716
7717 prepareButtons();
7718 prepareAxes(POSITION);
7719 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7720
7721 NotifyMotionArgs motionArgs;
7722
7723 // Configure the DisplayViewport such that the logical display maps to a subsection of
7724 // the display panel called the physical display. Here, the physical display is bounded by the
7725 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7726 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7727 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7728 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7729
7730 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7731 DISPLAY_ORIENTATION_270}) {
7732 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7733
7734 // Touches outside the physical display should be ignored, and should not generate any
7735 // events. Ensure touches at the following points that lie outside of the physical display
7736 // area do not generate any events.
7737 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7738 processDown(mapper, toRawX(point.x), toRawY(point.y));
7739 processSync(mapper);
7740 processUp(mapper);
7741 processSync(mapper);
7742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7743 << "Unexpected event generated for touch outside physical display at point: "
7744 << point.x << ", " << point.y;
7745 }
7746 }
7747}
7748
7749TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7750 addConfigurationProperty("touch.deviceType", "touchScreen");
7751 prepareDisplay(DISPLAY_ORIENTATION_0);
7752
7753 prepareButtons();
7754 prepareAxes(POSITION);
7755 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7756
7757 NotifyMotionArgs motionArgs;
7758
7759 // Configure the DisplayViewport such that the logical display maps to a subsection of
7760 // the display panel called the physical display. Here, the physical display is bounded by the
7761 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7762 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7763
7764 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7765 DISPLAY_ORIENTATION_270}) {
7766 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7767
7768 // Touches that start outside the physical display should be ignored until it enters the
7769 // physical display bounds, at which point it should generate a down event. Start a touch at
7770 // the point (5, 100), which is outside the physical display bounds.
7771 static const Point kOutsidePoint{5, 100};
7772 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7773 processSync(mapper);
7774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7775
7776 // Move the touch into the physical display area. This should generate a pointer down.
7777 processMove(mapper, toRawX(11), toRawY(21));
7778 processSync(mapper);
7779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7780 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7781 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7782 ASSERT_NO_FATAL_FAILURE(
7783 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7784
7785 // Move the touch inside the physical display area. This should generate a pointer move.
7786 processMove(mapper, toRawX(69), toRawY(159));
7787 processSync(mapper);
7788 assertReceivedMove({69, 159});
7789
7790 // Move outside the physical display area. Since the pointer is already down, this should
7791 // now continue generating events.
7792 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7793 processSync(mapper);
7794 assertReceivedMove(kOutsidePoint);
7795
7796 // Release. This should generate a pointer up.
7797 processUp(mapper);
7798 processSync(mapper);
7799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7800 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7801 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7802 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7803
7804 // Ensure no more events were generated.
7805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7807 }
7808}
7809
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007810// --- ExternalStylusFusionTest ---
7811
7812class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7813public:
7814 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7815 addConfigurationProperty("touch.deviceType", "touchScreen");
7816 prepareDisplay(DISPLAY_ORIENTATION_0);
7817 prepareButtons();
7818 prepareAxes(POSITION);
7819 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7820
7821 mStylusState.when = ARBITRARY_TIME;
7822 mStylusState.pressure = 0.f;
7823 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7824 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
7825 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
7826 processExternalStylusState(mapper);
7827 return mapper;
7828 }
7829
7830 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7831 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7832 for (const NotifyArgs& args : generatedArgs) {
7833 mFakeListener->notify(args);
7834 }
7835 // Loop the reader to flush the input listener queue.
7836 mReader->loopOnce();
7837 return generatedArgs;
7838 }
7839
7840protected:
7841 StylusState mStylusState{};
7842 static constexpr uint32_t EXPECTED_SOURCE =
7843 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7844
7845 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7846 auto toolTypeSource =
7847 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7848
7849 // The first pointer is withheld.
7850 processDown(mapper, 100, 200);
7851 processSync(mapper);
7852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7853 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7854 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7855
7856 // The external stylus reports pressure. The withheld finger pointer is released as a
7857 // stylus.
7858 mStylusState.pressure = 1.f;
7859 processExternalStylusState(mapper);
7860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7861 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7862 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7863
7864 // Subsequent pointer events are not withheld.
7865 processMove(mapper, 101, 201);
7866 processSync(mapper);
7867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7868 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7869
7870 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7872 }
7873
7874 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7875 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7876
7877 // Releasing the touch pointer ends the gesture.
7878 processUp(mapper);
7879 processSync(mapper);
7880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7881 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7882 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7883
7884 mStylusState.pressure = 0.f;
7885 processExternalStylusState(mapper);
7886 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7888 }
7889
7890 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7891 auto toolTypeSource =
7892 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER));
7893
7894 // The first pointer is withheld when an external stylus is connected,
7895 // and a timeout is requested.
7896 processDown(mapper, 100, 200);
7897 processSync(mapper);
7898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7899 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7900 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7901
7902 // If the timeout expires early, it is requested again.
7903 handleTimeout(mapper, ARBITRARY_TIME + 1);
7904 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7905 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7906
7907 // When the timeout expires, the withheld touch is released as a finger pointer.
7908 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7910 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7911
7912 // Subsequent pointer events are not withheld.
7913 processMove(mapper, 101, 201);
7914 processSync(mapper);
7915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7916 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7917 processUp(mapper);
7918 processSync(mapper);
7919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7920 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7921
7922 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7924 }
7925
7926private:
7927 InputDeviceInfo mExternalStylusDeviceInfo{};
7928};
7929
7930TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7931 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7932 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7933}
7934
7935TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7936 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7937 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7938}
7939
7940TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7941 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7942 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7943}
7944
7945// Test a successful stylus fusion gesture where the pressure is reported by the external
7946// before the touch is reported by the touchscreen.
7947TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7948 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7949 auto toolTypeSource =
7950 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7951
7952 // The external stylus reports pressure first. It is ignored for now.
7953 mStylusState.pressure = 1.f;
7954 processExternalStylusState(mapper);
7955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7956 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7957
7958 // When the touch goes down afterwards, it is reported as a stylus pointer.
7959 processDown(mapper, 100, 200);
7960 processSync(mapper);
7961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7962 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7963 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7964
7965 processMove(mapper, 101, 201);
7966 processSync(mapper);
7967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7968 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7969 processUp(mapper);
7970 processSync(mapper);
7971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7972 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7973
7974 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7976}
7977
7978TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7979 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7980
7981 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7982 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7983
7984 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7985 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7986 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7987 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7988}
7989
7990TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7991 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7992 auto toolTypeSource =
7993 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7994
7995 mStylusState.pressure = 0.8f;
7996 processExternalStylusState(mapper);
7997 processDown(mapper, 100, 200);
7998 processSync(mapper);
7999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8000 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
8001 WithPressure(0.8f))));
8002 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8003
8004 // The external stylus reports a pressure change. We wait for some time for a touch event.
8005 mStylusState.pressure = 0.6f;
8006 processExternalStylusState(mapper);
8007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8008 ASSERT_NO_FATAL_FAILURE(
8009 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8010
8011 // If a touch is reported within the timeout, it reports the updated pressure.
8012 processMove(mapper, 101, 201);
8013 processSync(mapper);
8014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8015 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8016 WithPressure(0.6f))));
8017 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8018
8019 // There is another pressure change.
8020 mStylusState.pressure = 0.5f;
8021 processExternalStylusState(mapper);
8022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8023 ASSERT_NO_FATAL_FAILURE(
8024 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8025
8026 // If a touch is not reported within the timeout, a move event is generated to report
8027 // the new pressure.
8028 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
8029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8030 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8031 WithPressure(0.5f))));
8032
8033 // If a zero pressure is reported before the touch goes up, the previous pressure value is
8034 // repeated indefinitely.
8035 mStylusState.pressure = 0.0f;
8036 processExternalStylusState(mapper);
8037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8038 ASSERT_NO_FATAL_FAILURE(
8039 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8040 processMove(mapper, 102, 202);
8041 processSync(mapper);
8042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8043 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8044 WithPressure(0.5f))));
8045 processMove(mapper, 103, 203);
8046 processSync(mapper);
8047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8048 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8049 WithPressure(0.5f))));
8050
8051 processUp(mapper);
8052 processSync(mapper);
8053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8054 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
8055 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
8056
8057 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8059}
8060
8061TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
8062 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8063 auto source = WithSource(EXPECTED_SOURCE);
8064
8065 mStylusState.pressure = 1.f;
8066 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_ERASER;
8067 processExternalStylusState(mapper);
8068 processDown(mapper, 100, 200);
8069 processSync(mapper);
8070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8071 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
8072 WithToolType(AMOTION_EVENT_TOOL_TYPE_ERASER))));
8073 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8074
8075 // The external stylus reports a tool change. We wait for some time for a touch event.
8076 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
8077 processExternalStylusState(mapper);
8078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8079 ASSERT_NO_FATAL_FAILURE(
8080 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8081
8082 // If a touch is reported within the timeout, it reports the updated pressure.
8083 processMove(mapper, 101, 201);
8084 processSync(mapper);
8085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8086 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8087 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
8088 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8089
8090 // There is another tool type change.
8091 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
8092 processExternalStylusState(mapper);
8093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8094 ASSERT_NO_FATAL_FAILURE(
8095 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8096
8097 // If a touch is not reported within the timeout, a move event is generated to report
8098 // the new tool type.
8099 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
8100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8101 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8102 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
8103
8104 processUp(mapper);
8105 processSync(mapper);
8106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8107 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
8108 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
8109
8110 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8112}
8113
8114TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
8115 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8116 auto toolTypeSource =
8117 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
8118
8119 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
8120
8121 // The external stylus reports a button change. We wait for some time for a touch event.
8122 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
8123 processExternalStylusState(mapper);
8124 ASSERT_NO_FATAL_FAILURE(
8125 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8126
8127 // If a touch is reported within the timeout, it reports the updated button state.
8128 processMove(mapper, 101, 201);
8129 processSync(mapper);
8130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8131 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8132 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8134 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8135 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8136 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8137
8138 // The button is now released.
8139 mStylusState.buttons = 0;
8140 processExternalStylusState(mapper);
8141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8142 ASSERT_NO_FATAL_FAILURE(
8143 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8144
8145 // If a touch is not reported within the timeout, a move event is generated to report
8146 // the new button state.
8147 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8149 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
8150 WithButtonState(0))));
8151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00008152 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8153 WithButtonState(0))));
8154
8155 processUp(mapper);
8156 processSync(mapper);
8157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008158 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8159
8160 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8162}
8163
Michael Wrightd02c5b62014-02-10 15:10:22 -08008164// --- MultiTouchInputMapperTest ---
8165
8166class MultiTouchInputMapperTest : public TouchInputMapperTest {
8167protected:
8168 void prepareAxes(int axes);
8169
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008170 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
8171 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
8172 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
8173 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
8174 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
8175 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
8176 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
8177 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
8178 void processId(MultiTouchInputMapper& mapper, int32_t id);
8179 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
8180 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
8181 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008182 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008183 void processMTSync(MultiTouchInputMapper& mapper);
8184 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008185};
8186
8187void MultiTouchInputMapperTest::prepareAxes(int axes) {
8188 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008189 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
8190 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008191 }
8192 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008193 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
8194 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008195 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008196 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
8197 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008198 }
8199 }
8200 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008201 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8202 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008203 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008204 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008205 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008206 }
8207 }
8208 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008209 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
8210 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008211 }
8212 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008213 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
8214 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008215 }
8216 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008217 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
8218 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008219 }
8220 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008221 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
8222 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008223 }
8224 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008225 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
8226 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008227 }
8228 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008229 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008230 }
8231}
8232
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008233void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
8234 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
8236 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008237}
8238
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008239void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
8240 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008241 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008242}
8243
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008244void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
8245 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008246 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008247}
8248
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008249void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008250 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008251}
8252
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008253void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008254 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008255}
8256
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008257void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
8258 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008259 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008260}
8261
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008262void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008264}
8265
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008266void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008268}
8269
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008270void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008271 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008272}
8273
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008274void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008276}
8277
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008278void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008279 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008280}
8281
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008282void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
8283 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008284 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008285}
8286
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008287void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
8288 int32_t value) {
8289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
8290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
8291}
8292
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008293void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008295}
8296
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008297void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008299}
8300
Michael Wrightd02c5b62014-02-10 15:10:22 -08008301TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008302 addConfigurationProperty("touch.deviceType", "touchScreen");
8303 prepareDisplay(DISPLAY_ORIENTATION_0);
8304 prepareAxes(POSITION);
8305 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008306 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008307
arthurhungdcef2dc2020-08-11 14:47:50 +08008308 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008309
8310 NotifyMotionArgs motionArgs;
8311
8312 // Two fingers down at once.
8313 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8314 processPosition(mapper, x1, y1);
8315 processMTSync(mapper);
8316 processPosition(mapper, x2, y2);
8317 processMTSync(mapper);
8318 processSync(mapper);
8319
8320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8321 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8322 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8323 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8324 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8325 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8326 ASSERT_EQ(0, motionArgs.flags);
8327 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8328 ASSERT_EQ(0, motionArgs.buttonState);
8329 ASSERT_EQ(0, motionArgs.edgeFlags);
8330 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8331 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8334 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8335 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8336 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8337 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8338
8339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8340 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8341 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8342 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8343 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008344 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008345 ASSERT_EQ(0, motionArgs.flags);
8346 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8347 ASSERT_EQ(0, motionArgs.buttonState);
8348 ASSERT_EQ(0, motionArgs.edgeFlags);
8349 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8350 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8351 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8352 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8353 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8354 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8355 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8356 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8357 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8358 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8359 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8360 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8361
8362 // Move.
8363 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8364 processPosition(mapper, x1, y1);
8365 processMTSync(mapper);
8366 processPosition(mapper, x2, y2);
8367 processMTSync(mapper);
8368 processSync(mapper);
8369
8370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8371 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8372 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8373 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8374 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8376 ASSERT_EQ(0, motionArgs.flags);
8377 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8378 ASSERT_EQ(0, motionArgs.buttonState);
8379 ASSERT_EQ(0, motionArgs.edgeFlags);
8380 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8381 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8382 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8383 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8384 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8386 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8387 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8388 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8389 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8390 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8391 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8392
8393 // First finger up.
8394 x2 += 15; y2 -= 20;
8395 processPosition(mapper, x2, y2);
8396 processMTSync(mapper);
8397 processSync(mapper);
8398
8399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8400 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8401 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8402 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8403 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008404 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008405 ASSERT_EQ(0, motionArgs.flags);
8406 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8407 ASSERT_EQ(0, motionArgs.buttonState);
8408 ASSERT_EQ(0, motionArgs.edgeFlags);
8409 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8410 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8411 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8412 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8413 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8415 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8417 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8418 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8419 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8420 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8421
8422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8423 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8424 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8425 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8426 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8427 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8428 ASSERT_EQ(0, motionArgs.flags);
8429 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8430 ASSERT_EQ(0, motionArgs.buttonState);
8431 ASSERT_EQ(0, motionArgs.edgeFlags);
8432 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8433 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8434 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8436 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8437 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8438 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8439 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8440
8441 // Move.
8442 x2 += 20; y2 -= 25;
8443 processPosition(mapper, x2, y2);
8444 processMTSync(mapper);
8445 processSync(mapper);
8446
8447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8448 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8449 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8450 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8451 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8453 ASSERT_EQ(0, motionArgs.flags);
8454 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8455 ASSERT_EQ(0, motionArgs.buttonState);
8456 ASSERT_EQ(0, motionArgs.edgeFlags);
8457 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8458 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8459 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8460 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8461 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8462 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8463 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8464 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8465
8466 // New finger down.
8467 int32_t x3 = 700, y3 = 300;
8468 processPosition(mapper, x2, y2);
8469 processMTSync(mapper);
8470 processPosition(mapper, x3, y3);
8471 processMTSync(mapper);
8472 processSync(mapper);
8473
8474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8475 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8476 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8477 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8478 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008479 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008480 ASSERT_EQ(0, motionArgs.flags);
8481 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8482 ASSERT_EQ(0, motionArgs.buttonState);
8483 ASSERT_EQ(0, motionArgs.edgeFlags);
8484 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8485 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8486 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8487 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8488 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8490 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8491 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8492 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8493 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8494 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8495 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8496
8497 // Second finger up.
8498 x3 += 30; y3 -= 20;
8499 processPosition(mapper, x3, y3);
8500 processMTSync(mapper);
8501 processSync(mapper);
8502
8503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8504 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8505 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8506 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8507 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008508 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008509 ASSERT_EQ(0, motionArgs.flags);
8510 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8511 ASSERT_EQ(0, motionArgs.buttonState);
8512 ASSERT_EQ(0, motionArgs.edgeFlags);
8513 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8514 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8515 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8516 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8517 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8518 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8519 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8521 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8522 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8523 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8524 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8525
8526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8527 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8528 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8529 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8530 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8531 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8532 ASSERT_EQ(0, motionArgs.flags);
8533 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8534 ASSERT_EQ(0, motionArgs.buttonState);
8535 ASSERT_EQ(0, motionArgs.edgeFlags);
8536 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8537 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8538 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8540 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8541 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8542 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8543 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8544
8545 // Last finger up.
8546 processMTSync(mapper);
8547 processSync(mapper);
8548
8549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8550 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8551 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8552 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8553 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8554 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8555 ASSERT_EQ(0, motionArgs.flags);
8556 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8557 ASSERT_EQ(0, motionArgs.buttonState);
8558 ASSERT_EQ(0, motionArgs.edgeFlags);
8559 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8560 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8561 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8563 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8564 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8565 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8566 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8567
8568 // Should not have sent any more keys or motions.
8569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8571}
8572
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008573TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8574 addConfigurationProperty("touch.deviceType", "touchScreen");
8575 prepareDisplay(DISPLAY_ORIENTATION_0);
8576
8577 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8578 /*fuzz*/ 0, /*resolution*/ 10);
8579 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8580 /*fuzz*/ 0, /*resolution*/ 11);
8581 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8582 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8583 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8584 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8585 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8586 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8587 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8588 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8589
8590 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8591
8592 // X and Y axes
8593 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8594 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8595 // Touch major and minor
8596 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8597 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8598 // Tool major and minor
8599 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8600 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8601}
8602
8603TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8604 addConfigurationProperty("touch.deviceType", "touchScreen");
8605 prepareDisplay(DISPLAY_ORIENTATION_0);
8606
8607 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8608 /*fuzz*/ 0, /*resolution*/ 10);
8609 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8610 /*fuzz*/ 0, /*resolution*/ 11);
8611
8612 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8613
8614 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8615
8616 // Touch major and minor
8617 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8618 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8619 // Tool major and minor
8620 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8621 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8622}
8623
Michael Wrightd02c5b62014-02-10 15:10:22 -08008624TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008625 addConfigurationProperty("touch.deviceType", "touchScreen");
8626 prepareDisplay(DISPLAY_ORIENTATION_0);
8627 prepareAxes(POSITION | ID);
8628 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008629 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008630
arthurhungdcef2dc2020-08-11 14:47:50 +08008631 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008632
8633 NotifyMotionArgs motionArgs;
8634
8635 // Two fingers down at once.
8636 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8637 processPosition(mapper, x1, y1);
8638 processId(mapper, 1);
8639 processMTSync(mapper);
8640 processPosition(mapper, x2, y2);
8641 processId(mapper, 2);
8642 processMTSync(mapper);
8643 processSync(mapper);
8644
8645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8646 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8647 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8648 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8649 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8651 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8652
8653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008654 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008655 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8656 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8657 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8658 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8659 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8661 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8663 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8664
8665 // Move.
8666 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8667 processPosition(mapper, x1, y1);
8668 processId(mapper, 1);
8669 processMTSync(mapper);
8670 processPosition(mapper, x2, y2);
8671 processId(mapper, 2);
8672 processMTSync(mapper);
8673 processSync(mapper);
8674
8675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8676 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8677 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8678 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8679 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8680 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8681 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8683 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8684 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8685 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8686
8687 // First finger up.
8688 x2 += 15; y2 -= 20;
8689 processPosition(mapper, x2, y2);
8690 processId(mapper, 2);
8691 processMTSync(mapper);
8692 processSync(mapper);
8693
8694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008695 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008696 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8697 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8698 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8699 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8700 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8702 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8704 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8705
8706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8707 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8708 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8709 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8712 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8713
8714 // Move.
8715 x2 += 20; y2 -= 25;
8716 processPosition(mapper, x2, y2);
8717 processId(mapper, 2);
8718 processMTSync(mapper);
8719 processSync(mapper);
8720
8721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8722 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8723 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8724 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8725 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8727 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8728
8729 // New finger down.
8730 int32_t x3 = 700, y3 = 300;
8731 processPosition(mapper, x2, y2);
8732 processId(mapper, 2);
8733 processMTSync(mapper);
8734 processPosition(mapper, x3, y3);
8735 processId(mapper, 3);
8736 processMTSync(mapper);
8737 processSync(mapper);
8738
8739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008740 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008741 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8742 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8743 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8744 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8745 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8747 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8748 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8749 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8750
8751 // Second finger up.
8752 x3 += 30; y3 -= 20;
8753 processPosition(mapper, x3, y3);
8754 processId(mapper, 3);
8755 processMTSync(mapper);
8756 processSync(mapper);
8757
8758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008759 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008760 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8761 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8762 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8763 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8764 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8766 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8768 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8769
8770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8771 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8772 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8773 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8774 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8776 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8777
8778 // Last finger up.
8779 processMTSync(mapper);
8780 processSync(mapper);
8781
8782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8783 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8784 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8785 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8786 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8788 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8789
8790 // Should not have sent any more keys or motions.
8791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8793}
8794
8795TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008796 addConfigurationProperty("touch.deviceType", "touchScreen");
8797 prepareDisplay(DISPLAY_ORIENTATION_0);
8798 prepareAxes(POSITION | ID | SLOT);
8799 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008800 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008801
arthurhungdcef2dc2020-08-11 14:47:50 +08008802 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008803
8804 NotifyMotionArgs motionArgs;
8805
8806 // Two fingers down at once.
8807 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8808 processPosition(mapper, x1, y1);
8809 processId(mapper, 1);
8810 processSlot(mapper, 1);
8811 processPosition(mapper, x2, y2);
8812 processId(mapper, 2);
8813 processSync(mapper);
8814
8815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8816 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8817 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8818 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8819 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8821 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8822
8823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008824 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008825 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8826 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8827 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8828 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8829 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8831 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8833 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8834
8835 // Move.
8836 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8837 processSlot(mapper, 0);
8838 processPosition(mapper, x1, y1);
8839 processSlot(mapper, 1);
8840 processPosition(mapper, x2, y2);
8841 processSync(mapper);
8842
8843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8844 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8845 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8846 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8847 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8848 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8849 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8851 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8853 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8854
8855 // First finger up.
8856 x2 += 15; y2 -= 20;
8857 processSlot(mapper, 0);
8858 processId(mapper, -1);
8859 processSlot(mapper, 1);
8860 processPosition(mapper, x2, y2);
8861 processSync(mapper);
8862
8863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008864 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008865 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8866 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8867 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8868 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8869 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8870 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8871 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8872 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8873 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8874
8875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8876 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8877 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8878 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8879 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8881 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8882
8883 // Move.
8884 x2 += 20; y2 -= 25;
8885 processPosition(mapper, x2, y2);
8886 processSync(mapper);
8887
8888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8889 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8890 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8891 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8892 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8894 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8895
8896 // New finger down.
8897 int32_t x3 = 700, y3 = 300;
8898 processPosition(mapper, x2, y2);
8899 processSlot(mapper, 0);
8900 processId(mapper, 3);
8901 processPosition(mapper, x3, y3);
8902 processSync(mapper);
8903
8904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008905 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008906 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8907 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8908 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8909 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8910 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8912 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8913 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8914 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8915
8916 // Second finger up.
8917 x3 += 30; y3 -= 20;
8918 processSlot(mapper, 1);
8919 processId(mapper, -1);
8920 processSlot(mapper, 0);
8921 processPosition(mapper, x3, y3);
8922 processSync(mapper);
8923
8924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008925 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008926 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8927 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8928 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8929 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8930 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8932 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8934 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8935
8936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8937 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8938 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8939 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8940 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8942 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8943
8944 // Last finger up.
8945 processId(mapper, -1);
8946 processSync(mapper);
8947
8948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8949 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8950 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8951 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8952 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8953 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8954 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8955
8956 // Should not have sent any more keys or motions.
8957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8959}
8960
8961TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008962 addConfigurationProperty("touch.deviceType", "touchScreen");
8963 prepareDisplay(DISPLAY_ORIENTATION_0);
8964 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008965 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008966
8967 // These calculations are based on the input device calibration documentation.
8968 int32_t rawX = 100;
8969 int32_t rawY = 200;
8970 int32_t rawTouchMajor = 7;
8971 int32_t rawTouchMinor = 6;
8972 int32_t rawToolMajor = 9;
8973 int32_t rawToolMinor = 8;
8974 int32_t rawPressure = 11;
8975 int32_t rawDistance = 0;
8976 int32_t rawOrientation = 3;
8977 int32_t id = 5;
8978
8979 float x = toDisplayX(rawX);
8980 float y = toDisplayY(rawY);
8981 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8982 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8983 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8984 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8985 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8986 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8987 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8988 float distance = float(rawDistance);
8989
8990 processPosition(mapper, rawX, rawY);
8991 processTouchMajor(mapper, rawTouchMajor);
8992 processTouchMinor(mapper, rawTouchMinor);
8993 processToolMajor(mapper, rawToolMajor);
8994 processToolMinor(mapper, rawToolMinor);
8995 processPressure(mapper, rawPressure);
8996 processOrientation(mapper, rawOrientation);
8997 processDistance(mapper, rawDistance);
8998 processId(mapper, id);
8999 processMTSync(mapper);
9000 processSync(mapper);
9001
9002 NotifyMotionArgs args;
9003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9004 ASSERT_EQ(0, args.pointerProperties[0].id);
9005 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9006 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
9007 orientation, distance));
9008}
9009
9010TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009011 addConfigurationProperty("touch.deviceType", "touchScreen");
9012 prepareDisplay(DISPLAY_ORIENTATION_0);
9013 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
9014 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009015 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009016
9017 // These calculations are based on the input device calibration documentation.
9018 int32_t rawX = 100;
9019 int32_t rawY = 200;
9020 int32_t rawTouchMajor = 140;
9021 int32_t rawTouchMinor = 120;
9022 int32_t rawToolMajor = 180;
9023 int32_t rawToolMinor = 160;
9024
9025 float x = toDisplayX(rawX);
9026 float y = toDisplayY(rawY);
9027 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
9028 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
9029 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
9030 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
9031 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
9032
9033 processPosition(mapper, rawX, rawY);
9034 processTouchMajor(mapper, rawTouchMajor);
9035 processTouchMinor(mapper, rawTouchMinor);
9036 processToolMajor(mapper, rawToolMajor);
9037 processToolMinor(mapper, rawToolMinor);
9038 processMTSync(mapper);
9039 processSync(mapper);
9040
9041 NotifyMotionArgs args;
9042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9044 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
9045}
9046
9047TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009048 addConfigurationProperty("touch.deviceType", "touchScreen");
9049 prepareDisplay(DISPLAY_ORIENTATION_0);
9050 prepareAxes(POSITION | TOUCH | TOOL);
9051 addConfigurationProperty("touch.size.calibration", "diameter");
9052 addConfigurationProperty("touch.size.scale", "10");
9053 addConfigurationProperty("touch.size.bias", "160");
9054 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009055 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009056
9057 // These calculations are based on the input device calibration documentation.
9058 // Note: We only provide a single common touch/tool value because the device is assumed
9059 // not to emit separate values for each pointer (isSummed = 1).
9060 int32_t rawX = 100;
9061 int32_t rawY = 200;
9062 int32_t rawX2 = 150;
9063 int32_t rawY2 = 250;
9064 int32_t rawTouchMajor = 5;
9065 int32_t rawToolMajor = 8;
9066
9067 float x = toDisplayX(rawX);
9068 float y = toDisplayY(rawY);
9069 float x2 = toDisplayX(rawX2);
9070 float y2 = toDisplayY(rawY2);
9071 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
9072 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
9073 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
9074
9075 processPosition(mapper, rawX, rawY);
9076 processTouchMajor(mapper, rawTouchMajor);
9077 processToolMajor(mapper, rawToolMajor);
9078 processMTSync(mapper);
9079 processPosition(mapper, rawX2, rawY2);
9080 processTouchMajor(mapper, rawTouchMajor);
9081 processToolMajor(mapper, rawToolMajor);
9082 processMTSync(mapper);
9083 processSync(mapper);
9084
9085 NotifyMotionArgs args;
9086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9087 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9088
9089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009090 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009091 ASSERT_EQ(size_t(2), args.pointerCount);
9092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9093 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
9094 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
9095 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
9096}
9097
9098TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009099 addConfigurationProperty("touch.deviceType", "touchScreen");
9100 prepareDisplay(DISPLAY_ORIENTATION_0);
9101 prepareAxes(POSITION | TOUCH | TOOL);
9102 addConfigurationProperty("touch.size.calibration", "area");
9103 addConfigurationProperty("touch.size.scale", "43");
9104 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009105 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009106
9107 // These calculations are based on the input device calibration documentation.
9108 int32_t rawX = 100;
9109 int32_t rawY = 200;
9110 int32_t rawTouchMajor = 5;
9111 int32_t rawToolMajor = 8;
9112
9113 float x = toDisplayX(rawX);
9114 float y = toDisplayY(rawY);
9115 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
9116 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
9117 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
9118
9119 processPosition(mapper, rawX, rawY);
9120 processTouchMajor(mapper, rawTouchMajor);
9121 processToolMajor(mapper, rawToolMajor);
9122 processMTSync(mapper);
9123 processSync(mapper);
9124
9125 NotifyMotionArgs args;
9126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9127 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9128 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
9129}
9130
9131TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009132 addConfigurationProperty("touch.deviceType", "touchScreen");
9133 prepareDisplay(DISPLAY_ORIENTATION_0);
9134 prepareAxes(POSITION | PRESSURE);
9135 addConfigurationProperty("touch.pressure.calibration", "amplitude");
9136 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009137 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009138
Michael Wrightaa449c92017-12-13 21:21:43 +00009139 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009140 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00009141 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
9142 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
9143 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
9144
Michael Wrightd02c5b62014-02-10 15:10:22 -08009145 // These calculations are based on the input device calibration documentation.
9146 int32_t rawX = 100;
9147 int32_t rawY = 200;
9148 int32_t rawPressure = 60;
9149
9150 float x = toDisplayX(rawX);
9151 float y = toDisplayY(rawY);
9152 float pressure = float(rawPressure) * 0.01f;
9153
9154 processPosition(mapper, rawX, rawY);
9155 processPressure(mapper, rawPressure);
9156 processMTSync(mapper);
9157 processSync(mapper);
9158
9159 NotifyMotionArgs args;
9160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9161 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9162 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
9163}
9164
9165TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009166 addConfigurationProperty("touch.deviceType", "touchScreen");
9167 prepareDisplay(DISPLAY_ORIENTATION_0);
9168 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009169 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009170
9171 NotifyMotionArgs motionArgs;
9172 NotifyKeyArgs keyArgs;
9173
9174 processId(mapper, 1);
9175 processPosition(mapper, 100, 200);
9176 processSync(mapper);
9177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9178 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9179 ASSERT_EQ(0, motionArgs.buttonState);
9180
9181 // press BTN_LEFT, release BTN_LEFT
9182 processKey(mapper, BTN_LEFT, 1);
9183 processSync(mapper);
9184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9185 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9186 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
9187
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9189 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9190 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
9191
Michael Wrightd02c5b62014-02-10 15:10:22 -08009192 processKey(mapper, BTN_LEFT, 0);
9193 processSync(mapper);
9194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009195 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009196 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009197
9198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009199 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009200 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009201
9202 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
9203 processKey(mapper, BTN_RIGHT, 1);
9204 processKey(mapper, BTN_MIDDLE, 1);
9205 processSync(mapper);
9206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9207 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9208 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
9209 motionArgs.buttonState);
9210
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9212 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9213 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
9214
9215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9216 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9217 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
9218 motionArgs.buttonState);
9219
Michael Wrightd02c5b62014-02-10 15:10:22 -08009220 processKey(mapper, BTN_RIGHT, 0);
9221 processSync(mapper);
9222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009223 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009224 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009225
9226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009228 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009229
9230 processKey(mapper, BTN_MIDDLE, 0);
9231 processSync(mapper);
9232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009233 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009234 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009235
9236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009238 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009239
9240 // press BTN_BACK, release BTN_BACK
9241 processKey(mapper, BTN_BACK, 1);
9242 processSync(mapper);
9243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9244 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9245 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009246
Michael Wrightd02c5b62014-02-10 15:10:22 -08009247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009249 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
9250
9251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9252 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9253 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009254
9255 processKey(mapper, BTN_BACK, 0);
9256 processSync(mapper);
9257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009258 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009259 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009260
9261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009262 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009263 ASSERT_EQ(0, motionArgs.buttonState);
9264
Michael Wrightd02c5b62014-02-10 15:10:22 -08009265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9266 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9267 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
9268
9269 // press BTN_SIDE, release BTN_SIDE
9270 processKey(mapper, BTN_SIDE, 1);
9271 processSync(mapper);
9272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9273 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9274 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009275
Michael Wrightd02c5b62014-02-10 15:10:22 -08009276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009278 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
9279
9280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9281 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9282 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009283
9284 processKey(mapper, BTN_SIDE, 0);
9285 processSync(mapper);
9286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009287 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009288 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009289
9290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009291 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009292 ASSERT_EQ(0, motionArgs.buttonState);
9293
Michael Wrightd02c5b62014-02-10 15:10:22 -08009294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9295 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9296 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
9297
9298 // press BTN_FORWARD, release BTN_FORWARD
9299 processKey(mapper, BTN_FORWARD, 1);
9300 processSync(mapper);
9301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9302 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9303 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009304
Michael Wrightd02c5b62014-02-10 15:10:22 -08009305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009306 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009307 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9308
9309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9310 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9311 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009312
9313 processKey(mapper, BTN_FORWARD, 0);
9314 processSync(mapper);
9315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009316 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009317 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009318
9319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009320 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009321 ASSERT_EQ(0, motionArgs.buttonState);
9322
Michael Wrightd02c5b62014-02-10 15:10:22 -08009323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9324 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9325 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9326
9327 // press BTN_EXTRA, release BTN_EXTRA
9328 processKey(mapper, BTN_EXTRA, 1);
9329 processSync(mapper);
9330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9331 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9332 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009333
Michael Wrightd02c5b62014-02-10 15:10:22 -08009334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009335 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009336 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9337
9338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9339 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9340 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009341
9342 processKey(mapper, BTN_EXTRA, 0);
9343 processSync(mapper);
9344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009345 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009346 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009347
9348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009350 ASSERT_EQ(0, motionArgs.buttonState);
9351
Michael Wrightd02c5b62014-02-10 15:10:22 -08009352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9353 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9354 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9355
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9357
Michael Wrightd02c5b62014-02-10 15:10:22 -08009358 // press BTN_STYLUS, release BTN_STYLUS
9359 processKey(mapper, BTN_STYLUS, 1);
9360 processSync(mapper);
9361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009363 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
9364
9365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9366 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9367 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009368
9369 processKey(mapper, BTN_STYLUS, 0);
9370 processSync(mapper);
9371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009372 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009373 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009374
9375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009376 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009377 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009378
9379 // press BTN_STYLUS2, release BTN_STYLUS2
9380 processKey(mapper, BTN_STYLUS2, 1);
9381 processSync(mapper);
9382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009384 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
9385
9386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9387 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9388 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009389
9390 processKey(mapper, BTN_STYLUS2, 0);
9391 processSync(mapper);
9392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009393 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009394 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009395
9396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009398 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009399
9400 // release touch
9401 processId(mapper, -1);
9402 processSync(mapper);
9403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9404 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9405 ASSERT_EQ(0, motionArgs.buttonState);
9406}
9407
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009408TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
9409 addConfigurationProperty("touch.deviceType", "touchScreen");
9410 prepareDisplay(DISPLAY_ORIENTATION_0);
9411 prepareAxes(POSITION | ID | SLOT);
9412 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9413
9414 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
9415 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
9416
9417 // Touch down.
9418 processId(mapper, 1);
9419 processPosition(mapper, 100, 200);
9420 processSync(mapper);
9421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9422 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
9423
9424 // Press and release button mapped to the primary stylus button.
9425 processKey(mapper, BTN_A, 1);
9426 processSync(mapper);
9427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9428 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9429 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9431 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9432 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9433
9434 processKey(mapper, BTN_A, 0);
9435 processSync(mapper);
9436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9437 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9439 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9440
9441 // Press and release the HID usage mapped to the secondary stylus button.
9442 processHidUsage(mapper, 0xabcd, 1);
9443 processSync(mapper);
9444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9445 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9446 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9448 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9449 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9450
9451 processHidUsage(mapper, 0xabcd, 0);
9452 processSync(mapper);
9453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9454 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9456 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9457
9458 // Release touch.
9459 processId(mapper, -1);
9460 processSync(mapper);
9461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9462 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
9463}
9464
Michael Wrightd02c5b62014-02-10 15:10:22 -08009465TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009466 addConfigurationProperty("touch.deviceType", "touchScreen");
9467 prepareDisplay(DISPLAY_ORIENTATION_0);
9468 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009469 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009470
9471 NotifyMotionArgs motionArgs;
9472
9473 // default tool type is finger
9474 processId(mapper, 1);
9475 processPosition(mapper, 100, 200);
9476 processSync(mapper);
9477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9478 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9479 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9480
9481 // eraser
9482 processKey(mapper, BTN_TOOL_RUBBER, 1);
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_ERASER, motionArgs.pointerProperties[0].toolType);
9487
9488 // stylus
9489 processKey(mapper, BTN_TOOL_RUBBER, 0);
9490 processKey(mapper, BTN_TOOL_PEN, 1);
9491 processSync(mapper);
9492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9493 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9494 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9495
9496 // brush
9497 processKey(mapper, BTN_TOOL_PEN, 0);
9498 processKey(mapper, BTN_TOOL_BRUSH, 1);
9499 processSync(mapper);
9500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9502 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9503
9504 // pencil
9505 processKey(mapper, BTN_TOOL_BRUSH, 0);
9506 processKey(mapper, BTN_TOOL_PENCIL, 1);
9507 processSync(mapper);
9508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9509 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9510 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9511
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009512 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009513 processKey(mapper, BTN_TOOL_PENCIL, 0);
9514 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
9515 processSync(mapper);
9516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9517 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9518 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9519
9520 // mouse
9521 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9522 processKey(mapper, BTN_TOOL_MOUSE, 1);
9523 processSync(mapper);
9524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9525 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9526 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
9527
9528 // lens
9529 processKey(mapper, BTN_TOOL_MOUSE, 0);
9530 processKey(mapper, BTN_TOOL_LENS, 1);
9531 processSync(mapper);
9532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9533 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9534 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
9535
9536 // double-tap
9537 processKey(mapper, BTN_TOOL_LENS, 0);
9538 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
9539 processSync(mapper);
9540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9541 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9543
9544 // triple-tap
9545 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9546 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
9547 processSync(mapper);
9548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9550 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9551
9552 // quad-tap
9553 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9554 processKey(mapper, BTN_TOOL_QUADTAP, 1);
9555 processSync(mapper);
9556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9557 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9558 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9559
9560 // finger
9561 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9562 processKey(mapper, BTN_TOOL_FINGER, 1);
9563 processSync(mapper);
9564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9566 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9567
9568 // stylus trumps finger
9569 processKey(mapper, BTN_TOOL_PEN, 1);
9570 processSync(mapper);
9571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9572 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9573 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9574
9575 // eraser trumps stylus
9576 processKey(mapper, BTN_TOOL_RUBBER, 1);
9577 processSync(mapper);
9578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9580 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
9581
9582 // mouse trumps eraser
9583 processKey(mapper, BTN_TOOL_MOUSE, 1);
9584 processSync(mapper);
9585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9586 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9587 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
9588
9589 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9590 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9591 processSync(mapper);
9592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9593 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9594 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9595
9596 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9597 processToolType(mapper, MT_TOOL_PEN);
9598 processSync(mapper);
9599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9601 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9602
9603 // back to default tool type
9604 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9605 processKey(mapper, BTN_TOOL_MOUSE, 0);
9606 processKey(mapper, BTN_TOOL_RUBBER, 0);
9607 processKey(mapper, BTN_TOOL_PEN, 0);
9608 processKey(mapper, BTN_TOOL_FINGER, 0);
9609 processSync(mapper);
9610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9612 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9613}
9614
9615TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009616 addConfigurationProperty("touch.deviceType", "touchScreen");
9617 prepareDisplay(DISPLAY_ORIENTATION_0);
9618 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009619 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009620 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009621
9622 NotifyMotionArgs motionArgs;
9623
9624 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9625 processId(mapper, 1);
9626 processPosition(mapper, 100, 200);
9627 processSync(mapper);
9628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9629 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9631 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9632
9633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9634 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9636 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9637
9638 // move a little
9639 processPosition(mapper, 150, 250);
9640 processSync(mapper);
9641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9642 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9644 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9645
9646 // down when BTN_TOUCH is pressed, pressure defaults to 1
9647 processKey(mapper, BTN_TOUCH, 1);
9648 processSync(mapper);
9649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9650 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9651 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9652 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9653
9654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9655 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9657 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9658
9659 // up when BTN_TOUCH is released, hover restored
9660 processKey(mapper, BTN_TOUCH, 0);
9661 processSync(mapper);
9662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9663 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9664 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9665 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9666
9667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9668 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9670 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9671
9672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9673 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9674 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9675 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9676
9677 // exit hover when pointer goes away
9678 processId(mapper, -1);
9679 processSync(mapper);
9680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9681 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9683 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9684}
9685
9686TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009687 addConfigurationProperty("touch.deviceType", "touchScreen");
9688 prepareDisplay(DISPLAY_ORIENTATION_0);
9689 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009690 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009691
9692 NotifyMotionArgs motionArgs;
9693
9694 // initially hovering because pressure is 0
9695 processId(mapper, 1);
9696 processPosition(mapper, 100, 200);
9697 processPressure(mapper, 0);
9698 processSync(mapper);
9699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9700 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9701 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9702 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9703
9704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9705 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9706 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9707 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9708
9709 // move a little
9710 processPosition(mapper, 150, 250);
9711 processSync(mapper);
9712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9713 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9714 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9715 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9716
9717 // down when pressure becomes non-zero
9718 processPressure(mapper, RAW_PRESSURE_MAX);
9719 processSync(mapper);
9720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9721 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9723 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9724
9725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9726 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9727 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9728 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9729
9730 // up when pressure becomes 0, hover restored
9731 processPressure(mapper, 0);
9732 processSync(mapper);
9733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9734 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9736 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9737
9738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9739 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9741 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9742
9743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9744 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9746 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9747
9748 // exit hover when pointer goes away
9749 processId(mapper, -1);
9750 processSync(mapper);
9751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9752 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9754 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9755}
9756
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009757/**
9758 * Set the input device port <--> display port associations, and check that the
9759 * events are routed to the display that matches the display port.
9760 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9761 */
9762TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009763 const std::string usb2 = "USB2";
9764 const uint8_t hdmi1 = 0;
9765 const uint8_t hdmi2 = 1;
9766 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009767 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009768
9769 addConfigurationProperty("touch.deviceType", "touchScreen");
9770 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009771 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009772
9773 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9774 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9775
9776 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9777 // for this input device is specified, and the matching viewport is not present,
9778 // the input device should be disabled (at the mapper level).
9779
9780 // Add viewport for display 2 on hdmi2
9781 prepareSecondaryDisplay(type, hdmi2);
9782 // Send a touch event
9783 processPosition(mapper, 100, 100);
9784 processSync(mapper);
9785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9786
9787 // Add viewport for display 1 on hdmi1
9788 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
9789 // Send a touch event again
9790 processPosition(mapper, 100, 100);
9791 processSync(mapper);
9792
9793 NotifyMotionArgs args;
9794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9795 ASSERT_EQ(DISPLAY_ID, args.displayId);
9796}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009797
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009798TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9799 addConfigurationProperty("touch.deviceType", "touchScreen");
9800 prepareAxes(POSITION);
9801 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9802
9803 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9804
9805 prepareDisplay(DISPLAY_ORIENTATION_0);
9806 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9807
9808 // Send a touch event
9809 processPosition(mapper, 100, 100);
9810 processSync(mapper);
9811
9812 NotifyMotionArgs args;
9813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9814 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9815}
9816
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009817TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009818 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009819 std::shared_ptr<FakePointerController> fakePointerController =
9820 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009821 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009822 fakePointerController->setPosition(100, 200);
9823 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009824 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009825
Garfield Tan888a6a42020-01-09 11:39:16 -08009826 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009827 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009828
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009829 prepareDisplay(DISPLAY_ORIENTATION_0);
9830 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009831 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009832
Harry Cutts16a24cc2022-10-26 15:22:19 +00009833 // Check source is a touchpad that would obtain the PointerController.
9834 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009835
9836 NotifyMotionArgs motionArgs;
9837 processPosition(mapper, 100, 100);
9838 processSync(mapper);
9839
9840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9841 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9842 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9843}
9844
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009845/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009846 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9847 */
9848TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9849 addConfigurationProperty("touch.deviceType", "touchScreen");
9850 prepareAxes(POSITION);
9851 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9852
9853 prepareDisplay(DISPLAY_ORIENTATION_0);
9854 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
9855 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
9856 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
9857 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9858
9859 NotifyMotionArgs args;
9860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9861 ASSERT_EQ(26, args.readTime);
9862
9863 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
9864 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
9865 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9866
9867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9868 ASSERT_EQ(33, args.readTime);
9869}
9870
9871/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009872 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9873 * events should not be delivered to the listener.
9874 */
9875TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9876 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009877 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009878 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9879 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
9880 ViewportType::INTERNAL);
9881 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9882 prepareAxes(POSITION);
9883 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9884
9885 NotifyMotionArgs motionArgs;
9886 processPosition(mapper, 100, 100);
9887 processSync(mapper);
9888
9889 mFakeListener->assertNotifyMotionWasNotCalled();
9890}
9891
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009892/**
9893 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9894 * the touch mapper can process the events and the events can be delivered to the listener.
9895 */
9896TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9897 addConfigurationProperty("touch.deviceType", "touchScreen");
9898 addConfigurationProperty("touch.enableForInactiveViewport", "1");
9899 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9900 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
9901 ViewportType::INTERNAL);
9902 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9903 prepareAxes(POSITION);
9904 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9905
9906 NotifyMotionArgs motionArgs;
9907 processPosition(mapper, 100, 100);
9908 processSync(mapper);
9909
9910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9911 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9912}
9913
Garfield Tanc734e4f2021-01-15 20:01:39 -08009914TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9915 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009916 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Garfield Tanc734e4f2021-01-15 20:01:39 -08009917 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9918 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
9919 ViewportType::INTERNAL);
9920 std::optional<DisplayViewport> optionalDisplayViewport =
9921 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9922 ASSERT_TRUE(optionalDisplayViewport.has_value());
9923 DisplayViewport displayViewport = *optionalDisplayViewport;
9924
9925 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9926 prepareAxes(POSITION);
9927 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9928
9929 // Finger down
9930 int32_t x = 100, y = 100;
9931 processPosition(mapper, x, y);
9932 processSync(mapper);
9933
9934 NotifyMotionArgs motionArgs;
9935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9936 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9937
9938 // Deactivate display viewport
9939 displayViewport.isActive = false;
9940 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9941 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9942
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009943 // The ongoing touch should be canceled immediately
9944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9945 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9946
9947 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009948 x += 10, y += 10;
9949 processPosition(mapper, x, y);
9950 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009952
9953 // Reactivate display viewport
9954 displayViewport.isActive = true;
9955 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9956 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9957
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009958 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009959 x += 10, y += 10;
9960 processPosition(mapper, x, y);
9961 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9963 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009964}
9965
Arthur Hung7c645402019-01-25 17:45:42 +08009966TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9967 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009968 prepareAxes(POSITION | ID | SLOT);
9969 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009970 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009971
9972 // Create the second touch screen device, and enable multi fingers.
9973 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009974 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009975 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009976 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009977 std::shared_ptr<InputDevice> device2 =
9978 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009979 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009980
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009981 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9982 0 /*flat*/, 0 /*fuzz*/);
9983 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9984 0 /*flat*/, 0 /*fuzz*/);
9985 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
9986 0 /*flat*/, 0 /*fuzz*/);
9987 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
9988 0 /*flat*/, 0 /*fuzz*/);
9989 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
9990 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9991 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009992
9993 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009994 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009995 std::list<NotifyArgs> unused =
9996 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9997 0 /*changes*/);
9998 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009999
10000 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +010010001 std::shared_ptr<FakePointerController> fakePointerController =
10002 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010003 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +080010004
10005 // Setup policy for associated displays and show touches.
10006 const uint8_t hdmi1 = 0;
10007 const uint8_t hdmi2 = 1;
10008 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
10009 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
10010 mFakePolicy->setShowTouches(true);
10011
10012 // Create displays.
10013 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010014 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +080010015
10016 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010017 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10018 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
10019 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +080010020
10021 // Two fingers down at default display.
10022 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
10023 processPosition(mapper, x1, y1);
10024 processId(mapper, 1);
10025 processSlot(mapper, 1);
10026 processPosition(mapper, x2, y2);
10027 processId(mapper, 2);
10028 processSync(mapper);
10029
10030 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
10031 fakePointerController->getSpots().find(DISPLAY_ID);
10032 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
10033 ASSERT_EQ(size_t(2), iter->second.size());
10034
10035 // Two fingers down at second display.
10036 processPosition(mapper2, x1, y1);
10037 processId(mapper2, 1);
10038 processSlot(mapper2, 1);
10039 processPosition(mapper2, x2, y2);
10040 processId(mapper2, 2);
10041 processSync(mapper2);
10042
10043 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
10044 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
10045 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +000010046
10047 // Disable the show touches configuration and ensure the spots are cleared.
10048 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010049 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10050 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +000010051
10052 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +080010053}
10054
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010055TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010056 prepareAxes(POSITION);
10057 addConfigurationProperty("touch.deviceType", "touchScreen");
10058 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010059 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010060
10061 NotifyMotionArgs motionArgs;
10062 // Unrotated video frame
10063 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10064 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010065 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010066 processPosition(mapper, 100, 200);
10067 processSync(mapper);
10068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10069 ASSERT_EQ(frames, motionArgs.videoFrames);
10070
10071 // Subsequent touch events should not have any videoframes
10072 // This is implemented separately in FakeEventHub,
10073 // but that should match the behaviour of TouchVideoDevice.
10074 processPosition(mapper, 200, 200);
10075 processSync(mapper);
10076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10077 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
10078}
10079
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010080TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010081 prepareAxes(POSITION);
10082 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010083 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010084 // Unrotated video frame
10085 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10086 NotifyMotionArgs motionArgs;
10087
10088 // Test all 4 orientations
10089 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010090 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
10091 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
10092 clearViewports();
10093 prepareDisplay(orientation);
10094 std::vector<TouchVideoFrame> frames{frame};
10095 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
10096 processPosition(mapper, 100, 200);
10097 processSync(mapper);
10098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10099 ASSERT_EQ(frames, motionArgs.videoFrames);
10100 }
10101}
10102
10103TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
10104 prepareAxes(POSITION);
10105 addConfigurationProperty("touch.deviceType", "touchScreen");
10106 // Since InputReader works in the un-rotated coordinate space, only devices that are not
10107 // orientation-aware are affected by display rotation.
10108 addConfigurationProperty("touch.orientationAware", "0");
10109 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10110 // Unrotated video frame
10111 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10112 NotifyMotionArgs motionArgs;
10113
10114 // Test all 4 orientations
10115 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010116 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
10117 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
10118 clearViewports();
10119 prepareDisplay(orientation);
10120 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010121 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010122 processPosition(mapper, 100, 200);
10123 processSync(mapper);
10124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010125 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
10126 // compared to the display. This is so that when the window transform (which contains the
10127 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
10128 // window's coordinate space.
10129 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010130 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +080010131
10132 // Release finger.
10133 processSync(mapper);
10134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010135 }
10136}
10137
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010138TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010139 prepareAxes(POSITION);
10140 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010141 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010142 // Unrotated video frames. There's no rule that they must all have the same dimensions,
10143 // so mix these.
10144 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10145 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
10146 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
10147 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
10148 NotifyMotionArgs motionArgs;
10149
10150 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010151 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010152 processPosition(mapper, 100, 200);
10153 processSync(mapper);
10154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010155 ASSERT_EQ(frames, motionArgs.videoFrames);
10156}
10157
10158TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
10159 prepareAxes(POSITION);
10160 addConfigurationProperty("touch.deviceType", "touchScreen");
10161 // Since InputReader works in the un-rotated coordinate space, only devices that are not
10162 // orientation-aware are affected by display rotation.
10163 addConfigurationProperty("touch.orientationAware", "0");
10164 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10165 // Unrotated video frames. There's no rule that they must all have the same dimensions,
10166 // so mix these.
10167 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10168 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
10169 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
10170 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
10171 NotifyMotionArgs motionArgs;
10172
10173 prepareDisplay(DISPLAY_ORIENTATION_90);
10174 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
10175 processPosition(mapper, 100, 200);
10176 processSync(mapper);
10177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10178 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
10179 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
10180 // compared to the display. This is so that when the window transform (which contains the
10181 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
10182 // window's coordinate space.
10183 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
10184 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010185 ASSERT_EQ(frames, motionArgs.videoFrames);
10186}
10187
Arthur Hung9da14732019-09-02 16:16:58 +080010188/**
10189 * If we had defined port associations, but the viewport is not ready, the touch device would be
10190 * expected to be disabled, and it should be enabled after the viewport has found.
10191 */
10192TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +080010193 constexpr uint8_t hdmi2 = 1;
10194 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010195 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +080010196
10197 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
10198
10199 addConfigurationProperty("touch.deviceType", "touchScreen");
10200 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010201 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +080010202
10203 ASSERT_EQ(mDevice->isEnabled(), false);
10204
10205 // Add display on hdmi2, the device should be enabled and can receive touch event.
10206 prepareSecondaryDisplay(type, hdmi2);
10207 ASSERT_EQ(mDevice->isEnabled(), true);
10208
10209 // Send a touch event.
10210 processPosition(mapper, 100, 100);
10211 processSync(mapper);
10212
10213 NotifyMotionArgs args;
10214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10215 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
10216}
10217
Arthur Hung421eb1c2020-01-16 00:09:42 +080010218TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010219 addConfigurationProperty("touch.deviceType", "touchScreen");
10220 prepareDisplay(DISPLAY_ORIENTATION_0);
10221 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010222 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010223
10224 NotifyMotionArgs motionArgs;
10225
10226 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10227 // finger down
10228 processId(mapper, 1);
10229 processPosition(mapper, x1, y1);
10230 processSync(mapper);
10231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10232 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10233 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10234
10235 // finger move
10236 processId(mapper, 1);
10237 processPosition(mapper, x2, y2);
10238 processSync(mapper);
10239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10241 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10242
10243 // finger up.
10244 processId(mapper, -1);
10245 processSync(mapper);
10246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10247 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10248 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10249
10250 // new finger down
10251 processId(mapper, 1);
10252 processPosition(mapper, x3, y3);
10253 processSync(mapper);
10254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10255 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10256 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10257}
10258
10259/**
arthurhungcc7f9802020-04-30 17:55:40 +080010260 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
10261 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +080010262 */
arthurhungcc7f9802020-04-30 17:55:40 +080010263TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010264 addConfigurationProperty("touch.deviceType", "touchScreen");
10265 prepareDisplay(DISPLAY_ORIENTATION_0);
10266 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010267 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010268
10269 NotifyMotionArgs motionArgs;
10270
10271 // default tool type is finger
10272 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +080010273 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010274 processPosition(mapper, x1, y1);
10275 processSync(mapper);
10276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10277 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10278 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10279
10280 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
10281 processToolType(mapper, MT_TOOL_PALM);
10282 processSync(mapper);
10283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10284 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10285
10286 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +080010287 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010288 processPosition(mapper, x2, y2);
10289 processSync(mapper);
10290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10291
10292 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +080010293 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010294 processSync(mapper);
10295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10296
10297 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +080010298 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010299 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010300 processPosition(mapper, x3, y3);
10301 processSync(mapper);
10302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10303 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10305}
10306
arthurhungbf89a482020-04-17 17:37:55 +080010307/**
arthurhungcc7f9802020-04-30 17:55:40 +080010308 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10309 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +080010310 */
arthurhungcc7f9802020-04-30 17:55:40 +080010311TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +080010312 addConfigurationProperty("touch.deviceType", "touchScreen");
10313 prepareDisplay(DISPLAY_ORIENTATION_0);
10314 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
10315 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10316
10317 NotifyMotionArgs motionArgs;
10318
10319 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +080010320 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10321 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010322 processPosition(mapper, x1, y1);
10323 processSync(mapper);
10324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10325 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10326 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10327
10328 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +080010329 processSlot(mapper, SECOND_SLOT);
10330 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010331 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +080010332 processSync(mapper);
10333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010334 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010335 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
10336
10337 // If the tool type of the first finger changes to MT_TOOL_PALM,
10338 // we expect to receive ACTION_POINTER_UP with cancel flag.
10339 processSlot(mapper, FIRST_SLOT);
10340 processId(mapper, FIRST_TRACKING_ID);
10341 processToolType(mapper, MT_TOOL_PALM);
10342 processSync(mapper);
10343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010344 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010345 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10346
10347 // The following MOVE events of second finger should be processed.
10348 processSlot(mapper, SECOND_SLOT);
10349 processId(mapper, SECOND_TRACKING_ID);
10350 processPosition(mapper, x2 + 1, y2 + 1);
10351 processSync(mapper);
10352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10353 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10354 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10355
10356 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
10357 // it. Second finger receive move.
10358 processSlot(mapper, FIRST_SLOT);
10359 processId(mapper, INVALID_TRACKING_ID);
10360 processSync(mapper);
10361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10363 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10364
10365 // Second finger keeps moving.
10366 processSlot(mapper, SECOND_SLOT);
10367 processId(mapper, SECOND_TRACKING_ID);
10368 processPosition(mapper, x2 + 2, y2 + 2);
10369 processSync(mapper);
10370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10372 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10373
10374 // Second finger up.
10375 processId(mapper, INVALID_TRACKING_ID);
10376 processSync(mapper);
10377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10378 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10379 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10380}
10381
10382/**
10383 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
10384 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
10385 */
10386TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
10387 addConfigurationProperty("touch.deviceType", "touchScreen");
10388 prepareDisplay(DISPLAY_ORIENTATION_0);
10389 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
10390 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10391
10392 NotifyMotionArgs motionArgs;
10393
10394 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10395 // First finger down.
10396 processId(mapper, FIRST_TRACKING_ID);
10397 processPosition(mapper, x1, y1);
10398 processSync(mapper);
10399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10400 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10401 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10402
10403 // Second finger down.
10404 processSlot(mapper, SECOND_SLOT);
10405 processId(mapper, SECOND_TRACKING_ID);
10406 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +080010407 processSync(mapper);
10408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010409 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +080010410 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10411
arthurhungcc7f9802020-04-30 17:55:40 +080010412 // If the tool type of the first finger changes to MT_TOOL_PALM,
10413 // we expect to receive ACTION_POINTER_UP with cancel flag.
10414 processSlot(mapper, FIRST_SLOT);
10415 processId(mapper, FIRST_TRACKING_ID);
10416 processToolType(mapper, MT_TOOL_PALM);
10417 processSync(mapper);
10418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010419 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010420 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10421
10422 // Second finger keeps moving.
10423 processSlot(mapper, SECOND_SLOT);
10424 processId(mapper, SECOND_TRACKING_ID);
10425 processPosition(mapper, x2 + 1, y2 + 1);
10426 processSync(mapper);
10427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10429
10430 // second finger becomes palm, receive cancel due to only 1 finger is active.
10431 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010432 processToolType(mapper, MT_TOOL_PALM);
10433 processSync(mapper);
10434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10435 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10436
arthurhungcc7f9802020-04-30 17:55:40 +080010437 // third finger down.
10438 processSlot(mapper, THIRD_SLOT);
10439 processId(mapper, THIRD_TRACKING_ID);
10440 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +080010441 processPosition(mapper, x3, y3);
10442 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +080010443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10444 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10445 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010446 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10447
10448 // third finger move
10449 processId(mapper, THIRD_TRACKING_ID);
10450 processPosition(mapper, x3 + 1, y3 + 1);
10451 processSync(mapper);
10452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10453 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10454
10455 // first finger up, third finger receive move.
10456 processSlot(mapper, FIRST_SLOT);
10457 processId(mapper, INVALID_TRACKING_ID);
10458 processSync(mapper);
10459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10461 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10462
10463 // second finger up, third finger receive move.
10464 processSlot(mapper, SECOND_SLOT);
10465 processId(mapper, INVALID_TRACKING_ID);
10466 processSync(mapper);
10467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10468 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10469 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10470
10471 // third finger up.
10472 processSlot(mapper, THIRD_SLOT);
10473 processId(mapper, INVALID_TRACKING_ID);
10474 processSync(mapper);
10475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10476 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10477 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10478}
10479
10480/**
10481 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10482 * and the active finger could still be allowed to receive the events
10483 */
10484TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
10485 addConfigurationProperty("touch.deviceType", "touchScreen");
10486 prepareDisplay(DISPLAY_ORIENTATION_0);
10487 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
10488 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10489
10490 NotifyMotionArgs motionArgs;
10491
10492 // default tool type is finger
10493 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10494 processId(mapper, FIRST_TRACKING_ID);
10495 processPosition(mapper, x1, y1);
10496 processSync(mapper);
10497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10498 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10499 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10500
10501 // Second finger down.
10502 processSlot(mapper, SECOND_SLOT);
10503 processId(mapper, SECOND_TRACKING_ID);
10504 processPosition(mapper, x2, y2);
10505 processSync(mapper);
10506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010507 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010508 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10509
10510 // If the tool type of the second finger changes to MT_TOOL_PALM,
10511 // we expect to receive ACTION_POINTER_UP with cancel flag.
10512 processId(mapper, SECOND_TRACKING_ID);
10513 processToolType(mapper, MT_TOOL_PALM);
10514 processSync(mapper);
10515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010516 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010517 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10518
10519 // The following MOVE event should be processed.
10520 processSlot(mapper, FIRST_SLOT);
10521 processId(mapper, FIRST_TRACKING_ID);
10522 processPosition(mapper, x1 + 1, y1 + 1);
10523 processSync(mapper);
10524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10525 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10526 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10527
10528 // second finger up.
10529 processSlot(mapper, SECOND_SLOT);
10530 processId(mapper, INVALID_TRACKING_ID);
10531 processSync(mapper);
10532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10533 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10534
10535 // first finger keep moving
10536 processSlot(mapper, FIRST_SLOT);
10537 processId(mapper, FIRST_TRACKING_ID);
10538 processPosition(mapper, x1 + 2, y1 + 2);
10539 processSync(mapper);
10540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10541 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10542
10543 // first finger up.
10544 processId(mapper, INVALID_TRACKING_ID);
10545 processSync(mapper);
10546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10547 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10548 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010549}
10550
Arthur Hung9ad18942021-06-19 02:04:46 +000010551/**
10552 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10553 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10554 * cause slot be valid again.
10555 */
10556TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10557 addConfigurationProperty("touch.deviceType", "touchScreen");
10558 prepareDisplay(DISPLAY_ORIENTATION_0);
10559 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10560 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10561
10562 NotifyMotionArgs motionArgs;
10563
10564 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10565 // First finger down.
10566 processId(mapper, FIRST_TRACKING_ID);
10567 processPosition(mapper, x1, y1);
10568 processPressure(mapper, RAW_PRESSURE_MAX);
10569 processSync(mapper);
10570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10571 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10572 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10573
10574 // First finger move.
10575 processId(mapper, FIRST_TRACKING_ID);
10576 processPosition(mapper, x1 + 1, y1 + 1);
10577 processPressure(mapper, RAW_PRESSURE_MAX);
10578 processSync(mapper);
10579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10581 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10582
10583 // Second finger down.
10584 processSlot(mapper, SECOND_SLOT);
10585 processId(mapper, SECOND_TRACKING_ID);
10586 processPosition(mapper, x2, y2);
10587 processPressure(mapper, RAW_PRESSURE_MAX);
10588 processSync(mapper);
10589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010590 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010591 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10592
10593 // second finger up with some unexpected data.
10594 processSlot(mapper, SECOND_SLOT);
10595 processId(mapper, INVALID_TRACKING_ID);
10596 processPosition(mapper, x2, y2);
10597 processSync(mapper);
10598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010599 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010600 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10601
10602 // first finger up with some unexpected data.
10603 processSlot(mapper, FIRST_SLOT);
10604 processId(mapper, INVALID_TRACKING_ID);
10605 processPosition(mapper, x2, y2);
10606 processPressure(mapper, RAW_PRESSURE_MAX);
10607 processSync(mapper);
10608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10609 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10610 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10611}
10612
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010613TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10614 addConfigurationProperty("touch.deviceType", "touchScreen");
10615 prepareDisplay(DISPLAY_ORIENTATION_0);
10616 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10617 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10618
10619 // First finger down.
10620 processId(mapper, FIRST_TRACKING_ID);
10621 processPosition(mapper, 100, 200);
10622 processPressure(mapper, RAW_PRESSURE_MAX);
10623 processSync(mapper);
10624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10625 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10626
10627 // Second finger down.
10628 processSlot(mapper, SECOND_SLOT);
10629 processId(mapper, SECOND_TRACKING_ID);
10630 processPosition(mapper, 300, 400);
10631 processPressure(mapper, RAW_PRESSURE_MAX);
10632 processSync(mapper);
10633 ASSERT_NO_FATAL_FAILURE(
10634 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10635
10636 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010637 // preserved. Resetting should cancel the ongoing gesture.
10638 resetMapper(mapper, ARBITRARY_TIME);
10639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10640 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010641
10642 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10643 // the existing touch state to generate a down event.
10644 processPosition(mapper, 301, 302);
10645 processSync(mapper);
10646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10647 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10649 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10650
10651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10652}
10653
10654TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10655 addConfigurationProperty("touch.deviceType", "touchScreen");
10656 prepareDisplay(DISPLAY_ORIENTATION_0);
10657 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10658 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10659
10660 // First finger touches down and releases.
10661 processId(mapper, FIRST_TRACKING_ID);
10662 processPosition(mapper, 100, 200);
10663 processPressure(mapper, RAW_PRESSURE_MAX);
10664 processSync(mapper);
10665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10666 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10667 processId(mapper, INVALID_TRACKING_ID);
10668 processSync(mapper);
10669 ASSERT_NO_FATAL_FAILURE(
10670 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10671
10672 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10673 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010674 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10676
10677 // Send an empty sync frame. Since there are no pointers, no events are generated.
10678 processSync(mapper);
10679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10680}
10681
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010682TEST_F(MultiTouchInputMapperTest, ToolTypeSource) {
10683 addConfigurationProperty("touch.deviceType", "touchScreen");
10684 prepareDisplay(DISPLAY_ORIENTATION_0);
10685 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
10686 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10687
10688 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10689 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10690 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10691 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10692 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10693
10694 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
10695 // reported with the stylus source, even through the device doesn't support the stylus source.
10696 processId(mapper, FIRST_TRACKING_ID);
10697 processToolType(mapper, MT_TOOL_PEN);
10698 processPosition(mapper, 100, 200);
10699 processPressure(mapper, RAW_PRESSURE_MAX);
10700 processSync(mapper);
10701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10702 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10703 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
10704 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10705
10706 processId(mapper, INVALID_TRACKING_ID);
10707 processSync(mapper);
10708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10709 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10710 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
10711 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10712
10713 // The mapper should still report only a touchscreen source.
10714 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10715}
10716
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010717// --- MultiTouchInputMapperTest_ExternalDevice ---
10718
10719class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10720protected:
Chris Yea52ade12020-08-27 16:49:20 -070010721 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010722};
10723
10724/**
10725 * Expect fallback to internal viewport if device is external and external viewport is not present.
10726 */
10727TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10728 prepareAxes(POSITION);
10729 addConfigurationProperty("touch.deviceType", "touchScreen");
10730 prepareDisplay(DISPLAY_ORIENTATION_0);
10731 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10732
10733 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10734
10735 NotifyMotionArgs motionArgs;
10736
10737 // Expect the event to be sent to the internal viewport,
10738 // because an external viewport is not present.
10739 processPosition(mapper, 100, 100);
10740 processSync(mapper);
10741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10742 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10743
10744 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010745 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010746 processPosition(mapper, 100, 100);
10747 processSync(mapper);
10748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10749 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10750}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010751
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010752TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10753 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10754 std::shared_ptr<FakePointerController> fakePointerController =
10755 std::make_shared<FakePointerController>();
10756 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10757 fakePointerController->setPosition(0, 0);
10758 fakePointerController->setButtonState(0);
10759
10760 // prepare device and capture
10761 prepareDisplay(DISPLAY_ORIENTATION_0);
10762 prepareAxes(POSITION | ID | SLOT);
10763 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10764 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10765 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010766 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010767 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10768
10769 // captured touchpad should be a touchpad source
10770 NotifyDeviceResetArgs resetArgs;
10771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10772 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10773
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010774 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010775
10776 const InputDeviceInfo::MotionRange* relRangeX =
10777 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10778 ASSERT_NE(relRangeX, nullptr);
10779 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10780 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10781 const InputDeviceInfo::MotionRange* relRangeY =
10782 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10783 ASSERT_NE(relRangeY, nullptr);
10784 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10785 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10786
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010787 // run captured pointer tests - note that this is unscaled, so input listener events should be
10788 // identical to what the hardware sends (accounting for any
10789 // calibration).
10790 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010791 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010792 processId(mapper, 1);
10793 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10794 processKey(mapper, BTN_TOUCH, 1);
10795 processSync(mapper);
10796
10797 // expect coord[0] to contain initial location of touch 0
10798 NotifyMotionArgs args;
10799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10800 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10801 ASSERT_EQ(1U, args.pointerCount);
10802 ASSERT_EQ(0, args.pointerProperties[0].id);
10803 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10804 ASSERT_NO_FATAL_FAILURE(
10805 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10806
10807 // FINGER 1 DOWN
10808 processSlot(mapper, 1);
10809 processId(mapper, 2);
10810 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10811 processSync(mapper);
10812
10813 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010815 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010816 ASSERT_EQ(2U, args.pointerCount);
10817 ASSERT_EQ(0, args.pointerProperties[0].id);
10818 ASSERT_EQ(1, args.pointerProperties[1].id);
10819 ASSERT_NO_FATAL_FAILURE(
10820 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10821 ASSERT_NO_FATAL_FAILURE(
10822 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10823
10824 // FINGER 1 MOVE
10825 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10826 processSync(mapper);
10827
10828 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10829 // from move
10830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10832 ASSERT_NO_FATAL_FAILURE(
10833 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10834 ASSERT_NO_FATAL_FAILURE(
10835 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10836
10837 // FINGER 0 MOVE
10838 processSlot(mapper, 0);
10839 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10840 processSync(mapper);
10841
10842 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10844 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10845 ASSERT_NO_FATAL_FAILURE(
10846 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10847 ASSERT_NO_FATAL_FAILURE(
10848 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10849
10850 // BUTTON DOWN
10851 processKey(mapper, BTN_LEFT, 1);
10852 processSync(mapper);
10853
10854 // touchinputmapper design sends a move before button press
10855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10856 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10858 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10859
10860 // BUTTON UP
10861 processKey(mapper, BTN_LEFT, 0);
10862 processSync(mapper);
10863
10864 // touchinputmapper design sends a move after button release
10865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10866 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10868 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10869
10870 // FINGER 0 UP
10871 processId(mapper, -1);
10872 processSync(mapper);
10873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10874 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10875
10876 // FINGER 1 MOVE
10877 processSlot(mapper, 1);
10878 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10879 processSync(mapper);
10880
10881 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10883 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10884 ASSERT_EQ(1U, args.pointerCount);
10885 ASSERT_EQ(1, args.pointerProperties[0].id);
10886 ASSERT_NO_FATAL_FAILURE(
10887 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10888
10889 // FINGER 1 UP
10890 processId(mapper, -1);
10891 processKey(mapper, BTN_TOUCH, 0);
10892 processSync(mapper);
10893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10894 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10895
Harry Cutts16a24cc2022-10-26 15:22:19 +000010896 // A non captured touchpad should have a mouse and touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010897 mFakePolicy->setPointerCapture(false);
10898 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Harry Cutts16a24cc2022-10-26 15:22:19 +000010900 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010901}
10902
10903TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10904 std::shared_ptr<FakePointerController> fakePointerController =
10905 std::make_shared<FakePointerController>();
10906 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10907 fakePointerController->setPosition(0, 0);
10908 fakePointerController->setButtonState(0);
10909
10910 // prepare device and capture
10911 prepareDisplay(DISPLAY_ORIENTATION_0);
10912 prepareAxes(POSITION | ID | SLOT);
10913 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10914 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010915 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010916 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10917 // run uncaptured pointer tests - pushes out generic events
10918 // FINGER 0 DOWN
10919 processId(mapper, 3);
10920 processPosition(mapper, 100, 100);
10921 processKey(mapper, BTN_TOUCH, 1);
10922 processSync(mapper);
10923
10924 // start at (100,100), cursor should be at (0,0) * scale
10925 NotifyMotionArgs args;
10926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10927 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10928 ASSERT_NO_FATAL_FAILURE(
10929 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10930
10931 // FINGER 0 MOVE
10932 processPosition(mapper, 200, 200);
10933 processSync(mapper);
10934
10935 // compute scaling to help with touch position checking
10936 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10937 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10938 float scale =
10939 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10940
10941 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10943 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10944 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10945 0, 0, 0, 0, 0, 0, 0));
10946}
10947
10948TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10949 std::shared_ptr<FakePointerController> fakePointerController =
10950 std::make_shared<FakePointerController>();
10951
10952 prepareDisplay(DISPLAY_ORIENTATION_0);
10953 prepareAxes(POSITION | ID | SLOT);
10954 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010955 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010956 mFakePolicy->setPointerCapture(false);
10957 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10958
Harry Cutts16a24cc2022-10-26 15:22:19 +000010959 // An uncaptured touchpad should be a pointer device, with additional touchpad source.
10960 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010961
Harry Cutts16a24cc2022-10-26 15:22:19 +000010962 // A captured touchpad should just have a touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010963 mFakePolicy->setPointerCapture(true);
10964 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10965 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10966}
10967
HQ Liue6983c72022-04-19 22:14:56 +000010968class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10969protected:
10970 float mPointerMovementScale;
10971 float mPointerXZoomScale;
10972 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10973 addConfigurationProperty("touch.deviceType", "pointer");
10974 std::shared_ptr<FakePointerController> fakePointerController =
10975 std::make_shared<FakePointerController>();
10976 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10977 fakePointerController->setPosition(0, 0);
10978 fakePointerController->setButtonState(0);
10979 prepareDisplay(DISPLAY_ORIENTATION_0);
10980
10981 prepareAxes(POSITION);
10982 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10983 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10984 // needs to be disabled, and the pointer gesture needs to be enabled.
10985 mFakePolicy->setPointerCapture(false);
10986 mFakePolicy->setPointerGestureEnabled(true);
10987 mFakePolicy->setPointerController(fakePointerController);
10988
10989 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10990 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10991 mPointerMovementScale =
10992 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10993 mPointerXZoomScale =
10994 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10995 }
10996
10997 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10998 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10999 /*flat*/ 0,
11000 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
11001 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
11002 /*flat*/ 0,
11003 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
11004 }
11005};
11006
11007/**
11008 * Two fingers down on a pointer mode touch pad. The width
11009 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
11010 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
11011 * be greater than the both value to be freeform gesture, so that after two
11012 * fingers start to move downwards, the gesture should be swipe.
11013 */
11014TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
11015 // The min freeform gesture width is 25units/mm x 30mm = 750
11016 // which is greater than fraction of the diagnal length of the touchpad (349).
11017 // Thus, MaxSwipWidth is 750.
11018 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
11019 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11020 NotifyMotionArgs motionArgs;
11021
11022 // Two fingers down at once.
11023 // The two fingers are 450 units apart, expects the current gesture to be PRESS
11024 // Pointer's initial position is used the [0,0] coordinate.
11025 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
11026
11027 processId(mapper, FIRST_TRACKING_ID);
11028 processPosition(mapper, x1, y1);
11029 processMTSync(mapper);
11030 processId(mapper, SECOND_TRACKING_ID);
11031 processPosition(mapper, x2, y2);
11032 processMTSync(mapper);
11033 processSync(mapper);
11034
11035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11036 ASSERT_EQ(1U, motionArgs.pointerCount);
11037 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11038 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011039 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011040 ASSERT_NO_FATAL_FAILURE(
11041 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11042
11043 // It should be recognized as a SWIPE gesture when two fingers start to move down,
11044 // that there should be 1 pointer.
11045 int32_t movingDistance = 200;
11046 y1 += movingDistance;
11047 y2 += movingDistance;
11048
11049 processId(mapper, FIRST_TRACKING_ID);
11050 processPosition(mapper, x1, y1);
11051 processMTSync(mapper);
11052 processId(mapper, SECOND_TRACKING_ID);
11053 processPosition(mapper, x2, y2);
11054 processMTSync(mapper);
11055 processSync(mapper);
11056
11057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11058 ASSERT_EQ(1U, motionArgs.pointerCount);
11059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11060 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011061 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
11063 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11064 0, 0, 0, 0));
11065}
11066
11067/**
11068 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
11069 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
11070 * the touch pack diagnal length. Two fingers' distance must be greater than the both
11071 * value to be freeform gesture, so that after two fingers start to move downwards,
11072 * the gesture should be swipe.
11073 */
11074TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
11075 // The min freeform gesture width is 5units/mm x 30mm = 150
11076 // which is greater than fraction of the diagnal length of the touchpad (349).
11077 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
11078 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
11079 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11080 NotifyMotionArgs motionArgs;
11081
11082 // Two fingers down at once.
11083 // The two fingers are 250 units apart, expects the current gesture to be PRESS
11084 // Pointer's initial position is used the [0,0] coordinate.
11085 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
11086
11087 processId(mapper, FIRST_TRACKING_ID);
11088 processPosition(mapper, x1, y1);
11089 processMTSync(mapper);
11090 processId(mapper, SECOND_TRACKING_ID);
11091 processPosition(mapper, x2, y2);
11092 processMTSync(mapper);
11093 processSync(mapper);
11094
11095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11096 ASSERT_EQ(1U, motionArgs.pointerCount);
11097 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11098 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011099 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011100 ASSERT_NO_FATAL_FAILURE(
11101 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11102
11103 // It should be recognized as a SWIPE gesture when two fingers start to move down,
11104 // and there should be 1 pointer.
11105 int32_t movingDistance = 200;
11106 y1 += movingDistance;
11107 y2 += movingDistance;
11108
11109 processId(mapper, FIRST_TRACKING_ID);
11110 processPosition(mapper, x1, y1);
11111 processMTSync(mapper);
11112 processId(mapper, SECOND_TRACKING_ID);
11113 processPosition(mapper, x2, y2);
11114 processMTSync(mapper);
11115 processSync(mapper);
11116
11117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11118 ASSERT_EQ(1U, motionArgs.pointerCount);
11119 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11120 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011121 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011122 // New coordinate is the scaled relative coordinate from the initial coordinate.
11123 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
11124 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11125 0, 0, 0, 0));
11126}
11127
11128/**
11129 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
11130 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
11131 * freeform gestures after two fingers start to move downwards.
11132 */
11133TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
11134 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
11135 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11136
11137 NotifyMotionArgs motionArgs;
11138
11139 // Two fingers down at once. Wider than the max swipe width.
11140 // The gesture is expected to be PRESS, then transformed to FREEFORM
11141 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
11142
11143 processId(mapper, FIRST_TRACKING_ID);
11144 processPosition(mapper, x1, y1);
11145 processMTSync(mapper);
11146 processId(mapper, SECOND_TRACKING_ID);
11147 processPosition(mapper, x2, y2);
11148 processMTSync(mapper);
11149 processSync(mapper);
11150
11151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11152 ASSERT_EQ(1U, motionArgs.pointerCount);
11153 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11154 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011155 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011156 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
11157 ASSERT_NO_FATAL_FAILURE(
11158 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11159
11160 int32_t movingDistance = 200;
11161
11162 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
11163 // then two down events for two pointers.
11164 y1 += movingDistance;
11165 y2 += movingDistance;
11166
11167 processId(mapper, FIRST_TRACKING_ID);
11168 processPosition(mapper, x1, y1);
11169 processMTSync(mapper);
11170 processId(mapper, SECOND_TRACKING_ID);
11171 processPosition(mapper, x2, y2);
11172 processMTSync(mapper);
11173 processSync(mapper);
11174
11175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11176 // The previous PRESS gesture is cancelled, because it is transformed to freeform
11177 ASSERT_EQ(1U, motionArgs.pointerCount);
11178 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
11179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11180 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
11181 ASSERT_EQ(1U, motionArgs.pointerCount);
11182 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11184 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011185 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011186 ASSERT_EQ(2U, motionArgs.pointerCount);
11187 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
11188 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011189 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011190 // Two pointers' scaled relative coordinates from their initial centroid.
11191 // Initial y coordinates are 0 as y1 and y2 have the same value.
11192 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
11193 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
11194 // When pointers move, the new coordinates equal to the initial coordinates plus
11195 // scaled moving distance.
11196 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11197 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11198 0, 0, 0, 0));
11199 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11200 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11201 0, 0, 0, 0));
11202
11203 // Move two fingers down again, expect one MOVE motion event.
11204 y1 += movingDistance;
11205 y2 += movingDistance;
11206
11207 processId(mapper, FIRST_TRACKING_ID);
11208 processPosition(mapper, x1, y1);
11209 processMTSync(mapper);
11210 processId(mapper, SECOND_TRACKING_ID);
11211 processPosition(mapper, x2, y2);
11212 processMTSync(mapper);
11213 processSync(mapper);
11214
11215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11216 ASSERT_EQ(2U, motionArgs.pointerCount);
11217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11218 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011219 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011220 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11221 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11222 0, 0, 0, 0, 0));
11223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11224 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11225 0, 0, 0, 0, 0));
11226}
11227
Harry Cutts39b7ca22022-10-05 15:55:48 +000011228TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
11229 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
11230 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11231 NotifyMotionArgs motionArgs;
11232
11233 // Place two fingers down.
11234 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
11235
11236 processId(mapper, FIRST_TRACKING_ID);
11237 processPosition(mapper, x1, y1);
11238 processMTSync(mapper);
11239 processId(mapper, SECOND_TRACKING_ID);
11240 processPosition(mapper, x2, y2);
11241 processMTSync(mapper);
11242 processSync(mapper);
11243
11244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11245 ASSERT_EQ(1U, motionArgs.pointerCount);
11246 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11247 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
11248 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
11249 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
11250
11251 // Move the two fingers down and to the left.
11252 int32_t movingDistance = 200;
11253 x1 -= movingDistance;
11254 y1 += movingDistance;
11255 x2 -= movingDistance;
11256 y2 += movingDistance;
11257
11258 processId(mapper, FIRST_TRACKING_ID);
11259 processPosition(mapper, x1, y1);
11260 processMTSync(mapper);
11261 processId(mapper, SECOND_TRACKING_ID);
11262 processPosition(mapper, x2, y2);
11263 processMTSync(mapper);
11264 processSync(mapper);
11265
11266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11267 ASSERT_EQ(1U, motionArgs.pointerCount);
11268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11269 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
11270 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
11271 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
11272}
11273
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011274TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
11275 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
11276 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
11277 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
11279
11280 // Start a stylus gesture.
11281 processKey(mapper, BTN_TOOL_PEN, 1);
11282 processId(mapper, FIRST_TRACKING_ID);
11283 processPosition(mapper, 100, 200);
11284 processSync(mapper);
11285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11286 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
11287 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11288 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11289 // TODO(b/257078296): Pointer mode generates extra event.
11290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11291 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11292 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11293 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11295
11296 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
11297 // gesture should be disabled.
11298 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
11299 viewport->isActive = false;
11300 mFakePolicy->updateViewport(*viewport);
11301 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
11302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11303 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11304 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11305 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11306 // TODO(b/257078296): Pointer mode generates extra event.
11307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11308 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11309 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11310 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11312}
11313
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011314// --- JoystickInputMapperTest ---
11315
11316class JoystickInputMapperTest : public InputMapperTest {
11317protected:
11318 static const int32_t RAW_X_MIN;
11319 static const int32_t RAW_X_MAX;
11320 static const int32_t RAW_Y_MIN;
11321 static const int32_t RAW_Y_MAX;
11322
11323 void SetUp() override {
11324 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
11325 }
11326 void prepareAxes() {
11327 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
11328 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
11329 }
11330
11331 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
11332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
11333 }
11334
11335 void processSync(JoystickInputMapper& mapper) {
11336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
11337 }
11338
11339 void prepareVirtualDisplay(int32_t orientation) {
11340 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
11341 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
11342 NO_PORT, ViewportType::VIRTUAL);
11343 }
11344};
11345
11346const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
11347const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
11348const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
11349const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
11350
11351TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
11352 prepareAxes();
11353 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
11354
11355 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
11356
11357 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
11358
11359 // Send an axis event
11360 processAxis(mapper, ABS_X, 100);
11361 processSync(mapper);
11362
11363 NotifyMotionArgs args;
11364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11365 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11366
11367 // Send another axis event
11368 processAxis(mapper, ABS_Y, 100);
11369 processSync(mapper);
11370
11371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11372 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11373}
11374
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011375// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011376
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011377class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011378protected:
11379 static const char* DEVICE_NAME;
11380 static const char* DEVICE_LOCATION;
11381 static const int32_t DEVICE_ID;
11382 static const int32_t DEVICE_GENERATION;
11383 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011384 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011385 static const int32_t EVENTHUB_ID;
11386
11387 std::shared_ptr<FakeEventHub> mFakeEventHub;
11388 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011389 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011390 std::unique_ptr<InstrumentedInputReader> mReader;
11391 std::shared_ptr<InputDevice> mDevice;
11392
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011393 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011394 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011395 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011396 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011397 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011398 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011399 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11400 }
11401
11402 void SetUp() override { SetUp(DEVICE_CLASSES); }
11403
11404 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011405 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011406 mFakePolicy.clear();
11407 }
11408
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070011409 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011410 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
11411 mReader->requestRefreshConfiguration(changes);
11412 mReader->loopOnce();
11413 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070011414 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011415 }
11416
11417 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11418 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011419 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011420 InputDeviceIdentifier identifier;
11421 identifier.name = name;
11422 identifier.location = location;
11423 std::shared_ptr<InputDevice> device =
11424 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11425 identifier);
11426 mReader->pushNextDevice(device);
11427 mFakeEventHub->addDevice(eventHubId, name, classes);
11428 mReader->loopOnce();
11429 return device;
11430 }
11431
11432 template <class T, typename... Args>
11433 T& addControllerAndConfigure(Args... args) {
11434 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11435
11436 return controller;
11437 }
11438};
11439
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011440const char* PeripheralControllerTest::DEVICE_NAME = "device";
11441const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11442const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11443const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11444const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011445const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11446 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011447const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011448
11449// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011450class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011451protected:
11452 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011453 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011454 }
11455};
11456
11457TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011458 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011459
11460 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
11461 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
11462}
11463
11464TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011465 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011466
11467 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
11468 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
11469}
11470
11471// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011472class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011473protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011474 void SetUp() override {
11475 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11476 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011477};
11478
Chris Ye85758332021-05-16 23:05:17 -070011479TEST_F(LightControllerTest, MonoLight) {
11480 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011481 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011482 .maxBrightness = 255,
11483 .flags = InputLightClass::BRIGHTNESS,
11484 .path = ""};
11485 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011486
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011487 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011488 InputDeviceInfo info;
11489 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011490 std::vector<InputDeviceLightInfo> lights = info.getLights();
11491 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011492 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11493 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11494
11495 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11496 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11497}
11498
11499TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11500 RawLightInfo infoMono = {.id = 1,
11501 .name = "mono_keyboard_backlight",
11502 .maxBrightness = 255,
11503 .flags = InputLightClass::BRIGHTNESS |
11504 InputLightClass::KEYBOARD_BACKLIGHT,
11505 .path = ""};
11506 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11507
11508 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11509 InputDeviceInfo info;
11510 controller.populateDeviceInfo(&info);
11511 std::vector<InputDeviceLightInfo> lights = info.getLights();
11512 ASSERT_EQ(1U, lights.size());
11513 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11514 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011515
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011516 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11517 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011518}
11519
11520TEST_F(LightControllerTest, RGBLight) {
11521 RawLightInfo infoRed = {.id = 1,
11522 .name = "red",
11523 .maxBrightness = 255,
11524 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11525 .path = ""};
11526 RawLightInfo infoGreen = {.id = 2,
11527 .name = "green",
11528 .maxBrightness = 255,
11529 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11530 .path = ""};
11531 RawLightInfo infoBlue = {.id = 3,
11532 .name = "blue",
11533 .maxBrightness = 255,
11534 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11535 .path = ""};
11536 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11537 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11538 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11539
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011540 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011541 InputDeviceInfo info;
11542 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011543 std::vector<InputDeviceLightInfo> lights = info.getLights();
11544 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011545 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11546 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11547 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11548
11549 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11550 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11551}
11552
11553TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11554 RawLightInfo infoRed = {.id = 1,
11555 .name = "red_keyboard_backlight",
11556 .maxBrightness = 255,
11557 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11558 InputLightClass::KEYBOARD_BACKLIGHT,
11559 .path = ""};
11560 RawLightInfo infoGreen = {.id = 2,
11561 .name = "green_keyboard_backlight",
11562 .maxBrightness = 255,
11563 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11564 InputLightClass::KEYBOARD_BACKLIGHT,
11565 .path = ""};
11566 RawLightInfo infoBlue = {.id = 3,
11567 .name = "blue_keyboard_backlight",
11568 .maxBrightness = 255,
11569 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11570 InputLightClass::KEYBOARD_BACKLIGHT,
11571 .path = ""};
11572 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11573 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11574 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11575
11576 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11577 InputDeviceInfo info;
11578 controller.populateDeviceInfo(&info);
11579 std::vector<InputDeviceLightInfo> lights = info.getLights();
11580 ASSERT_EQ(1U, lights.size());
11581 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11582 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11583 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11584
11585 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11586 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11587}
11588
11589TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11590 RawLightInfo infoRed = {.id = 1,
11591 .name = "red",
11592 .maxBrightness = 255,
11593 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11594 .path = ""};
11595 RawLightInfo infoGreen = {.id = 2,
11596 .name = "green",
11597 .maxBrightness = 255,
11598 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11599 .path = ""};
11600 RawLightInfo infoBlue = {.id = 3,
11601 .name = "blue",
11602 .maxBrightness = 255,
11603 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11604 .path = ""};
11605 RawLightInfo infoGlobal = {.id = 3,
11606 .name = "global_keyboard_backlight",
11607 .maxBrightness = 255,
11608 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11609 InputLightClass::KEYBOARD_BACKLIGHT,
11610 .path = ""};
11611 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11612 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11613 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11614 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11615
11616 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11617 InputDeviceInfo info;
11618 controller.populateDeviceInfo(&info);
11619 std::vector<InputDeviceLightInfo> lights = info.getLights();
11620 ASSERT_EQ(1U, lights.size());
11621 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11622 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11623 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011624
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011625 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11626 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011627}
11628
11629TEST_F(LightControllerTest, MultiColorRGBLight) {
11630 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011631 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011632 .maxBrightness = 255,
11633 .flags = InputLightClass::BRIGHTNESS |
11634 InputLightClass::MULTI_INTENSITY |
11635 InputLightClass::MULTI_INDEX,
11636 .path = ""};
11637
11638 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11639
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011640 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011641 InputDeviceInfo info;
11642 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011643 std::vector<InputDeviceLightInfo> lights = info.getLights();
11644 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011645 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11646 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11647 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11648
11649 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11650 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11651}
11652
11653TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11654 RawLightInfo infoColor = {.id = 1,
11655 .name = "multi_color_keyboard_backlight",
11656 .maxBrightness = 255,
11657 .flags = InputLightClass::BRIGHTNESS |
11658 InputLightClass::MULTI_INTENSITY |
11659 InputLightClass::MULTI_INDEX |
11660 InputLightClass::KEYBOARD_BACKLIGHT,
11661 .path = ""};
11662
11663 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11664
11665 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11666 InputDeviceInfo info;
11667 controller.populateDeviceInfo(&info);
11668 std::vector<InputDeviceLightInfo> lights = info.getLights();
11669 ASSERT_EQ(1U, lights.size());
11670 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11671 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11672 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011673
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011674 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11675 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011676}
11677
11678TEST_F(LightControllerTest, PlayerIdLight) {
11679 RawLightInfo info1 = {.id = 1,
11680 .name = "player1",
11681 .maxBrightness = 255,
11682 .flags = InputLightClass::BRIGHTNESS,
11683 .path = ""};
11684 RawLightInfo info2 = {.id = 2,
11685 .name = "player2",
11686 .maxBrightness = 255,
11687 .flags = InputLightClass::BRIGHTNESS,
11688 .path = ""};
11689 RawLightInfo info3 = {.id = 3,
11690 .name = "player3",
11691 .maxBrightness = 255,
11692 .flags = InputLightClass::BRIGHTNESS,
11693 .path = ""};
11694 RawLightInfo info4 = {.id = 4,
11695 .name = "player4",
11696 .maxBrightness = 255,
11697 .flags = InputLightClass::BRIGHTNESS,
11698 .path = ""};
11699 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11700 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11701 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11702 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11703
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011704 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011705 InputDeviceInfo info;
11706 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011707 std::vector<InputDeviceLightInfo> lights = info.getLights();
11708 ASSERT_EQ(1U, lights.size());
11709 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011710 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11711 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011712
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011713 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11714 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11715 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011716}
11717
Michael Wrightd02c5b62014-02-10 15:10:22 -080011718} // namespace android