blob: 3ce03f34c137833e5c4e7c9db4942a5b47401fa6 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
19
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <CursorInputMapper.h>
21#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080024#include <InputReaderBase.h>
25#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000026#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070029#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070030#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
32#include <SwitchInputMapper.h>
33#include <TestInputListener.h>
Prabir Pradhan739dca42022-09-09 20:12:01 +000034#include <TestInputListenerMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070035#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080036#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000037#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070038#include <android-base/thread_annotations.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080039#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050040#include <gui/constants.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080041
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070042#include <thread>
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000043#include "android/hardware/input/InputDeviceCountryCode.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000044#include "input/DisplayViewport.h"
45#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010046
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000047using android::hardware::input::InputDeviceCountryCode;
48
Michael Wrightd02c5b62014-02-10 15:10:22 -080049namespace android {
50
Dominik Laskowski2f01d772022-03-23 16:01:29 -070051using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000052using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070053using std::chrono_literals::operator""ms;
54
55// Timeout for waiting for an expected event
56static constexpr std::chrono::duration WAIT_TIMEOUT = 100ms;
57
Michael Wrightd02c5b62014-02-10 15:10:22 -080058// An arbitrary time value.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000059static constexpr nsecs_t ARBITRARY_TIME = 1234;
60static constexpr nsecs_t READ_TIME = 4321;
Michael Wrightd02c5b62014-02-10 15:10:22 -080061
62// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080063static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000064static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080065static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000066static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080067static constexpr int32_t DISPLAY_WIDTH = 480;
68static constexpr int32_t DISPLAY_HEIGHT = 800;
69static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
70static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
71static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070072static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070073static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080074
arthurhungcc7f9802020-04-30 17:55:40 +080075static constexpr int32_t FIRST_SLOT = 0;
76static constexpr int32_t SECOND_SLOT = 1;
77static constexpr int32_t THIRD_SLOT = 2;
78static constexpr int32_t INVALID_TRACKING_ID = -1;
79static constexpr int32_t FIRST_TRACKING_ID = 0;
80static constexpr int32_t SECOND_TRACKING_ID = 1;
81static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Yee2b1e5c2021-03-10 22:45:12 -080082static constexpr int32_t DEFAULT_BATTERY = 1;
Kim Low03ea0352020-11-06 12:45:07 -080083static constexpr int32_t BATTERY_STATUS = 4;
84static constexpr int32_t BATTERY_CAPACITY = 66;
Prabir Pradhane287ecd2022-09-07 21:18:05 +000085static const std::string BATTERY_DEVPATH = "/sys/devices/mydevice/power_supply/mybattery";
Chris Ye3fdbfef2021-01-06 18:45:18 -080086static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
87static constexpr int32_t LIGHT_COLOR = 0x7F448866;
88static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080089
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080090static constexpr int32_t ACTION_POINTER_0_DOWN =
91 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
92static constexpr int32_t ACTION_POINTER_0_UP =
93 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
94static constexpr int32_t ACTION_POINTER_1_DOWN =
95 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
96static constexpr int32_t ACTION_POINTER_1_UP =
97 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
98
Michael Wrightd02c5b62014-02-10 15:10:22 -080099// Error tolerance for floating point assertions.
100static const float EPSILON = 0.001f;
101
102template<typename T>
103static inline T min(T a, T b) {
104 return a < b ? a : b;
105}
106
107static inline float avg(float x, float y) {
108 return (x + y) / 2;
109}
110
Chris Ye3fdbfef2021-01-06 18:45:18 -0800111// Mapping for light color name and the light color
112const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
113 {"green", LightColor::GREEN},
114 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800115
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700116static int32_t getInverseRotation(int32_t orientation) {
117 switch (orientation) {
118 case DISPLAY_ORIENTATION_90:
119 return DISPLAY_ORIENTATION_270;
120 case DISPLAY_ORIENTATION_270:
121 return DISPLAY_ORIENTATION_90;
122 default:
123 return orientation;
124 }
125}
126
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800127static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
128 InputDeviceInfo info;
129 mapper.populateDeviceInfo(&info);
130
131 const InputDeviceInfo::MotionRange* motionRange =
132 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
133 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
134}
135
136static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
137 InputDeviceInfo info;
138 mapper.populateDeviceInfo(&info);
139
140 const InputDeviceInfo::MotionRange* motionRange =
141 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
142 ASSERT_EQ(nullptr, motionRange);
143}
144
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700145[[maybe_unused]] static void dumpReader(InputReader& reader) {
146 std::string dump;
147 reader.dump(dump);
148 std::istringstream iss(dump);
149 for (std::string line; std::getline(iss, line);) {
150 ALOGE("%s", line.c_str());
151 std::this_thread::sleep_for(std::chrono::milliseconds(1));
152 }
153}
154
Michael Wrightd02c5b62014-02-10 15:10:22 -0800155// --- FakePointerController ---
156
157class FakePointerController : public PointerControllerInterface {
158 bool mHaveBounds;
159 float mMinX, mMinY, mMaxX, mMaxY;
160 float mX, mY;
161 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800162 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163
Michael Wrightd02c5b62014-02-10 15:10:22 -0800164public:
165 FakePointerController() :
166 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800167 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168 }
169
Michael Wright17db18e2020-06-26 20:51:44 +0100170 virtual ~FakePointerController() {}
171
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172 void setBounds(float minX, float minY, float maxX, float maxY) {
173 mHaveBounds = true;
174 mMinX = minX;
175 mMinY = minY;
176 mMaxX = maxX;
177 mMaxY = maxY;
178 }
179
Chris Yea52ade12020-08-27 16:49:20 -0700180 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181 mX = x;
182 mY = y;
183 }
184
Chris Yea52ade12020-08-27 16:49:20 -0700185 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186
Chris Yea52ade12020-08-27 16:49:20 -0700187 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188
Chris Yea52ade12020-08-27 16:49:20 -0700189 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190 *outX = mX;
191 *outY = mY;
192 }
193
Chris Yea52ade12020-08-27 16:49:20 -0700194 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800195
Chris Yea52ade12020-08-27 16:49:20 -0700196 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800197 mDisplayId = viewport.displayId;
198 }
199
Arthur Hung7c645402019-01-25 17:45:42 +0800200 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
201 return mSpotsByDisplay;
202 }
203
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204private:
Chris Yea52ade12020-08-27 16:49:20 -0700205 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800206 *outMinX = mMinX;
207 *outMinY = mMinY;
208 *outMaxX = mMaxX;
209 *outMaxY = mMaxY;
210 return mHaveBounds;
211 }
212
Chris Yea52ade12020-08-27 16:49:20 -0700213 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800214 mX += deltaX;
215 if (mX < mMinX) mX = mMinX;
216 if (mX > mMaxX) mX = mMaxX;
217 mY += deltaY;
218 if (mY < mMinY) mY = mMinY;
219 if (mY > mMaxY) mY = mMaxY;
220 }
221
Chris Yea52ade12020-08-27 16:49:20 -0700222 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800223
Chris Yea52ade12020-08-27 16:49:20 -0700224 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800225
Chris Yea52ade12020-08-27 16:49:20 -0700226 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800227
Chris Yea52ade12020-08-27 16:49:20 -0700228 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
229 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800230 std::vector<int32_t> newSpots;
231 // Add spots for fingers that are down.
232 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
233 uint32_t id = idBits.clearFirstMarkedBit();
234 newSpots.push_back(id);
235 }
236
237 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800238 }
239
Prabir Pradhan197e0862022-07-01 14:28:00 +0000240 void clearSpots() override { mSpotsByDisplay.clear(); }
Arthur Hung7c645402019-01-25 17:45:42 +0800241
242 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243};
244
245
246// --- FakeInputReaderPolicy ---
247
248class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700249 std::mutex mLock;
250 std::condition_variable mDevicesChangedCondition;
251
Michael Wrightd02c5b62014-02-10 15:10:22 -0800252 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000253 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700254 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
255 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100256 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700257 TouchAffineTransformation transform;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000258 std::optional<int32_t /*deviceId*/> mStylusGestureNotified GUARDED_BY(mLock){};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259
260protected:
Chris Yea52ade12020-08-27 16:49:20 -0700261 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800262
263public:
264 FakeInputReaderPolicy() {
265 }
266
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700267 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800268 waitForInputDevices([](bool devicesChanged) {
269 if (!devicesChanged) {
270 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
271 }
272 });
273 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700274
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800275 void assertInputDevicesNotChanged() {
276 waitForInputDevices([](bool devicesChanged) {
277 if (devicesChanged) {
278 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
279 }
280 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700281 }
282
Prabir Pradhanda20b172022-09-26 17:01:18 +0000283 void assertStylusGestureNotified(int32_t deviceId) {
284 std::scoped_lock lock(mLock);
285 ASSERT_TRUE(mStylusGestureNotified);
286 ASSERT_EQ(deviceId, *mStylusGestureNotified);
287 mStylusGestureNotified.reset();
288 }
289
290 void assertStylusGestureNotNotified() {
291 std::scoped_lock lock(mLock);
292 ASSERT_FALSE(mStylusGestureNotified);
293 }
294
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700295 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100296 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100297 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700298 }
299
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700300 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
301 return mConfig.getDisplayViewportByUniqueId(uniqueId);
302 }
303 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
304 return mConfig.getDisplayViewportByType(type);
305 }
306
307 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
308 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700309 }
310
Prabir Pradhan5632d622021-09-06 07:57:20 -0700311 void addDisplayViewport(DisplayViewport viewport) {
312 mViewports.push_back(std::move(viewport));
313 mConfig.setDisplayViewports(mViewports);
314 }
315
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700316 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000317 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700318 std::optional<uint8_t> physicalPort, ViewportType type) {
319 const bool isRotated =
320 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
321 DisplayViewport v;
322 v.displayId = displayId;
323 v.orientation = orientation;
324 v.logicalLeft = 0;
325 v.logicalTop = 0;
326 v.logicalRight = isRotated ? height : width;
327 v.logicalBottom = isRotated ? width : height;
328 v.physicalLeft = 0;
329 v.physicalTop = 0;
330 v.physicalRight = isRotated ? height : width;
331 v.physicalBottom = isRotated ? width : height;
332 v.deviceWidth = isRotated ? height : width;
333 v.deviceHeight = isRotated ? width : height;
334 v.isActive = isActive;
335 v.uniqueId = uniqueId;
336 v.physicalPort = physicalPort;
337 v.type = type;
338
339 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800340 }
341
Arthur Hung6cd19a42019-08-30 19:04:12 +0800342 bool updateViewport(const DisplayViewport& viewport) {
343 size_t count = mViewports.size();
344 for (size_t i = 0; i < count; i++) {
345 const DisplayViewport& currentViewport = mViewports[i];
346 if (currentViewport.displayId == viewport.displayId) {
347 mViewports[i] = viewport;
348 mConfig.setDisplayViewports(mViewports);
349 return true;
350 }
351 }
352 // no viewport found.
353 return false;
354 }
355
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100356 void addExcludedDeviceName(const std::string& deviceName) {
357 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800358 }
359
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700360 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
361 mConfig.portAssociations.insert({inputPort, displayPort});
362 }
363
Christine Franks1ba71cc2021-04-07 14:37:42 -0700364 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
365 const std::string& displayUniqueId) {
366 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
367 }
368
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000369 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700370
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000371 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700372
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000373 void setPointerController(std::shared_ptr<FakePointerController> controller) {
374 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800375 }
376
377 const InputReaderConfiguration* getReaderConfiguration() const {
378 return &mConfig;
379 }
380
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800381 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800382 return mInputDevices;
383 }
384
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100385 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700386 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700387 return transform;
388 }
389
390 void setTouchAffineTransformation(const TouchAffineTransformation t) {
391 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800392 }
393
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000394 PointerCaptureRequest setPointerCapture(bool enabled) {
395 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
396 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800397 }
398
Arthur Hung7c645402019-01-25 17:45:42 +0800399 void setShowTouches(bool enabled) {
400 mConfig.showTouches = enabled;
401 }
402
Garfield Tan888a6a42020-01-09 11:39:16 -0800403 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
404 mConfig.defaultPointerDisplayId = pointerDisplayId;
405 }
406
HQ Liue6983c72022-04-19 22:14:56 +0000407 void setPointerGestureEnabled(bool enabled) { mConfig.pointerGesturesEnabled = enabled; }
408
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800409 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
410
HQ Liue6983c72022-04-19 22:14:56 +0000411 float getPointerGestureZoomSpeedRatio() { return mConfig.pointerGestureZoomSpeedRatio; }
412
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000413 void setVelocityControlParams(const VelocityControlParameters& params) {
414 mConfig.pointerVelocityControlParameters = params;
415 mConfig.wheelVelocityControlParameters = params;
416 }
417
Michael Wrightd02c5b62014-02-10 15:10:22 -0800418private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000419 uint32_t mNextPointerCaptureSequenceNumber = 0;
420
Chris Yea52ade12020-08-27 16:49:20 -0700421 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800422 *outConfig = mConfig;
423 }
424
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000425 std::shared_ptr<PointerControllerInterface> obtainPointerController(
426 int32_t /*deviceId*/) override {
427 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800428 }
429
Chris Yea52ade12020-08-27 16:49:20 -0700430 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700431 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800432 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700433 mInputDevicesChanged = true;
434 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800435 }
436
Chris Yea52ade12020-08-27 16:49:20 -0700437 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
438 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700439 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800440 }
441
Chris Yea52ade12020-08-27 16:49:20 -0700442 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800443
444 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
445 std::unique_lock<std::mutex> lock(mLock);
446 base::ScopedLockAssertion assumeLocked(mLock);
447
448 const bool devicesChanged =
449 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
450 return mInputDevicesChanged;
451 });
452 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
453 mInputDevicesChanged = false;
454 }
Prabir Pradhanda20b172022-09-26 17:01:18 +0000455
456 void notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) override {
457 std::scoped_lock<std::mutex> lock(mLock);
458 mStylusGestureNotified = deviceId;
459 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800460};
461
Michael Wrightd02c5b62014-02-10 15:10:22 -0800462// --- FakeEventHub ---
463
464class FakeEventHub : public EventHubInterface {
465 struct KeyInfo {
466 int32_t keyCode;
467 uint32_t flags;
468 };
469
Chris Yef59a2f42020-10-16 12:55:26 -0700470 struct SensorInfo {
471 InputDeviceSensorType sensorType;
472 int32_t sensorDataIndex;
473 };
474
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475 struct Device {
476 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700477 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800478 PropertyMap configuration;
479 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
480 KeyedVector<int, bool> relativeAxes;
481 KeyedVector<int32_t, int32_t> keyCodeStates;
482 KeyedVector<int32_t, int32_t> scanCodeStates;
483 KeyedVector<int32_t, int32_t> switchStates;
484 KeyedVector<int32_t, int32_t> absoluteAxisValue;
485 KeyedVector<int32_t, KeyInfo> keysByScanCode;
486 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
487 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100488 // fake mapping which would normally come from keyCharacterMap
489 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700490 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
491 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800492 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700493 bool enabled;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000494 InputDeviceCountryCode countryCode;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700495
496 status_t enable() {
497 enabled = true;
498 return OK;
499 }
500
501 status_t disable() {
502 enabled = false;
503 return OK;
504 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800505
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700506 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800507 };
508
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700509 std::mutex mLock;
510 std::condition_variable mEventsCondition;
511
Michael Wrightd02c5b62014-02-10 15:10:22 -0800512 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100513 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000514 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600515 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000516 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800517 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
518 // Simulates a device light brightness, from light id to light brightness.
519 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
520 // Simulates a device light intensities, from light id to light intensities map.
521 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
522 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800523
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700524public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800525 virtual ~FakeEventHub() {
526 for (size_t i = 0; i < mDevices.size(); i++) {
527 delete mDevices.valueAt(i);
528 }
529 }
530
Michael Wrightd02c5b62014-02-10 15:10:22 -0800531 FakeEventHub() { }
532
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700533 void addDevice(int32_t deviceId, const std::string& name,
534 ftl::Flags<InputDeviceClass> classes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800535 Device* device = new Device(classes);
536 device->identifier.name = name;
537 mDevices.add(deviceId, device);
538
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000539 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800540 }
541
542 void removeDevice(int32_t deviceId) {
543 delete mDevices.valueFor(deviceId);
544 mDevices.removeItem(deviceId);
545
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000546 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800547 }
548
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000549 bool isDeviceEnabled(int32_t deviceId) const override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700550 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700551 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700552 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
553 return false;
554 }
555 return device->enabled;
556 }
557
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000558 status_t enableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700559 status_t result;
560 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700561 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700562 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
563 return BAD_VALUE;
564 }
565 if (device->enabled) {
566 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
567 return OK;
568 }
569 result = device->enable();
570 return result;
571 }
572
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000573 status_t disableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700574 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700575 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700576 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
577 return BAD_VALUE;
578 }
579 if (!device->enabled) {
580 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
581 return OK;
582 }
583 return device->disable();
584 }
585
Michael Wrightd02c5b62014-02-10 15:10:22 -0800586 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000587 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588 }
589
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700590 void addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800591 Device* device = getDevice(deviceId);
592 device->configuration.addProperty(key, value);
593 }
594
595 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
596 Device* device = getDevice(deviceId);
597 device->configuration.addAll(configuration);
598 }
599
600 void addAbsoluteAxis(int32_t deviceId, int axis,
601 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
602 Device* device = getDevice(deviceId);
603
604 RawAbsoluteAxisInfo info;
605 info.valid = true;
606 info.minValue = minValue;
607 info.maxValue = maxValue;
608 info.flat = flat;
609 info.fuzz = fuzz;
610 info.resolution = resolution;
611 device->absoluteAxes.add(axis, info);
612 }
613
614 void addRelativeAxis(int32_t deviceId, int32_t axis) {
615 Device* device = getDevice(deviceId);
616 device->relativeAxes.add(axis, true);
617 }
618
619 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
620 Device* device = getDevice(deviceId);
621 device->keyCodeStates.replaceValueFor(keyCode, state);
622 }
623
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000624 void setCountryCode(int32_t deviceId, InputDeviceCountryCode countryCode) {
625 Device* device = getDevice(deviceId);
626 device->countryCode = countryCode;
627 }
628
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
630 Device* device = getDevice(deviceId);
631 device->scanCodeStates.replaceValueFor(scanCode, state);
632 }
633
634 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
635 Device* device = getDevice(deviceId);
636 device->switchStates.replaceValueFor(switchCode, state);
637 }
638
639 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
640 Device* device = getDevice(deviceId);
641 device->absoluteAxisValue.replaceValueFor(axis, value);
642 }
643
644 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
645 int32_t keyCode, uint32_t flags) {
646 Device* device = getDevice(deviceId);
647 KeyInfo info;
648 info.keyCode = keyCode;
649 info.flags = flags;
650 if (scanCode) {
651 device->keysByScanCode.add(scanCode, info);
652 }
653 if (usageCode) {
654 device->keysByUsageCode.add(usageCode, info);
655 }
656 }
657
Philip Junker4af3b3d2021-12-14 10:36:55 +0100658 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
659 Device* device = getDevice(deviceId);
660 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
661 }
662
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 void addLed(int32_t deviceId, int32_t led, bool initialState) {
664 Device* device = getDevice(deviceId);
665 device->leds.add(led, initialState);
666 }
667
Chris Yef59a2f42020-10-16 12:55:26 -0700668 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
669 int32_t sensorDataIndex) {
670 Device* device = getDevice(deviceId);
671 SensorInfo info;
672 info.sensorType = sensorType;
673 info.sensorDataIndex = sensorDataIndex;
674 device->sensorsByAbsCode.emplace(absCode, info);
675 }
676
677 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
678 Device* device = getDevice(deviceId);
679 typename BitArray<MSC_MAX>::Buffer buffer;
680 buffer[mscEvent / 32] = 1 << mscEvent % 32;
681 device->mscBitmask.loadFromBuffer(buffer);
682 }
683
Chris Ye3fdbfef2021-01-06 18:45:18 -0800684 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
685 mRawLightInfos.emplace(rawId, std::move(info));
686 }
687
688 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
689 mLightBrightness.emplace(rawId, brightness);
690 }
691
692 void fakeLightIntensities(int32_t rawId,
693 const std::unordered_map<LightColor, int32_t> intensities) {
694 mLightIntensities.emplace(rawId, std::move(intensities));
695 }
696
Michael Wrightd02c5b62014-02-10 15:10:22 -0800697 bool getLedState(int32_t deviceId, int32_t led) {
698 Device* device = getDevice(deviceId);
699 return device->leds.valueFor(led);
700 }
701
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100702 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703 return mExcludedDevices;
704 }
705
706 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
707 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800708 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800709 }
710
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000711 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
712 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700713 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800714 RawEvent event;
715 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000716 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800717 event.deviceId = deviceId;
718 event.type = type;
719 event.code = code;
720 event.value = value;
721 mEvents.push_back(event);
722
723 if (type == EV_ABS) {
724 setAbsoluteAxisValue(deviceId, code, value);
725 }
726 }
727
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600728 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
729 std::vector<TouchVideoFrame>> videoFrames) {
730 mVideoFrames = std::move(videoFrames);
731 }
732
Michael Wrightd02c5b62014-02-10 15:10:22 -0800733 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700734 std::unique_lock<std::mutex> lock(mLock);
735 base::ScopedLockAssertion assumeLocked(mLock);
736 const bool queueIsEmpty =
737 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
738 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
739 if (!queueIsEmpty) {
740 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
741 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800742 }
743
744private:
745 Device* getDevice(int32_t deviceId) const {
746 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100747 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800748 }
749
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700750 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800751 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700752 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800753 }
754
Chris Yea52ade12020-08-27 16:49:20 -0700755 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800756 Device* device = getDevice(deviceId);
757 return device ? device->identifier : InputDeviceIdentifier();
758 }
759
Chris Yea52ade12020-08-27 16:49:20 -0700760 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800761
Chris Yea52ade12020-08-27 16:49:20 -0700762 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800763 Device* device = getDevice(deviceId);
764 if (device) {
765 *outConfiguration = device->configuration;
766 }
767 }
768
Chris Yea52ade12020-08-27 16:49:20 -0700769 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
770 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800771 Device* device = getDevice(deviceId);
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700772 if (device) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800773 ssize_t index = device->absoluteAxes.indexOfKey(axis);
774 if (index >= 0) {
775 *outAxisInfo = device->absoluteAxes.valueAt(index);
776 return OK;
777 }
778 }
779 outAxisInfo->clear();
780 return -1;
781 }
782
Chris Yea52ade12020-08-27 16:49:20 -0700783 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800784 Device* device = getDevice(deviceId);
785 if (device) {
786 return device->relativeAxes.indexOfKey(axis) >= 0;
787 }
788 return false;
789 }
790
Chris Yea52ade12020-08-27 16:49:20 -0700791 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800792
Chris Yef59a2f42020-10-16 12:55:26 -0700793 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
794 Device* device = getDevice(deviceId);
795 if (device) {
796 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
797 }
798 return false;
799 }
800
Chris Yea52ade12020-08-27 16:49:20 -0700801 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
802 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800803 Device* device = getDevice(deviceId);
804 if (device) {
805 const KeyInfo* key = getKey(device, scanCode, usageCode);
806 if (key) {
807 if (outKeycode) {
808 *outKeycode = key->keyCode;
809 }
810 if (outFlags) {
811 *outFlags = key->flags;
812 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700813 if (outMetaState) {
814 *outMetaState = metaState;
815 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800816 return OK;
817 }
818 }
819 return NAME_NOT_FOUND;
820 }
821
822 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
823 if (usageCode) {
824 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
825 if (index >= 0) {
826 return &device->keysByUsageCode.valueAt(index);
827 }
828 }
829 if (scanCode) {
830 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
831 if (index >= 0) {
832 return &device->keysByScanCode.valueAt(index);
833 }
834 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700835 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800836 }
837
Chris Yea52ade12020-08-27 16:49:20 -0700838 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800839
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000840 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
841 int32_t deviceId, int32_t absCode) const override {
Chris Yef59a2f42020-10-16 12:55:26 -0700842 Device* device = getDevice(deviceId);
843 if (!device) {
844 return Errorf("Sensor device not found.");
845 }
846 auto it = device->sensorsByAbsCode.find(absCode);
847 if (it == device->sensorsByAbsCode.end()) {
848 return Errorf("Sensor map not found.");
849 }
850 const SensorInfo& info = it->second;
851 return std::make_pair(info.sensorType, info.sensorDataIndex);
852 }
853
Chris Yea52ade12020-08-27 16:49:20 -0700854 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800855 mExcludedDevices = devices;
856 }
857
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700858 std::vector<RawEvent> getEvents(int) override {
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000859 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700861 std::vector<RawEvent> buffer;
862 std::swap(buffer, mEvents);
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000863
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700864 mEventsCondition.notify_all();
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700865 return buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800866 }
867
Chris Yea52ade12020-08-27 16:49:20 -0700868 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600869 auto it = mVideoFrames.find(deviceId);
870 if (it != mVideoFrames.end()) {
871 std::vector<TouchVideoFrame> frames = std::move(it->second);
872 mVideoFrames.erase(deviceId);
873 return frames;
874 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800875 return {};
876 }
877
Chris Yea52ade12020-08-27 16:49:20 -0700878 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800879 Device* device = getDevice(deviceId);
880 if (device) {
881 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
882 if (index >= 0) {
883 return device->scanCodeStates.valueAt(index);
884 }
885 }
886 return AKEY_STATE_UNKNOWN;
887 }
888
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000889 InputDeviceCountryCode getCountryCode(int32_t deviceId) const override {
890 Device* device = getDevice(deviceId);
891 if (device) {
892 return device->countryCode;
893 }
894 return InputDeviceCountryCode::INVALID;
895 }
896
Chris Yea52ade12020-08-27 16:49:20 -0700897 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898 Device* device = getDevice(deviceId);
899 if (device) {
900 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
901 if (index >= 0) {
902 return device->keyCodeStates.valueAt(index);
903 }
904 }
905 return AKEY_STATE_UNKNOWN;
906 }
907
Chris Yea52ade12020-08-27 16:49:20 -0700908 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909 Device* device = getDevice(deviceId);
910 if (device) {
911 ssize_t index = device->switchStates.indexOfKey(sw);
912 if (index >= 0) {
913 return device->switchStates.valueAt(index);
914 }
915 }
916 return AKEY_STATE_UNKNOWN;
917 }
918
Chris Yea52ade12020-08-27 16:49:20 -0700919 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
920 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921 Device* device = getDevice(deviceId);
922 if (device) {
923 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
924 if (index >= 0) {
925 *outValue = device->absoluteAxisValue.valueAt(index);
926 return OK;
927 }
928 }
929 *outValue = 0;
930 return -1;
931 }
932
Philip Junker4af3b3d2021-12-14 10:36:55 +0100933 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
934 Device* device = getDevice(deviceId);
935 if (!device) {
936 return AKEYCODE_UNKNOWN;
937 }
938 auto it = device->keyCodeMapping.find(locationKeyCode);
939 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
940 }
941
Chris Yea52ade12020-08-27 16:49:20 -0700942 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700943 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700944 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800945 bool result = false;
946 Device* device = getDevice(deviceId);
947 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700948 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700949 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800950 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
951 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
952 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953 }
954 }
955 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
956 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
957 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800958 }
959 }
960 }
961 }
962 return result;
963 }
964
Chris Yea52ade12020-08-27 16:49:20 -0700965 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800966 Device* device = getDevice(deviceId);
967 if (device) {
968 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
969 return index >= 0;
970 }
971 return false;
972 }
973
Arthur Hungcb40a002021-08-03 14:31:01 +0000974 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
975 Device* device = getDevice(deviceId);
976 if (!device) {
977 return false;
978 }
979 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
980 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
981 return true;
982 }
983 }
984 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
985 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
986 return true;
987 }
988 }
989 return false;
990 }
991
Chris Yea52ade12020-08-27 16:49:20 -0700992 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800993 Device* device = getDevice(deviceId);
994 return device && device->leds.indexOfKey(led) >= 0;
995 }
996
Chris Yea52ade12020-08-27 16:49:20 -0700997 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800998 Device* device = getDevice(deviceId);
999 if (device) {
1000 ssize_t index = device->leds.indexOfKey(led);
1001 if (index >= 0) {
1002 device->leds.replaceValueAt(led, on);
1003 } else {
1004 ADD_FAILURE()
1005 << "Attempted to set the state of an LED that the EventHub declared "
1006 "was not present. led=" << led;
1007 }
1008 }
1009 }
1010
Chris Yea52ade12020-08-27 16:49:20 -07001011 void getVirtualKeyDefinitions(
1012 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001013 outVirtualKeys.clear();
1014
1015 Device* device = getDevice(deviceId);
1016 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001017 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001018 }
1019 }
1020
Chris Yea52ade12020-08-27 16:49:20 -07001021 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -07001022 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001023 }
1024
Chris Yea52ade12020-08-27 16:49:20 -07001025 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001026 return false;
1027 }
1028
Chris Yea52ade12020-08-27 16:49:20 -07001029 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001030
Chris Yea52ade12020-08-27 16:49:20 -07001031 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001032
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001033 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return mVibrators; };
Chris Ye87143712020-11-10 05:05:58 +00001034
Chris Yee2b1e5c2021-03-10 22:45:12 -08001035 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
1036 return BATTERY_CAPACITY;
1037 }
Kim Low03ea0352020-11-06 12:45:07 -08001038
Chris Yee2b1e5c2021-03-10 22:45:12 -08001039 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
1040 return BATTERY_STATUS;
1041 }
1042
Andy Chenf9f1a022022-08-29 20:07:10 -04001043 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override {
1044 return {DEFAULT_BATTERY};
1045 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001046
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001047 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
1048 int32_t batteryId) const override {
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001049 if (batteryId != DEFAULT_BATTERY) return {};
1050 static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY,
1051 .name = "default battery",
1052 .flags = InputBatteryClass::CAPACITY,
1053 .path = BATTERY_DEVPATH};
1054 return BATTERY_INFO;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001055 }
Kim Low03ea0352020-11-06 12:45:07 -08001056
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001057 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001058 std::vector<int32_t> ids;
1059 for (const auto& [rawId, info] : mRawLightInfos) {
1060 ids.push_back(rawId);
1061 }
1062 return ids;
1063 }
1064
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001065 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001066 auto it = mRawLightInfos.find(lightId);
1067 if (it == mRawLightInfos.end()) {
1068 return std::nullopt;
1069 }
1070 return it->second;
1071 }
1072
1073 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1074 mLightBrightness.emplace(lightId, brightness);
1075 }
1076
1077 void setLightIntensities(int32_t deviceId, int32_t lightId,
1078 std::unordered_map<LightColor, int32_t> intensities) override {
1079 mLightIntensities.emplace(lightId, intensities);
1080 };
1081
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001082 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001083 auto lightIt = mLightBrightness.find(lightId);
1084 if (lightIt == mLightBrightness.end()) {
1085 return std::nullopt;
1086 }
1087 return lightIt->second;
1088 }
1089
1090 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001091 int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001092 auto lightIt = mLightIntensities.find(lightId);
1093 if (lightIt == mLightIntensities.end()) {
1094 return std::nullopt;
1095 }
1096 return lightIt->second;
1097 };
1098
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001099 void dump(std::string&) const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001100
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001101 void monitor() const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102
Chris Yea52ade12020-08-27 16:49:20 -07001103 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001104
Chris Yea52ade12020-08-27 16:49:20 -07001105 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001106};
1107
Michael Wrightd02c5b62014-02-10 15:10:22 -08001108// --- FakeInputMapper ---
1109
1110class FakeInputMapper : public InputMapper {
1111 uint32_t mSources;
1112 int32_t mKeyboardType;
1113 int32_t mMetaState;
1114 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1115 KeyedVector<int32_t, int32_t> mScanCodeStates;
1116 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001117 // fake mapping which would normally come from keyCharacterMap
1118 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001119 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001120
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001121 std::mutex mLock;
1122 std::condition_variable mStateChangedCondition;
1123 bool mConfigureWasCalled GUARDED_BY(mLock);
1124 bool mResetWasCalled GUARDED_BY(mLock);
1125 bool mProcessWasCalled GUARDED_BY(mLock);
1126 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001127
Arthur Hungc23540e2018-11-29 20:42:11 +08001128 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001129public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001130 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1131 : InputMapper(deviceContext),
1132 mSources(sources),
1133 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001134 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001135 mConfigureWasCalled(false),
1136 mResetWasCalled(false),
1137 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001138
Chris Yea52ade12020-08-27 16:49:20 -07001139 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001140
1141 void setKeyboardType(int32_t keyboardType) {
1142 mKeyboardType = keyboardType;
1143 }
1144
1145 void setMetaState(int32_t metaState) {
1146 mMetaState = metaState;
1147 }
1148
1149 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001150 std::unique_lock<std::mutex> lock(mLock);
1151 base::ScopedLockAssertion assumeLocked(mLock);
1152 const bool configureCalled =
1153 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1154 return mConfigureWasCalled;
1155 });
1156 if (!configureCalled) {
1157 FAIL() << "Expected configure() to have been called.";
1158 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001159 mConfigureWasCalled = false;
1160 }
1161
1162 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001163 std::unique_lock<std::mutex> lock(mLock);
1164 base::ScopedLockAssertion assumeLocked(mLock);
1165 const bool resetCalled =
1166 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1167 return mResetWasCalled;
1168 });
1169 if (!resetCalled) {
1170 FAIL() << "Expected reset() to have been called.";
1171 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001172 mResetWasCalled = false;
1173 }
1174
Yi Kong9b14ac62018-07-17 13:48:38 -07001175 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001176 std::unique_lock<std::mutex> lock(mLock);
1177 base::ScopedLockAssertion assumeLocked(mLock);
1178 const bool processCalled =
1179 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1180 return mProcessWasCalled;
1181 });
1182 if (!processCalled) {
1183 FAIL() << "Expected process() to have been called.";
1184 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001185 if (outLastEvent) {
1186 *outLastEvent = mLastEvent;
1187 }
1188 mProcessWasCalled = false;
1189 }
1190
1191 void setKeyCodeState(int32_t keyCode, int32_t state) {
1192 mKeyCodeStates.replaceValueFor(keyCode, state);
1193 }
1194
1195 void setScanCodeState(int32_t scanCode, int32_t state) {
1196 mScanCodeStates.replaceValueFor(scanCode, state);
1197 }
1198
1199 void setSwitchState(int32_t switchCode, int32_t state) {
1200 mSwitchStates.replaceValueFor(switchCode, state);
1201 }
1202
1203 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001204 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001205 }
1206
Philip Junker4af3b3d2021-12-14 10:36:55 +01001207 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1208 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1209 }
1210
Michael Wrightd02c5b62014-02-10 15:10:22 -08001211private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001212 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001213
Chris Yea52ade12020-08-27 16:49:20 -07001214 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001215 InputMapper::populateDeviceInfo(deviceInfo);
1216
1217 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1218 deviceInfo->setKeyboardType(mKeyboardType);
1219 }
1220 }
1221
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001222 std::list<NotifyArgs> configure(nsecs_t, const InputReaderConfiguration* config,
1223 uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001224 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001225 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001226
1227 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001228 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001229 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1230 mViewport = config->getDisplayViewportByPort(*displayPort);
1231 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001232
1233 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001234 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001235 }
1236
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001237 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001238 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001239 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001240 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001241 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001242 }
1243
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001244 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001245 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001246 mLastEvent = *rawEvent;
1247 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001248 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001249 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001250 }
1251
Chris Yea52ade12020-08-27 16:49:20 -07001252 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001253 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1254 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1255 }
1256
Philip Junker4af3b3d2021-12-14 10:36:55 +01001257 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1258 auto it = mKeyCodeMapping.find(locationKeyCode);
1259 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1260 }
1261
Chris Yea52ade12020-08-27 16:49:20 -07001262 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001263 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1264 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1265 }
1266
Chris Yea52ade12020-08-27 16:49:20 -07001267 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001268 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1269 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1270 }
1271
Chris Yea52ade12020-08-27 16:49:20 -07001272 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001273 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001274 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001275 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001276 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1277 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1278 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001279 }
1280 }
1281 }
Chris Yea52ade12020-08-27 16:49:20 -07001282 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001283 return result;
1284 }
1285
1286 virtual int32_t getMetaState() {
1287 return mMetaState;
1288 }
1289
1290 virtual void fadePointer() {
1291 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001292
1293 virtual std::optional<int32_t> getAssociatedDisplay() {
1294 if (mViewport) {
1295 return std::make_optional(mViewport->displayId);
1296 }
1297 return std::nullopt;
1298 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001299};
1300
1301
1302// --- InstrumentedInputReader ---
1303
1304class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001305 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001306
1307public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001308 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1309 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001310 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001311 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001312
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001313 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001314
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001315 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001316
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001317 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001318 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001319 InputDeviceIdentifier identifier;
1320 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001321 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001322 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001323 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001324 }
1325
Prabir Pradhan28efc192019-11-05 01:10:04 +00001326 // Make the protected loopOnce method accessible to tests.
1327 using InputReader::loopOnce;
1328
Michael Wrightd02c5b62014-02-10 15:10:22 -08001329protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001330 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1331 const InputDeviceIdentifier& identifier)
1332 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001333 if (!mNextDevices.empty()) {
1334 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1335 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001336 return device;
1337 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001338 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001339 }
1340
arthurhungdcef2dc2020-08-11 14:47:50 +08001341 // --- FakeInputReaderContext ---
1342 class FakeInputReaderContext : public ContextImpl {
1343 int32_t mGlobalMetaState;
1344 bool mUpdateGlobalMetaStateWasCalled;
1345 int32_t mGeneration;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00001346 std::optional<nsecs_t> mRequestedTimeout;
1347 std::vector<InputDeviceInfo> mExternalStylusDevices;
arthurhungdcef2dc2020-08-11 14:47:50 +08001348
1349 public:
1350 FakeInputReaderContext(InputReader* reader)
1351 : ContextImpl(reader),
1352 mGlobalMetaState(0),
1353 mUpdateGlobalMetaStateWasCalled(false),
1354 mGeneration(1) {}
1355
1356 virtual ~FakeInputReaderContext() {}
1357
1358 void assertUpdateGlobalMetaStateWasCalled() {
1359 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1360 << "Expected updateGlobalMetaState() to have been called.";
1361 mUpdateGlobalMetaStateWasCalled = false;
1362 }
1363
1364 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1365
1366 uint32_t getGeneration() { return mGeneration; }
1367
1368 void updateGlobalMetaState() override {
1369 mUpdateGlobalMetaStateWasCalled = true;
1370 ContextImpl::updateGlobalMetaState();
1371 }
1372
1373 int32_t getGlobalMetaState() override {
1374 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1375 }
1376
1377 int32_t bumpGeneration() override {
1378 mGeneration = ContextImpl::bumpGeneration();
1379 return mGeneration;
1380 }
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00001381
1382 void requestTimeoutAtTime(nsecs_t when) override { mRequestedTimeout = when; }
1383
1384 void assertTimeoutWasRequested(nsecs_t when) {
1385 ASSERT_TRUE(mRequestedTimeout) << "Expected timeout at time " << when
1386 << " but there was no timeout requested.";
1387 ASSERT_EQ(when, *mRequestedTimeout);
1388 mRequestedTimeout.reset();
1389 }
1390
1391 void assertTimeoutWasNotRequested() {
1392 ASSERT_FALSE(mRequestedTimeout) << "Expected no timeout to have been requested,"
1393 " but one was requested at time "
1394 << *mRequestedTimeout;
1395 }
1396
1397 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override {
1398 outDevices = mExternalStylusDevices;
1399 }
1400
1401 void setExternalStylusDevices(std::vector<InputDeviceInfo>&& devices) {
1402 mExternalStylusDevices = devices;
1403 }
arthurhungdcef2dc2020-08-11 14:47:50 +08001404 } mFakeContext;
1405
Michael Wrightd02c5b62014-02-10 15:10:22 -08001406 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001407
1408public:
1409 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001410};
1411
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001412// --- InputReaderPolicyTest ---
1413class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001414protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001415 sp<FakeInputReaderPolicy> mFakePolicy;
1416
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001417 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001418 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001419};
1420
1421/**
1422 * Check that empty set of viewports is an acceptable configuration.
1423 * Also try to get internal viewport two different ways - by type and by uniqueId.
1424 *
1425 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1426 * Such configuration is not currently allowed.
1427 */
1428TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001429 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001430
1431 // We didn't add any viewports yet, so there shouldn't be any.
1432 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001433 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001434 ASSERT_FALSE(internalViewport);
1435
1436 // Add an internal viewport, then clear it
1437 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001438 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001439 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001440
1441 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001442 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001443 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001444 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001445
1446 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001447 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001448 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001449 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001450
1451 mFakePolicy->clearViewports();
1452 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001453 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001454 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001455 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001456 ASSERT_FALSE(internalViewport);
1457}
1458
1459TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1460 const std::string internalUniqueId = "local:0";
1461 const std::string externalUniqueId = "local:1";
1462 const std::string virtualUniqueId1 = "virtual:2";
1463 const std::string virtualUniqueId2 = "virtual:3";
1464 constexpr int32_t virtualDisplayId1 = 2;
1465 constexpr int32_t virtualDisplayId2 = 3;
1466
1467 // Add an internal viewport
1468 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001469 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1470 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001471 // Add an external viewport
1472 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001473 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1474 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001475 // Add an virtual viewport
1476 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001477 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1478 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001479 // Add another virtual viewport
1480 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001481 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1482 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001483
1484 // Check matching by type for internal
1485 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001486 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001487 ASSERT_TRUE(internalViewport);
1488 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1489
1490 // Check matching by type for external
1491 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001492 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001493 ASSERT_TRUE(externalViewport);
1494 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1495
1496 // Check matching by uniqueId for virtual viewport #1
1497 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001498 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001499 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001500 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001501 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1502 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1503
1504 // Check matching by uniqueId for virtual viewport #2
1505 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001506 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001507 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001508 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001509 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1510 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1511}
1512
1513
1514/**
1515 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1516 * that lookup works by checking display id.
1517 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1518 */
1519TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1520 const std::string uniqueId1 = "uniqueId1";
1521 const std::string uniqueId2 = "uniqueId2";
1522 constexpr int32_t displayId1 = 2;
1523 constexpr int32_t displayId2 = 3;
1524
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001525 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1526 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001527 for (const ViewportType& type : types) {
1528 mFakePolicy->clearViewports();
1529 // Add a viewport
1530 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001531 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1532 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001533 // Add another viewport
1534 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001535 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1536 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001537
1538 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001539 std::optional<DisplayViewport> viewport1 =
1540 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001541 ASSERT_TRUE(viewport1);
1542 ASSERT_EQ(displayId1, viewport1->displayId);
1543 ASSERT_EQ(type, viewport1->type);
1544
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001545 std::optional<DisplayViewport> viewport2 =
1546 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001547 ASSERT_TRUE(viewport2);
1548 ASSERT_EQ(displayId2, viewport2->displayId);
1549 ASSERT_EQ(type, viewport2->type);
1550
1551 // When there are multiple viewports of the same kind, and uniqueId is not specified
1552 // in the call to getDisplayViewport, then that situation is not supported.
1553 // The viewports can be stored in any order, so we cannot rely on the order, since that
1554 // is just implementation detail.
1555 // However, we can check that it still returns *a* viewport, we just cannot assert
1556 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001557 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001558 ASSERT_TRUE(someViewport);
1559 }
1560}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001561
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001562/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001563 * When we have multiple internal displays make sure we always return the default display when
1564 * querying by type.
1565 */
1566TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1567 const std::string uniqueId1 = "uniqueId1";
1568 const std::string uniqueId2 = "uniqueId2";
1569 constexpr int32_t nonDefaultDisplayId = 2;
1570 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1571 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1572
1573 // Add the default display first and ensure it gets returned.
1574 mFakePolicy->clearViewports();
1575 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001576 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001577 ViewportType::INTERNAL);
1578 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001579 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001580 ViewportType::INTERNAL);
1581
1582 std::optional<DisplayViewport> viewport =
1583 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1584 ASSERT_TRUE(viewport);
1585 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1586 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1587
1588 // Add the default display second to make sure order doesn't matter.
1589 mFakePolicy->clearViewports();
1590 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001591 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001592 ViewportType::INTERNAL);
1593 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001594 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001595 ViewportType::INTERNAL);
1596
1597 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1598 ASSERT_TRUE(viewport);
1599 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1600 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1601}
1602
1603/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001604 * Check getDisplayViewportByPort
1605 */
1606TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001607 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001608 const std::string uniqueId1 = "uniqueId1";
1609 const std::string uniqueId2 = "uniqueId2";
1610 constexpr int32_t displayId1 = 1;
1611 constexpr int32_t displayId2 = 2;
1612 const uint8_t hdmi1 = 0;
1613 const uint8_t hdmi2 = 1;
1614 const uint8_t hdmi3 = 2;
1615
1616 mFakePolicy->clearViewports();
1617 // Add a viewport that's associated with some display port that's not of interest.
1618 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001619 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1620 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001621 // Add another viewport, connected to HDMI1 port
1622 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001623 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1624 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001625
1626 // Check that correct display viewport was returned by comparing the display ports.
1627 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1628 ASSERT_TRUE(hdmi1Viewport);
1629 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1630 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1631
1632 // Check that we can still get the same viewport using the uniqueId
1633 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1634 ASSERT_TRUE(hdmi1Viewport);
1635 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1636 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1637 ASSERT_EQ(type, hdmi1Viewport->type);
1638
1639 // Check that we cannot find a port with "HDMI2", because we never added one
1640 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1641 ASSERT_FALSE(hdmi2Viewport);
1642}
1643
Michael Wrightd02c5b62014-02-10 15:10:22 -08001644// --- InputReaderTest ---
1645
1646class InputReaderTest : public testing::Test {
1647protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001648 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001649 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001650 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001651 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001652
Chris Yea52ade12020-08-27 16:49:20 -07001653 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001654 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001655 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001656 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001657
Prabir Pradhan28efc192019-11-05 01:10:04 +00001658 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001659 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001660 }
1661
Chris Yea52ade12020-08-27 16:49:20 -07001662 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001663 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001664 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001665 }
1666
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001667 void addDevice(int32_t eventHubId, const std::string& name,
1668 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001669 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001670
1671 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001672 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001673 }
1674 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001675 mReader->loopOnce();
1676 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001677 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1678 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001679 }
1680
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001681 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001682 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001683 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001684 }
1685
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001686 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001687 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001688 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001689 }
1690
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001691 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001692 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001693 ftl::Flags<InputDeviceClass> classes,
1694 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001695 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001696 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1697 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001698 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001699 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001700 return mapper;
1701 }
1702};
1703
Chris Ye98d3f532020-10-01 21:48:59 -07001704TEST_F(InputReaderTest, PolicyGetInputDevices) {
1705 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001706 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001707 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001708
1709 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001710 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001711 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001712 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001713 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001714 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1715 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001716 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001717}
1718
Chris Yee7310032020-09-22 15:36:28 -07001719TEST_F(InputReaderTest, GetMergedInputDevices) {
1720 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1721 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1722 // Add two subdevices to device
1723 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1724 // Must add at least one mapper or the device will be ignored!
1725 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1726 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1727
1728 // Push same device instance for next device to be added, so they'll have same identifier.
1729 mReader->pushNextDevice(device);
1730 mReader->pushNextDevice(device);
1731 ASSERT_NO_FATAL_FAILURE(
1732 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1733 ASSERT_NO_FATAL_FAILURE(
1734 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1735
1736 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001737 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001738}
1739
Chris Yee14523a2020-12-19 13:46:00 -08001740TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1741 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1742 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1743 // Add two subdevices to device
1744 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1745 // Must add at least one mapper or the device will be ignored!
1746 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1747 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1748
1749 // Push same device instance for next device to be added, so they'll have same identifier.
1750 mReader->pushNextDevice(device);
1751 mReader->pushNextDevice(device);
1752 // Sensor device is initially disabled
1753 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1754 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1755 nullptr));
1756 // Device is disabled because the only sub device is a sensor device and disabled initially.
1757 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1758 ASSERT_FALSE(device->isEnabled());
1759 ASSERT_NO_FATAL_FAILURE(
1760 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1761 // The merged device is enabled if any sub device is enabled
1762 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1763 ASSERT_TRUE(device->isEnabled());
1764}
1765
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001766TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001767 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001768 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001769 constexpr int32_t eventHubId = 1;
1770 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001771 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001772 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001773 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001774 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001775
Yi Kong9b14ac62018-07-17 13:48:38 -07001776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001777
1778 NotifyDeviceResetArgs resetArgs;
1779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001780 ASSERT_EQ(deviceId, resetArgs.deviceId);
1781
1782 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001783 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001784 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001785
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001787 ASSERT_EQ(deviceId, resetArgs.deviceId);
1788 ASSERT_EQ(device->isEnabled(), false);
1789
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001790 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001791 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001794 ASSERT_EQ(device->isEnabled(), false);
1795
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001796 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001797 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001799 ASSERT_EQ(deviceId, resetArgs.deviceId);
1800 ASSERT_EQ(device->isEnabled(), true);
1801}
1802
Michael Wrightd02c5b62014-02-10 15:10:22 -08001803TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001804 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001805 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001806 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001807 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001808 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001809 AINPUT_SOURCE_KEYBOARD, nullptr);
1810 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001811
1812 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1813 AINPUT_SOURCE_ANY, AKEYCODE_A))
1814 << "Should return unknown when the device id is >= 0 but unknown.";
1815
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001816 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1817 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1818 << "Should return unknown when the device id is valid but the sources are not "
1819 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001820
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001821 ASSERT_EQ(AKEY_STATE_DOWN,
1822 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1823 AKEYCODE_A))
1824 << "Should return value provided by mapper when device id is valid and the device "
1825 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001826
1827 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1828 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1829 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1830
1831 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1832 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1833 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1834}
1835
Philip Junker4af3b3d2021-12-14 10:36:55 +01001836TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1837 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1838 constexpr int32_t eventHubId = 1;
1839 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1840 InputDeviceClass::KEYBOARD,
1841 AINPUT_SOURCE_KEYBOARD, nullptr);
1842 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1843
1844 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1845 << "Should return unknown when the device with the specified id is not found.";
1846
1847 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1848 << "Should return correct mapping when device id is valid and mapping exists.";
1849
1850 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1851 << "Should return the location key code when device id is valid and there's no "
1852 "mapping.";
1853}
1854
1855TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1856 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1857 constexpr int32_t eventHubId = 1;
1858 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1859 InputDeviceClass::JOYSTICK,
1860 AINPUT_SOURCE_GAMEPAD, nullptr);
1861 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1862
1863 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1864 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1865}
1866
Michael Wrightd02c5b62014-02-10 15:10:22 -08001867TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001868 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001869 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001870 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001871 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001872 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001873 AINPUT_SOURCE_KEYBOARD, nullptr);
1874 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001875
1876 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1877 AINPUT_SOURCE_ANY, KEY_A))
1878 << "Should return unknown when the device id is >= 0 but unknown.";
1879
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001880 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1881 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1882 << "Should return unknown when the device id is valid but the sources are not "
1883 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001884
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001885 ASSERT_EQ(AKEY_STATE_DOWN,
1886 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1887 KEY_A))
1888 << "Should return value provided by mapper when device id is valid and the device "
1889 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001890
1891 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1892 AINPUT_SOURCE_TRACKBALL, KEY_A))
1893 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1894
1895 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1896 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1897 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1898}
1899
1900TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001901 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001902 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001903 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001904 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001905 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001906 AINPUT_SOURCE_KEYBOARD, nullptr);
1907 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001908
1909 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1910 AINPUT_SOURCE_ANY, SW_LID))
1911 << "Should return unknown when the device id is >= 0 but unknown.";
1912
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001913 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1914 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1915 << "Should return unknown when the device id is valid but the sources are not "
1916 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001917
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001918 ASSERT_EQ(AKEY_STATE_DOWN,
1919 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1920 SW_LID))
1921 << "Should return value provided by mapper when device id is valid and the device "
1922 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001923
1924 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1925 AINPUT_SOURCE_TRACKBALL, SW_LID))
1926 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1927
1928 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1929 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1930 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1931}
1932
1933TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001934 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001935 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001936 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001937 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001938 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001939 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001940
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001941 mapper.addSupportedKeyCode(AKEYCODE_A);
1942 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001943
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001944 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001945 uint8_t flags[4] = { 0, 0, 0, 1 };
1946
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001947 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001948 << "Should return false when device id is >= 0 but unknown.";
1949 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1950
1951 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001952 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001953 << "Should return false when device id is valid but the sources are not supported by "
1954 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001955 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1956
1957 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001958 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001959 keyCodes, flags))
1960 << "Should return value provided by mapper when device id is valid and the device "
1961 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001962 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1963
1964 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001965 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1966 << "Should return false when the device id is < 0 but the sources are not supported by "
1967 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001968 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1969
1970 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001971 ASSERT_TRUE(
1972 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1973 << "Should return value provided by mapper when device id is < 0 and one of the "
1974 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001975 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1976}
1977
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001978TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001979 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001980 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001981
1982 NotifyConfigurationChangedArgs args;
1983
1984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1985 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1986}
1987
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001988TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001989 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001990 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001991 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001992 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001993 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001994 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001995 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001996 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001997
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001998 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001999 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002000 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
2001
2002 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002003 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002004 ASSERT_EQ(when, event.when);
2005 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002006 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002007 ASSERT_EQ(EV_KEY, event.type);
2008 ASSERT_EQ(KEY_A, event.code);
2009 ASSERT_EQ(1, event.value);
2010}
2011
Garfield Tan1c7bc862020-01-28 13:24:04 -08002012TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002013 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002014 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002015 constexpr int32_t eventHubId = 1;
2016 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08002017 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002018 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002019 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002020 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08002021
2022 NotifyDeviceResetArgs resetArgs;
2023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002024 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002025
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002026 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002027 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002029 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002030 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002031
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002032 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002033 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002035 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002036 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002037
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002038 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002039 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002041 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002042 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002043}
2044
Garfield Tan1c7bc862020-01-28 13:24:04 -08002045TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
2046 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002047 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002048 constexpr int32_t eventHubId = 1;
2049 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2050 // Must add at least one mapper or the device will be ignored!
2051 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002052 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002053 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
2054
2055 NotifyDeviceResetArgs resetArgs;
2056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2057 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
2058}
2059
Arthur Hungc23540e2018-11-29 20:42:11 +08002060TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002061 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002062 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002063 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08002064 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002065 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2066 FakeInputMapper& mapper =
2067 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002068 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002069
2070 const uint8_t hdmi1 = 1;
2071
2072 // Associated touch screen with second display.
2073 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2074
2075 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002076 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002077 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002078 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002079 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002080 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002081 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002082 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002083 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002084 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002085
2086 // Add the device, and make sure all of the callbacks are triggered.
2087 // The device is added after the input port associations are processed since
2088 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002089 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002092 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002093
Arthur Hung2c9a3342019-07-23 14:18:59 +08002094 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002095 ASSERT_EQ(deviceId, device->getId());
2096 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2097 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002098
2099 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002100 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002101 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002102 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002103}
2104
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002105TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2106 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002107 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002108 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2109 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2110 // Must add at least one mapper or the device will be ignored!
2111 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2112 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2113 mReader->pushNextDevice(device);
2114 mReader->pushNextDevice(device);
2115 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2116 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2117
2118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2119
2120 NotifyDeviceResetArgs resetArgs;
2121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2122 ASSERT_EQ(deviceId, resetArgs.deviceId);
2123 ASSERT_TRUE(device->isEnabled());
2124 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2125 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2126
2127 disableDevice(deviceId);
2128 mReader->loopOnce();
2129
2130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2131 ASSERT_EQ(deviceId, resetArgs.deviceId);
2132 ASSERT_FALSE(device->isEnabled());
2133 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2134 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2135
2136 enableDevice(deviceId);
2137 mReader->loopOnce();
2138
2139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2140 ASSERT_EQ(deviceId, resetArgs.deviceId);
2141 ASSERT_TRUE(device->isEnabled());
2142 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2143 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2144}
2145
2146TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2147 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002148 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002149 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2150 // Add two subdevices to device
2151 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2152 FakeInputMapper& mapperDevice1 =
2153 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2154 FakeInputMapper& mapperDevice2 =
2155 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2156 mReader->pushNextDevice(device);
2157 mReader->pushNextDevice(device);
2158 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2159 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2160
2161 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2162 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2163
2164 ASSERT_EQ(AKEY_STATE_DOWN,
2165 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2166 ASSERT_EQ(AKEY_STATE_DOWN,
2167 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2168 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2169 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2170}
2171
Prabir Pradhan7e186182020-11-10 13:56:45 -08002172TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2173 NotifyPointerCaptureChangedArgs args;
2174
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002175 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002176 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2177 mReader->loopOnce();
2178 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002179 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2180 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002181
2182 mFakePolicy->setPointerCapture(false);
2183 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2184 mReader->loopOnce();
2185 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002186 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002187
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002188 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002189 // does not change.
2190 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2191 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002192 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002193}
2194
Chris Ye87143712020-11-10 05:05:58 +00002195class FakeVibratorInputMapper : public FakeInputMapper {
2196public:
2197 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2198 : FakeInputMapper(deviceContext, sources) {}
2199
2200 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2201};
2202
2203TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2204 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002205 ftl::Flags<InputDeviceClass> deviceClass =
2206 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002207 constexpr int32_t eventHubId = 1;
2208 const char* DEVICE_LOCATION = "BLUETOOTH";
2209 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2210 FakeVibratorInputMapper& mapper =
2211 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2212 mReader->pushNextDevice(device);
2213
2214 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2215 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2216
2217 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2218 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2219}
2220
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002221// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002222
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002223class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002224public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002225 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002226
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002227 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002228
Andy Chenf9f1a022022-08-29 20:07:10 -04002229 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2230
Chris Yee2b1e5c2021-03-10 22:45:12 -08002231 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2232
2233 void dump(std::string& dump) override {}
2234
2235 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2236 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002237 }
2238
Chris Yee2b1e5c2021-03-10 22:45:12 -08002239 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2240 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002241 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002242
2243 bool setLightColor(int32_t lightId, int32_t color) override {
2244 getDeviceContext().setLightBrightness(lightId, color >> 24);
2245 return true;
2246 }
2247
2248 std::optional<int32_t> getLightColor(int32_t lightId) override {
2249 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2250 if (!result.has_value()) {
2251 return std::nullopt;
2252 }
2253 return result.value() << 24;
2254 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002255
2256 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2257
2258 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2259
2260private:
2261 InputDeviceContext& mDeviceContext;
2262 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2263 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04002264 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002265};
2266
Chris Yee2b1e5c2021-03-10 22:45:12 -08002267TEST_F(InputReaderTest, BatteryGetCapacity) {
2268 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002269 ftl::Flags<InputDeviceClass> deviceClass =
2270 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002271 constexpr int32_t eventHubId = 1;
2272 const char* DEVICE_LOCATION = "BLUETOOTH";
2273 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002274 FakePeripheralController& controller =
2275 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002276 mReader->pushNextDevice(device);
2277
2278 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2279
2280 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2281 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2282}
2283
2284TEST_F(InputReaderTest, BatteryGetStatus) {
2285 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002286 ftl::Flags<InputDeviceClass> deviceClass =
2287 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002288 constexpr int32_t eventHubId = 1;
2289 const char* DEVICE_LOCATION = "BLUETOOTH";
2290 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002291 FakePeripheralController& controller =
2292 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002293 mReader->pushNextDevice(device);
2294
2295 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2296
2297 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2298 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2299}
2300
Prabir Pradhane287ecd2022-09-07 21:18:05 +00002301TEST_F(InputReaderTest, BatteryGetDevicePath) {
2302 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2303 ftl::Flags<InputDeviceClass> deviceClass =
2304 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2305 constexpr int32_t eventHubId = 1;
2306 const char* DEVICE_LOCATION = "BLUETOOTH";
2307 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2308 device->addController<FakePeripheralController>(eventHubId);
2309 mReader->pushNextDevice(device);
2310
2311 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2312
2313 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), BATTERY_DEVPATH);
2314}
2315
Chris Ye3fdbfef2021-01-06 18:45:18 -08002316TEST_F(InputReaderTest, LightGetColor) {
2317 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002318 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002319 constexpr int32_t eventHubId = 1;
2320 const char* DEVICE_LOCATION = "BLUETOOTH";
2321 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002322 FakePeripheralController& controller =
2323 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002324 mReader->pushNextDevice(device);
2325 RawLightInfo info = {.id = 1,
2326 .name = "Mono",
2327 .maxBrightness = 255,
2328 .flags = InputLightClass::BRIGHTNESS,
2329 .path = ""};
2330 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2331 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2332
2333 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002334
Chris Yee2b1e5c2021-03-10 22:45:12 -08002335 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2336 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002337 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2338 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2339}
2340
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002341// --- InputReaderIntegrationTest ---
2342
2343// These tests create and interact with the InputReader only through its interface.
2344// The InputReader is started during SetUp(), which starts its processing in its own
2345// thread. The tests use linux uinput to emulate input devices.
2346// NOTE: Interacting with the physical device while these tests are running may cause
2347// the tests to fail.
2348class InputReaderIntegrationTest : public testing::Test {
2349protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002350 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002351 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002352 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002353
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002354 std::shared_ptr<FakePointerController> mFakePointerController;
2355
Chris Yea52ade12020-08-27 16:49:20 -07002356 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002357#if !defined(__ANDROID__)
2358 GTEST_SKIP();
2359#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002360 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002361 mFakePointerController = std::make_shared<FakePointerController>();
2362 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002363 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2364 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002365
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002366 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2367 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002368 ASSERT_EQ(mReader->start(), OK);
2369
2370 // Since this test is run on a real device, all the input devices connected
2371 // to the test device will show up in mReader. We wait for those input devices to
2372 // show up before beginning the tests.
2373 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2374 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2375 }
2376
Chris Yea52ade12020-08-27 16:49:20 -07002377 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002378#if !defined(__ANDROID__)
2379 return;
2380#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002381 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002382 mReader.reset();
2383 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002384 mFakePolicy.clear();
2385 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00002386
2387 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
2388 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
2389 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
2390 [&name](const InputDeviceInfo& info) {
2391 return info.getIdentifier().name == name;
2392 });
2393 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
2394 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002395};
2396
2397TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2398 // An invalid input device that is only used for this test.
2399 class InvalidUinputDevice : public UinputDevice {
2400 public:
Prabir Pradhanb7d434e2022-10-14 22:41:38 +00002401 InvalidUinputDevice() : UinputDevice("Invalid Device", 99 /*productId*/) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002402
2403 private:
2404 void configureDevice(int fd, uinput_user_dev* device) override {}
2405 };
2406
2407 const size_t numDevices = mFakePolicy->getInputDevices().size();
2408
2409 // UinputDevice does not set any event or key bits, so InputReader should not
2410 // consider it as a valid device.
2411 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2412 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2413 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2414 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2415
2416 invalidDevice.reset();
2417 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2418 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2419 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2420}
2421
2422TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2423 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2424
2425 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2426 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2427 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2428 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2429
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002430 const auto device = findDeviceByName(keyboard->getName());
2431 ASSERT_TRUE(device.has_value());
2432 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
2433 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
2434 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002435
2436 keyboard.reset();
2437 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2438 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2439 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2440}
2441
2442TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2443 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2444 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2445
2446 NotifyConfigurationChangedArgs configChangedArgs;
2447 ASSERT_NO_FATAL_FAILURE(
2448 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002449 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002450 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2451
2452 NotifyKeyArgs keyArgs;
2453 keyboard->pressAndReleaseHomeKey();
2454 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2455 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002456 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002457 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002458 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002459 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002460 prevTimestamp = keyArgs.eventTime;
2461
2462 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2463 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002464 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002465 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002466 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002467}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002468
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002469TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
2470 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2471 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2472
2473 const auto device = findDeviceByName(stylus->getName());
2474 ASSERT_TRUE(device.has_value());
2475
Prabir Pradhana3621852022-10-14 18:57:23 +00002476 // An external stylus with buttons should also be recognized as a keyboard.
2477 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002478 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
2479 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
2480
2481 const auto DOWN =
2482 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
2483 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
2484
2485 stylus->pressAndReleaseKey(BTN_STYLUS);
2486 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2487 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2488 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2489 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2490
2491 stylus->pressAndReleaseKey(BTN_STYLUS2);
2492 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2493 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
2494 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2495 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
2496
2497 stylus->pressAndReleaseKey(BTN_STYLUS3);
2498 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2499 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
2500 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2501 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
2502}
2503
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002504/**
2505 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2506 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2507 * are passed to the listener.
2508 */
2509static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2510TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2511 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2512 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2513 NotifyKeyArgs keyArgs;
2514
2515 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2516 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2517 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2518 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2519
2520 controller->pressAndReleaseKey(BTN_GEAR_UP);
2521 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2522 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2523 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2524}
2525
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002526// --- TouchIntegrationTest ---
2527
Arthur Hungaab25622020-01-16 11:22:11 +08002528class TouchIntegrationTest : public InputReaderIntegrationTest {
2529protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002530 const std::string UNIQUE_ID = "local:0";
2531
Chris Yea52ade12020-08-27 16:49:20 -07002532 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002533#if !defined(__ANDROID__)
2534 GTEST_SKIP();
2535#endif
Arthur Hungaab25622020-01-16 11:22:11 +08002536 InputReaderIntegrationTest::SetUp();
2537 // At least add an internal display.
2538 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2539 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002540 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002541
2542 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2543 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2544 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00002545 const auto info = findDeviceByName(mDevice->getName());
2546 ASSERT_TRUE(info);
2547 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08002548 }
2549
2550 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2551 int32_t orientation, const std::string& uniqueId,
2552 std::optional<uint8_t> physicalPort,
2553 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002554 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2555 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002556 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2557 }
2558
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002559 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2560 NotifyMotionArgs args;
2561 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2562 EXPECT_EQ(action, args.action);
2563 ASSERT_EQ(points.size(), args.pointerCount);
2564 for (size_t i = 0; i < args.pointerCount; i++) {
2565 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2566 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2567 }
2568 }
2569
Arthur Hungaab25622020-01-16 11:22:11 +08002570 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00002571 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08002572};
2573
2574TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2575 NotifyMotionArgs args;
2576 const Point centerPoint = mDevice->getCenterPoint();
2577
2578 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002579 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002580 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002581 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002582 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2583 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2584
2585 // ACTION_MOVE
2586 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002587 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002588 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2589 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2590
2591 // ACTION_UP
2592 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002593 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002594 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2595 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2596}
2597
2598TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2599 NotifyMotionArgs args;
2600 const Point centerPoint = mDevice->getCenterPoint();
2601
2602 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002603 mDevice->sendSlot(FIRST_SLOT);
2604 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002605 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002606 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002607 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2608 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2609
2610 // ACTION_POINTER_DOWN (Second slot)
2611 const Point secondPoint = centerPoint + Point(100, 100);
2612 mDevice->sendSlot(SECOND_SLOT);
2613 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002614 mDevice->sendDown(secondPoint);
2615 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002616 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002617 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002618
2619 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002620 mDevice->sendMove(secondPoint + Point(1, 1));
2621 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002622 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2624
2625 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002626 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002627 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002628 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002629 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002630
2631 // ACTION_UP
2632 mDevice->sendSlot(FIRST_SLOT);
2633 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002634 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002635 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2636 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2637}
2638
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002639/**
2640 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2641 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2642 * data?
2643 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2644 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2645 * for Pointer 0 only is generated after.
2646 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2647 * events, we will not miss any information.
2648 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2649 * event generated afterwards that contains the newest movement of pointer 0.
2650 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2651 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2652 * losing information about non-palm pointers.
2653 */
2654TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2655 NotifyMotionArgs args;
2656 const Point centerPoint = mDevice->getCenterPoint();
2657
2658 // ACTION_DOWN
2659 mDevice->sendSlot(FIRST_SLOT);
2660 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2661 mDevice->sendDown(centerPoint);
2662 mDevice->sendSync();
2663 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2664
2665 // ACTION_POINTER_DOWN (Second slot)
2666 const Point secondPoint = centerPoint + Point(100, 100);
2667 mDevice->sendSlot(SECOND_SLOT);
2668 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2669 mDevice->sendDown(secondPoint);
2670 mDevice->sendSync();
2671 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2672
2673 // ACTION_MOVE (First slot)
2674 mDevice->sendSlot(FIRST_SLOT);
2675 mDevice->sendMove(centerPoint + Point(5, 5));
2676 // ACTION_POINTER_UP (Second slot)
2677 mDevice->sendSlot(SECOND_SLOT);
2678 mDevice->sendPointerUp();
2679 // Send a single sync for the above 2 pointer updates
2680 mDevice->sendSync();
2681
2682 // First, we should get POINTER_UP for the second pointer
2683 assertReceivedMotion(ACTION_POINTER_1_UP,
2684 {/*first pointer */ centerPoint + Point(5, 5),
2685 /*second pointer*/ secondPoint});
2686
2687 // Next, the MOVE event for the first pointer
2688 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2689}
2690
2691/**
2692 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2693 * move, and then it will go up, all in the same frame.
2694 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2695 * gets sent to the listener.
2696 */
2697TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2698 NotifyMotionArgs args;
2699 const Point centerPoint = mDevice->getCenterPoint();
2700
2701 // ACTION_DOWN
2702 mDevice->sendSlot(FIRST_SLOT);
2703 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2704 mDevice->sendDown(centerPoint);
2705 mDevice->sendSync();
2706 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2707
2708 // ACTION_POINTER_DOWN (Second slot)
2709 const Point secondPoint = centerPoint + Point(100, 100);
2710 mDevice->sendSlot(SECOND_SLOT);
2711 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2712 mDevice->sendDown(secondPoint);
2713 mDevice->sendSync();
2714 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2715
2716 // ACTION_MOVE (First slot)
2717 mDevice->sendSlot(FIRST_SLOT);
2718 mDevice->sendMove(centerPoint + Point(5, 5));
2719 // ACTION_POINTER_UP (Second slot)
2720 mDevice->sendSlot(SECOND_SLOT);
2721 mDevice->sendMove(secondPoint + Point(6, 6));
2722 mDevice->sendPointerUp();
2723 // Send a single sync for the above 2 pointer updates
2724 mDevice->sendSync();
2725
2726 // First, we should get POINTER_UP for the second pointer
2727 // The movement of the second pointer during the liftoff frame is ignored.
2728 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2729 assertReceivedMotion(ACTION_POINTER_1_UP,
2730 {/*first pointer */ centerPoint + Point(5, 5),
2731 /*second pointer*/ secondPoint});
2732
2733 // Next, the MOVE event for the first pointer
2734 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2735}
2736
Arthur Hungaab25622020-01-16 11:22:11 +08002737TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2738 NotifyMotionArgs args;
2739 const Point centerPoint = mDevice->getCenterPoint();
2740
2741 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002742 mDevice->sendSlot(FIRST_SLOT);
2743 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002744 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002745 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002746 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2747 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2748
arthurhungcc7f9802020-04-30 17:55:40 +08002749 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002750 const Point secondPoint = centerPoint + Point(100, 100);
2751 mDevice->sendSlot(SECOND_SLOT);
2752 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2753 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002754 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002755 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002756 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002757
arthurhungcc7f9802020-04-30 17:55:40 +08002758 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002759 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002760 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002761 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2762 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2763
arthurhungcc7f9802020-04-30 17:55:40 +08002764 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2765 // a palm event.
2766 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002767 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002768 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002769 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002770 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002771 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002772
arthurhungcc7f9802020-04-30 17:55:40 +08002773 // Send up to second slot, expect first slot send moving.
2774 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002775 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002776 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2777 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002778
arthurhungcc7f9802020-04-30 17:55:40 +08002779 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002780 mDevice->sendSlot(FIRST_SLOT);
2781 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002782 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002783
arthurhungcc7f9802020-04-30 17:55:40 +08002784 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2785 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002786}
2787
Prabir Pradhanda20b172022-09-26 17:01:18 +00002788TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
2789 const Point centerPoint = mDevice->getCenterPoint();
2790
2791 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
2792 mDevice->sendSlot(FIRST_SLOT);
2793 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2794 mDevice->sendToolType(MT_TOOL_PEN);
2795 mDevice->sendDown(centerPoint);
2796 mDevice->sendSync();
2797 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2798 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2799 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2800
2801 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2802
2803 // Release the stylus touch.
2804 mDevice->sendUp();
2805 mDevice->sendSync();
2806 ASSERT_NO_FATAL_FAILURE(
2807 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2808
2809 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2810
2811 // Touch down with the finger, without the pen tool selected. The policy is not notified.
2812 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2813 mDevice->sendToolType(MT_TOOL_FINGER);
2814 mDevice->sendDown(centerPoint);
2815 mDevice->sendSync();
2816 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2817 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2818 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2819
2820 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2821
2822 mDevice->sendUp();
2823 mDevice->sendSync();
2824 ASSERT_NO_FATAL_FAILURE(
2825 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2826
2827 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
2828 // The policy should be notified of the stylus presence.
2829 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2830 mDevice->sendToolType(MT_TOOL_PEN);
2831 mDevice->sendMove(centerPoint);
2832 mDevice->sendSync();
2833 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2834 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2835 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2836
2837 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2838}
2839
Prabir Pradhan124ea442022-10-28 20:27:44 +00002840// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002841
Prabir Pradhan124ea442022-10-28 20:27:44 +00002842// Verify the behavior of button presses reported by various kinds of styluses, including buttons
2843// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
2844// stylus.
2845template <typename UinputStylusDevice>
2846class StylusButtonIntegrationTest : public TouchIntegrationTest {
2847protected:
2848 void SetUp() override {
2849#if !defined(__ANDROID__)
2850 GTEST_SKIP();
2851#endif
2852 TouchIntegrationTest::SetUp();
2853 mTouchscreen = mDevice.get();
2854 mTouchscreenInfo = mDeviceInfo;
2855
2856 setUpStylusDevice();
2857 }
2858
2859 UinputStylusDevice* mStylus{nullptr};
2860 InputDeviceInfo mStylusInfo{};
2861
2862 UinputTouchScreen* mTouchscreen{nullptr};
2863 InputDeviceInfo mTouchscreenInfo{};
2864
2865private:
2866 // When we are attempting to test stylus button events that are sent from the touchscreen,
2867 // use the same Uinput device for the touchscreen and the stylus.
2868 template <typename T = UinputStylusDevice>
2869 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2870 mStylus = mDevice.get();
2871 mStylusInfo = mDeviceInfo;
2872 }
2873
2874 // When we are attempting to stylus buttons from an external stylus being merged with touches
2875 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2876 template <typename T = UinputStylusDevice>
2877 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2878 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2879 mStylus = mStylusDeviceLifecycleTracker.get();
2880 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2881 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2882 const auto info = findDeviceByName(mStylus->getName());
2883 ASSERT_TRUE(info);
2884 mStylusInfo = *info;
2885 }
2886
2887 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2888
2889 // Hide the base class's device to expose it with a different name for readability.
2890 using TouchIntegrationTest::mDevice;
2891 using TouchIntegrationTest::mDeviceInfo;
2892};
2893
2894using StylusButtonIntegrationTestTypes =
2895 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2896TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2897
2898TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
2899 const auto stylusId = TestFixture::mStylusInfo.getId();
2900
2901 TestFixture::mStylus->pressKey(BTN_STYLUS);
2902 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2903 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2904 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2905
2906 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2907 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002908 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002909 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002910}
2911
Prabir Pradhan124ea442022-10-28 20:27:44 +00002912TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
2913 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2914 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2915 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002916
2917 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002918 TestFixture::mStylus->pressKey(BTN_STYLUS);
2919 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002920 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002921 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002922
2923 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002924 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2925 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2926 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2927 TestFixture::mTouchscreen->sendDown(centerPoint);
2928 TestFixture::mTouchscreen->sendSync();
2929 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002930 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2931 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002932 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2933 WithDeviceId(touchscreenId))));
2934 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002935 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2936 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002937 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2938 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002939
Prabir Pradhan124ea442022-10-28 20:27:44 +00002940 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2941 TestFixture::mTouchscreen->sendSync();
2942 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002943 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002944 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2945 WithDeviceId(touchscreenId))));
2946 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002947 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002948 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2949 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002950
2951 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002952 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2953 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002954 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002955 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002956}
2957
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002958TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
2959 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2960 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2961 const auto stylusId = TestFixture::mStylusInfo.getId();
2962 auto toolTypeDevice =
2963 AllOf(WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithDeviceId(touchscreenId));
2964
2965 // Press the stylus button.
2966 TestFixture::mStylus->pressKey(BTN_STYLUS);
2967 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2968 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2969 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2970
2971 // Start hovering with the stylus.
2972 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2973 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2974 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2975 TestFixture::mTouchscreen->sendMove(centerPoint);
2976 TestFixture::mTouchscreen->sendSync();
2977 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2978 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2979 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2980 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2981 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2982 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2983 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2984 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2985 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2986
2987 // Touch down with the stylus.
2988 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2989 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2990 TestFixture::mTouchscreen->sendDown(centerPoint);
2991 TestFixture::mTouchscreen->sendSync();
2992 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2993 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2994 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2995
2996 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2997 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2998 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2999
3000 // Stop touching with the stylus, and start hovering.
3001 TestFixture::mTouchscreen->sendUp();
3002 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
3003 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
3004 TestFixture::mTouchscreen->sendMove(centerPoint);
3005 TestFixture::mTouchscreen->sendSync();
3006 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3007 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
3008 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
3009 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3010 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3011 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
3012 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3013 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3014 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
3015
3016 // Stop hovering.
3017 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
3018 TestFixture::mTouchscreen->sendSync();
3019 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3020 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
3021 WithButtonState(0))));
3022 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
3023 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3024 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3025 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
3026
3027 // Release the stylus button.
3028 TestFixture::mStylus->releaseKey(BTN_STYLUS);
3029 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
3030 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
3031 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
3032}
3033
Prabir Pradhan124ea442022-10-28 20:27:44 +00003034TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
3035 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
3036 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
3037 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003038
3039 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00003040 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
3041 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
3042 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
3043 TestFixture::mTouchscreen->sendDown(centerPoint);
3044 TestFixture::mTouchscreen->sendSync();
3045 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003046 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003047 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3048 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003049
3050 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00003051 TestFixture::mStylus->pressKey(BTN_STYLUS);
3052 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003053 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003054 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
3055 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003056 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
3057 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003058 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
3059 WithDeviceId(touchscreenId))));
3060 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003061 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
3062 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003063 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
3064 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003065
Prabir Pradhan124ea442022-10-28 20:27:44 +00003066 TestFixture::mStylus->releaseKey(BTN_STYLUS);
3067 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003068 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003069 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
3070 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003071 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003072 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3073 WithDeviceId(touchscreenId))));
3074 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003075 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003076 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3077 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003078
3079 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00003080 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
3081 TestFixture::mTouchscreen->sendSync();
3082 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003083 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003084 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3085 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003086}
3087
Prabir Pradhan484d55a2022-10-14 23:17:16 +00003088// --- ExternalStylusIntegrationTest ---
3089
3090// Verify the behavior of an external stylus. An external stylus can report pressure or button
3091// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
3092// ongoing stylus gesture that is being emitted by the touchscreen.
3093using ExternalStylusIntegrationTest = TouchIntegrationTest;
3094
3095TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
3096 const Point centerPoint = mDevice->getCenterPoint();
3097
3098 // Create an external stylus capable of reporting pressure data that
3099 // should be fused with a touch pointer.
3100 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
3101 createUinputDevice<UinputExternalStylusWithPressure>();
3102 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
3103 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
3104 const auto stylusInfo = findDeviceByName(stylus->getName());
3105 ASSERT_TRUE(stylusInfo);
3106
3107 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
3108
3109 const auto touchscreenId = mDeviceInfo.getId();
3110
3111 // Set a pressure value on the stylus. It doesn't generate any events.
3112 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
3113 stylus->setPressure(100);
3114 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3115
3116 // Start a finger gesture, and ensure it shows up as stylus gesture
3117 // with the pressure set by the external stylus.
3118 mDevice->sendSlot(FIRST_SLOT);
3119 mDevice->sendTrackingId(FIRST_TRACKING_ID);
3120 mDevice->sendToolType(MT_TOOL_FINGER);
3121 mDevice->sendDown(centerPoint);
3122 mDevice->sendSync();
3123 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3124 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3125 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3126 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
3127
3128 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
3129 // event with the updated pressure.
3130 stylus->setPressure(200);
3131 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3132 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
3133 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3134 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
3135
3136 // The external stylus did not generate any events.
3137 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3138 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
3139}
3140
3141TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
3142 const Point centerPoint = mDevice->getCenterPoint();
3143
3144 // Create an external stylus capable of reporting pressure data that
3145 // should be fused with a touch pointer.
3146 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
3147 createUinputDevice<UinputExternalStylusWithPressure>();
3148 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
3149 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
3150 const auto stylusInfo = findDeviceByName(stylus->getName());
3151 ASSERT_TRUE(stylusInfo);
3152
3153 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
3154
3155 const auto touchscreenId = mDeviceInfo.getId();
3156
3157 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
3158 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00003159 // Send a non-zero value first to prevent the kernel from consuming the zero event.
3160 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00003161 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00003162 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00003163
3164 // Start a finger gesture. The touch device will withhold generating any touches for
3165 // up to 72 milliseconds while waiting for pressure data from the external stylus.
3166 mDevice->sendSlot(FIRST_SLOT);
3167 mDevice->sendTrackingId(FIRST_TRACKING_ID);
3168 mDevice->sendToolType(MT_TOOL_FINGER);
3169 mDevice->sendDown(centerPoint);
3170 auto waitUntil = std::chrono::system_clock::now() +
3171 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
3172 mDevice->sendSync();
3173 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
3174
3175 // Since the external stylus did not report a pressure value within the timeout,
3176 // it shows up as a finger pointer.
3177 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3178 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3179 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithDeviceId(touchscreenId),
3180 WithPressure(1.f))));
3181
3182 // Change the pressure on the external stylus. Since the pressure was not present at the start
3183 // of the gesture, it is ignored for now.
3184 stylus->setPressure(200);
3185 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3186
3187 // Finish the finger gesture.
3188 mDevice->sendTrackingId(INVALID_TRACKING_ID);
3189 mDevice->sendSync();
3190 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3191 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
3192 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
3193
3194 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
3195 mDevice->sendTrackingId(FIRST_TRACKING_ID);
3196 mDevice->sendToolType(MT_TOOL_FINGER);
3197 mDevice->sendDown(centerPoint);
3198 mDevice->sendSync();
3199 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3200 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3201 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3202 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
3203
3204 // The external stylus did not generate any events.
3205 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3206 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
3207}
3208
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00003209TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
3210 const Point centerPoint = mDevice->getCenterPoint();
3211
3212 // Create an external stylus device that does not support pressure. It should not affect any
3213 // touch pointers.
3214 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
3215 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
3216 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
3217 const auto stylusInfo = findDeviceByName(stylus->getName());
3218 ASSERT_TRUE(stylusInfo);
3219
3220 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
3221
3222 const auto touchscreenId = mDeviceInfo.getId();
3223
3224 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
3225 // pressure data from the external stylus.
3226 mDevice->sendSlot(FIRST_SLOT);
3227 mDevice->sendTrackingId(FIRST_TRACKING_ID);
3228 mDevice->sendToolType(MT_TOOL_FINGER);
3229 mDevice->sendDown(centerPoint);
3230 auto waitUntil = std::chrono::system_clock::now() +
3231 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
3232 mDevice->sendSync();
3233 ASSERT_NO_FATAL_FAILURE(
3234 mTestListener
3235 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3236 WithToolType(
3237 AMOTION_EVENT_TOOL_TYPE_FINGER),
3238 WithButtonState(0),
3239 WithDeviceId(touchscreenId),
3240 WithPressure(1.f)),
3241 waitUntil));
3242
3243 // The external stylus did not generate any events.
3244 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3245 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
3246}
3247
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08003249class InputDeviceTest : public testing::Test {
3250protected:
3251 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08003252 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003253 static const int32_t DEVICE_ID;
3254 static const int32_t DEVICE_GENERATION;
3255 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003256 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003257 static const int32_t EVENTHUB_ID;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003258 static const std::string DEVICE_BLUETOOTH_ADDRESS;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003259
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003260 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003262 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08003263 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00003264 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003265
Chris Yea52ade12020-08-27 16:49:20 -07003266 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003267 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003268 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003269 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08003270 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003271 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003272 InputDeviceIdentifier identifier;
3273 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08003274 identifier.location = DEVICE_LOCATION;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003275 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
arthurhungdcef2dc2020-08-11 14:47:50 +08003276 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003277 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08003278 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003279 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003280 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003281 }
3282
Chris Yea52ade12020-08-27 16:49:20 -07003283 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003284 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003286 }
3287};
3288
3289const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003290const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003291const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003292const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
3293const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003294const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07003295 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003296const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003297const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
Michael Wrightd02c5b62014-02-10 15:10:22 -08003298
3299TEST_F(InputDeviceTest, ImmutableProperties) {
3300 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003301 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003302 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003303}
3304
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00003305TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
3306 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
3307
3308 // Configuration
3309 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
3310 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003311 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00003312
3313 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
3314}
3315
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003316TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
3317 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07003318}
3319
Michael Wrightd02c5b62014-02-10 15:10:22 -08003320TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
3321 // Configuration.
3322 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003323 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003324
3325 // Reset.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003326 unused += mDevice->reset(ARBITRARY_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327
3328 NotifyDeviceResetArgs resetArgs;
3329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3330 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3331 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3332
3333 // Metadata.
3334 ASSERT_TRUE(mDevice->isIgnored());
3335 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
3336
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00003337 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003339 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
3341 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
3342
3343 // State queries.
3344 ASSERT_EQ(0, mDevice->getMetaState());
3345
3346 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
3347 << "Ignored device should return unknown key code state.";
3348 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
3349 << "Ignored device should return unknown scan code state.";
3350 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
3351 << "Ignored device should return unknown switch state.";
3352
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003353 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08003354 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003355 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003356 << "Ignored device should never mark any key codes.";
3357 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
3358 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
3359}
3360
3361TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
3362 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003363 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003365 FakeInputMapper& mapper1 =
3366 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003367 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3368 mapper1.setMetaState(AMETA_ALT_ON);
3369 mapper1.addSupportedKeyCode(AKEYCODE_A);
3370 mapper1.addSupportedKeyCode(AKEYCODE_B);
3371 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
3372 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
3373 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
3374 mapper1.setScanCodeState(3, AKEY_STATE_UP);
3375 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003376
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003377 FakeInputMapper& mapper2 =
3378 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003379 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003380
3381 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003382 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003383
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003384 std::string propertyValue;
3385 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003386 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003387 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003388
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003389 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
3390 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003391
3392 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003393 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003394 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
3395 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003396
3397 NotifyDeviceResetArgs resetArgs;
3398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3399 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3400 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3401
3402 // Metadata.
3403 ASSERT_FALSE(mDevice->isIgnored());
3404 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
3405
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00003406 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003407 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003408 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003409 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
3410 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
3411
3412 // State queries.
3413 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
3414 << "Should query mappers and combine meta states.";
3415
3416 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3417 << "Should return unknown key code state when source not supported.";
3418 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3419 << "Should return unknown scan code state when source not supported.";
3420 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3421 << "Should return unknown switch state when source not supported.";
3422
3423 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
3424 << "Should query mapper when source is supported.";
3425 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
3426 << "Should query mapper when source is supported.";
3427 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
3428 << "Should query mapper when source is supported.";
3429
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003430 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08003431 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003432 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003433 << "Should do nothing when source is unsupported.";
3434 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
3435 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
3436 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
3437 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
3438
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003439 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440 << "Should query mapper when source is supported.";
3441 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
3442 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
3443 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
3444 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
3445
3446 // Event handling.
3447 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003448 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003449 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003450
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003451 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
3452 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003453}
3454
Arthur Hung2c9a3342019-07-23 14:18:59 +08003455// A single input device is associated with a specific display. Check that:
3456// 1. Device is disabled if the viewport corresponding to the associated display is not found
3457// 2. Device is disabled when setEnabled API is called
3458TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003459 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003460
3461 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003462 std::list<NotifyArgs> unused =
3463 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003464
3465 // Device should be enabled by default.
3466 ASSERT_TRUE(mDevice->isEnabled());
3467
3468 // Prepare associated info.
3469 constexpr uint8_t hdmi = 1;
3470 const std::string UNIQUE_ID = "local:1";
3471
3472 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003473 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3474 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003475 // Device should be disabled because it is associated with a specific display via
3476 // input port <-> display port association, but the corresponding display is not found
3477 ASSERT_FALSE(mDevice->isEnabled());
3478
3479 // Prepare displays.
3480 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003481 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
3482 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003483 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3484 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003485 ASSERT_TRUE(mDevice->isEnabled());
3486
3487 // Device should be disabled after set disable.
3488 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003489 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3490 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003491 ASSERT_FALSE(mDevice->isEnabled());
3492
3493 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003494 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3495 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003496 ASSERT_FALSE(mDevice->isEnabled());
3497}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498
Christine Franks1ba71cc2021-04-07 14:37:42 -07003499TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
3500 // Device should be enabled by default.
3501 mFakePolicy->clearViewports();
3502 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003503 std::list<NotifyArgs> unused =
3504 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003505 ASSERT_TRUE(mDevice->isEnabled());
3506
3507 // Device should be disabled because it is associated with a specific display, but the
3508 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08003509 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003510 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3511 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003512 ASSERT_FALSE(mDevice->isEnabled());
3513
3514 // Device should be enabled when a display is found.
3515 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3516 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3517 NO_PORT, ViewportType::INTERNAL);
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_TRUE(mDevice->isEnabled());
3521
3522 // Device should be disabled after set disable.
3523 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003524 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3525 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003526 ASSERT_FALSE(mDevice->isEnabled());
3527
3528 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003529 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3530 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003531 ASSERT_FALSE(mDevice->isEnabled());
3532}
3533
Christine Franks2a2293c2022-01-18 11:51:16 -08003534TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
3535 mFakePolicy->clearViewports();
3536 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003537 std::list<NotifyArgs> unused =
3538 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08003539
Christine Franks2a2293c2022-01-18 11:51:16 -08003540 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
3541 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3542 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3543 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003544 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3545 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08003546 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
3547}
3548
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07003549/**
3550 * This test reproduces a crash caused by a dangling reference that remains after device is added
3551 * and removed. The reference is accessed in InputDevice::dump(..);
3552 */
3553TEST_F(InputDeviceTest, DumpDoesNotCrash) {
3554 constexpr int32_t TEST_EVENTHUB_ID = 10;
3555 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
3556
3557 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
3558 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
3559 device.removeEventHubDevice(TEST_EVENTHUB_ID);
3560 std::string dumpStr, eventHubDevStr;
3561 device.dump(dumpStr, eventHubDevStr);
3562}
3563
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003564TEST_F(InputDeviceTest, GetBluetoothAddress) {
3565 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
3566 ASSERT_TRUE(address);
3567 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
3568}
3569
Michael Wrightd02c5b62014-02-10 15:10:22 -08003570// --- InputMapperTest ---
3571
3572class InputMapperTest : public testing::Test {
3573protected:
3574 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003575 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003576 static const int32_t DEVICE_ID;
3577 static const int32_t DEVICE_GENERATION;
3578 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003579 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003580 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003581
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003582 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003583 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003584 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08003585 std::unique_ptr<InstrumentedInputReader> mReader;
3586 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003587
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003588 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003589 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003590 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003591 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08003592 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003593 *mFakeListener);
arthurhungdcef2dc2020-08-11 14:47:50 +08003594 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003595 // Consume the device reset notification generated when adding a new device.
3596 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003597 }
3598
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003599 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003600 SetUp(DEVICE_CLASSES);
3601 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003602
Chris Yea52ade12020-08-27 16:49:20 -07003603 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003604 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003605 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003606 }
3607
3608 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003609 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003610 }
3611
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003612 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003613 if (!changes ||
3614 (changes &
3615 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3616 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003617 mReader->requestRefreshConfiguration(changes);
3618 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003619 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003620 std::list<NotifyArgs> out =
3621 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003622 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003623 for (const NotifyArgs& args : out) {
3624 mFakeListener->notify(args);
3625 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003626 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003627 return out;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003628 }
3629
arthurhungdcef2dc2020-08-11 14:47:50 +08003630 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3631 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003632 ftl::Flags<InputDeviceClass> classes) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003633 InputDeviceIdentifier identifier;
3634 identifier.name = name;
3635 identifier.location = location;
3636 std::shared_ptr<InputDevice> device =
3637 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3638 identifier);
3639 mReader->pushNextDevice(device);
3640 mFakeEventHub->addDevice(eventHubId, name, classes);
3641 mReader->loopOnce();
3642 return device;
3643 }
3644
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003645 template <class T, typename... Args>
3646 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003647 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003648 configureDevice(0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003649 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
3650 resetArgList += mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003651 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003652 for (const NotifyArgs& loopArgs : resetArgList) {
3653 mFakeListener->notify(loopArgs);
3654 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003655 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003656 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003657 }
3658
3659 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003660 int32_t orientation, const std::string& uniqueId,
3661 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003662 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3663 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003664 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3665 }
3666
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003667 void clearViewports() {
3668 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003669 }
3670
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003671 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
3672 int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003673 RawEvent event;
3674 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003675 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003676 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003677 event.type = type;
3678 event.code = code;
3679 event.value = value;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003680 std::list<NotifyArgs> processArgList = mapper.process(&event);
3681 for (const NotifyArgs& args : processArgList) {
3682 mFakeListener->notify(args);
3683 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003684 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003685 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003686 return processArgList;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003687 }
3688
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00003689 void resetMapper(InputMapper& mapper, nsecs_t when) {
3690 const auto resetArgs = mapper.reset(when);
3691 for (const auto args : resetArgs) {
3692 mFakeListener->notify(args);
3693 }
3694 // Loop the reader to flush the input listener queue.
3695 mReader->loopOnce();
3696 }
3697
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00003698 std::list<NotifyArgs> handleTimeout(InputMapper& mapper, nsecs_t when) {
3699 std::list<NotifyArgs> generatedArgs = mapper.timeoutExpired(when);
3700 for (const NotifyArgs& args : generatedArgs) {
3701 mFakeListener->notify(args);
3702 }
3703 // Loop the reader to flush the input listener queue.
3704 mReader->loopOnce();
3705 return generatedArgs;
3706 }
3707
Michael Wrightd02c5b62014-02-10 15:10:22 -08003708 static void assertMotionRange(const InputDeviceInfo& info,
3709 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3710 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003711 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003712 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3713 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3714 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3715 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3716 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3717 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3718 }
3719
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003720 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3721 float size, float touchMajor, float touchMinor, float toolMajor,
3722 float toolMinor, float orientation, float distance,
3723 float scaledAxisEpsilon = 1.f) {
3724 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3725 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003726 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3727 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003728 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3729 scaledAxisEpsilon);
3730 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3731 scaledAxisEpsilon);
3732 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3733 scaledAxisEpsilon);
3734 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3735 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003736 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3737 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3738 }
3739
Michael Wright17db18e2020-06-26 20:51:44 +01003740 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003741 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003742 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003743 ASSERT_NEAR(x, actualX, 1);
3744 ASSERT_NEAR(y, actualY, 1);
3745 }
3746};
3747
3748const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003749const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003750const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003751const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3752const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003753const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3754 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003755const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003756
3757// --- SwitchInputMapperTest ---
3758
3759class SwitchInputMapperTest : public InputMapperTest {
3760protected:
3761};
3762
3763TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003764 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003765
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003766 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003767}
3768
3769TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003770 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003771
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003772 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003773 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003774
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003775 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003776 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003777}
3778
3779TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003780 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003781 std::list<NotifyArgs> out;
3782 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3783 ASSERT_TRUE(out.empty());
3784 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3785 ASSERT_TRUE(out.empty());
3786 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3787 ASSERT_TRUE(out.empty());
3788 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003789
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003790 ASSERT_EQ(1u, out.size());
3791 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003792 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003793 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3794 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003795 args.switchMask);
3796 ASSERT_EQ(uint32_t(0), args.policyFlags);
3797}
3798
Chris Ye87143712020-11-10 05:05:58 +00003799// --- VibratorInputMapperTest ---
3800class VibratorInputMapperTest : public InputMapperTest {
3801protected:
3802 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3803};
3804
3805TEST_F(VibratorInputMapperTest, GetSources) {
3806 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3807
3808 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3809}
3810
3811TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3812 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3813
3814 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3815}
3816
3817TEST_F(VibratorInputMapperTest, Vibrate) {
3818 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003819 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003820 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3821
3822 VibrationElement pattern(2);
3823 VibrationSequence sequence(2);
3824 pattern.duration = std::chrono::milliseconds(200);
3825 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3826 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3827 sequence.addElement(pattern);
3828 pattern.duration = std::chrono::milliseconds(500);
3829 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3830 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3831 sequence.addElement(pattern);
3832
3833 std::vector<int64_t> timings = {0, 1};
3834 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3835
3836 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003837 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003838 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003839 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003840 // Verify vibrator state listener was notified.
3841 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003842 ASSERT_EQ(1u, out.size());
3843 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3844 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3845 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003846 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003847 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003848 ASSERT_FALSE(mapper.isVibrating());
3849 // Verify vibrator state listener was notified.
3850 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003851 ASSERT_EQ(1u, out.size());
3852 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3853 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3854 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003855}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003856
Chris Yef59a2f42020-10-16 12:55:26 -07003857// --- SensorInputMapperTest ---
3858
3859class SensorInputMapperTest : public InputMapperTest {
3860protected:
3861 static const int32_t ACCEL_RAW_MIN;
3862 static const int32_t ACCEL_RAW_MAX;
3863 static const int32_t ACCEL_RAW_FUZZ;
3864 static const int32_t ACCEL_RAW_FLAT;
3865 static const int32_t ACCEL_RAW_RESOLUTION;
3866
3867 static const int32_t GYRO_RAW_MIN;
3868 static const int32_t GYRO_RAW_MAX;
3869 static const int32_t GYRO_RAW_FUZZ;
3870 static const int32_t GYRO_RAW_FLAT;
3871 static const int32_t GYRO_RAW_RESOLUTION;
3872
3873 static const float GRAVITY_MS2_UNIT;
3874 static const float DEGREE_RADIAN_UNIT;
3875
3876 void prepareAccelAxes();
3877 void prepareGyroAxes();
3878 void setAccelProperties();
3879 void setGyroProperties();
3880 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3881};
3882
3883const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3884const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3885const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3886const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3887const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3888
3889const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3890const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3891const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3892const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3893const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3894
3895const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3896const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3897
3898void SensorInputMapperTest::prepareAccelAxes() {
3899 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3900 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3901 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3902 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3903 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3904 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3905}
3906
3907void SensorInputMapperTest::prepareGyroAxes() {
3908 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3909 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3910 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3911 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3912 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3913 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3914}
3915
3916void SensorInputMapperTest::setAccelProperties() {
3917 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3918 /* sensorDataIndex */ 0);
3919 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3920 /* sensorDataIndex */ 1);
3921 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3922 /* sensorDataIndex */ 2);
3923 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3924 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3925 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3926 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3927 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3928}
3929
3930void SensorInputMapperTest::setGyroProperties() {
3931 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3932 /* sensorDataIndex */ 0);
3933 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3934 /* sensorDataIndex */ 1);
3935 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3936 /* sensorDataIndex */ 2);
3937 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3938 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3939 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3940 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3941 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3942}
3943
3944TEST_F(SensorInputMapperTest, GetSources) {
3945 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3946
3947 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3948}
3949
3950TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3951 setAccelProperties();
3952 prepareAccelAxes();
3953 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3954
3955 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3956 std::chrono::microseconds(10000),
3957 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003958 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3960 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3961 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3962 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003964
3965 NotifySensorArgs args;
3966 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3967 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3968 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3969
3970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3971 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3972 ASSERT_EQ(args.deviceId, DEVICE_ID);
3973 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3974 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3975 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3976 ASSERT_EQ(args.values, values);
3977 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3978}
3979
3980TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3981 setGyroProperties();
3982 prepareGyroAxes();
3983 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3984
3985 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3986 std::chrono::microseconds(10000),
3987 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003988 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003989 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3990 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003994
3995 NotifySensorArgs args;
3996 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3997 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3998 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3999
4000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
4001 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
4002 ASSERT_EQ(args.deviceId, DEVICE_ID);
4003 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
4004 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
4005 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
4006 ASSERT_EQ(args.values, values);
4007 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
4008}
4009
Michael Wrightd02c5b62014-02-10 15:10:22 -08004010// --- KeyboardInputMapperTest ---
4011
4012class KeyboardInputMapperTest : public InputMapperTest {
4013protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004014 const std::string UNIQUE_ID = "local:0";
4015
4016 void prepareDisplay(int32_t orientation);
4017
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004018 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08004019 int32_t originalKeyCode, int32_t rotatedKeyCode,
4020 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021};
4022
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004023/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
4024 * orientation.
4025 */
4026void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004027 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4028 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004029}
4030
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004031void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08004032 int32_t originalScanCode, int32_t originalKeyCode,
4033 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004034 NotifyKeyArgs args;
4035
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4038 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4039 ASSERT_EQ(originalScanCode, args.scanCode);
4040 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004041 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004042
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4045 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4046 ASSERT_EQ(originalScanCode, args.scanCode);
4047 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004048 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049}
4050
Michael Wrightd02c5b62014-02-10 15:10:22 -08004051TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004052 KeyboardInputMapper& mapper =
4053 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4054 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004055
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004056 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004057}
4058
4059TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
4060 const int32_t USAGE_A = 0x070004;
4061 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004062 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4063 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07004064 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
4065 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
4066 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004067
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004068 KeyboardInputMapper& mapper =
4069 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4070 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004071 // Initial metastate is AMETA_NONE.
4072 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004073
4074 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004075 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004076 NotifyKeyArgs args;
4077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4078 ASSERT_EQ(DEVICE_ID, args.deviceId);
4079 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4080 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4081 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4082 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4083 ASSERT_EQ(KEY_HOME, args.scanCode);
4084 ASSERT_EQ(AMETA_NONE, args.metaState);
4085 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4086 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4087 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4088
4089 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004090 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4092 ASSERT_EQ(DEVICE_ID, args.deviceId);
4093 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4094 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4095 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4096 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4097 ASSERT_EQ(KEY_HOME, args.scanCode);
4098 ASSERT_EQ(AMETA_NONE, args.metaState);
4099 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4100 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4101 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4102
4103 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004104 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
4105 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4107 ASSERT_EQ(DEVICE_ID, args.deviceId);
4108 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4109 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4110 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4111 ASSERT_EQ(AKEYCODE_A, args.keyCode);
4112 ASSERT_EQ(0, args.scanCode);
4113 ASSERT_EQ(AMETA_NONE, args.metaState);
4114 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4115 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4116 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4117
4118 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
4120 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4122 ASSERT_EQ(DEVICE_ID, args.deviceId);
4123 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4124 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4125 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4126 ASSERT_EQ(AKEYCODE_A, args.keyCode);
4127 ASSERT_EQ(0, args.scanCode);
4128 ASSERT_EQ(AMETA_NONE, args.metaState);
4129 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4130 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4131 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4132
4133 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004134 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
4135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4137 ASSERT_EQ(DEVICE_ID, args.deviceId);
4138 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4139 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4140 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4141 ASSERT_EQ(0, args.keyCode);
4142 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
4143 ASSERT_EQ(AMETA_NONE, args.metaState);
4144 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4145 ASSERT_EQ(0U, args.policyFlags);
4146 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4147
4148 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
4150 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4152 ASSERT_EQ(DEVICE_ID, args.deviceId);
4153 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4154 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4155 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4156 ASSERT_EQ(0, args.keyCode);
4157 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
4158 ASSERT_EQ(AMETA_NONE, args.metaState);
4159 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4160 ASSERT_EQ(0U, args.policyFlags);
4161 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4162}
4163
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004164/**
4165 * Ensure that the readTime is set to the time when the EV_KEY is received.
4166 */
4167TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
4168 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4169
4170 KeyboardInputMapper& mapper =
4171 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4172 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4173 NotifyKeyArgs args;
4174
4175 // Key down
4176 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
4177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4178 ASSERT_EQ(12, args.readTime);
4179
4180 // Key up
4181 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
4182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4183 ASSERT_EQ(15, args.readTime);
4184}
4185
Michael Wrightd02c5b62014-02-10 15:10:22 -08004186TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004187 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
4188 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07004189 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
4190 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
4191 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004192
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004193 KeyboardInputMapper& mapper =
4194 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4195 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004196
Arthur Hung95f68612022-04-07 14:08:22 +08004197 // Initial metastate is AMETA_NONE.
4198 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199
4200 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202 NotifyKeyArgs args;
4203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4204 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004205 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08004206 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004207
4208 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004209 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4211 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004212 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213
4214 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004215 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4217 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004218 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219
4220 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004221 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4223 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004224 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08004225 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004226}
4227
4228TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004229 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4230 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4231 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4232 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004234 KeyboardInputMapper& mapper =
4235 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4236 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004238 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4240 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
4241 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4242 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
4243 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4244 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
4245 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4246 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
4247}
4248
4249TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004250 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4251 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4252 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4253 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254
Michael Wrightd02c5b62014-02-10 15:10:22 -08004255 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004256 KeyboardInputMapper& mapper =
4257 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4258 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004259
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004260 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004261 ASSERT_NO_FATAL_FAILURE(
4262 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4263 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4264 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4265 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4266 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4267 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4268 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004270 clearViewports();
4271 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004272 ASSERT_NO_FATAL_FAILURE(
4273 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4274 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4275 AKEYCODE_DPAD_UP, DISPLAY_ID));
4276 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4277 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4278 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4279 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004281 clearViewports();
4282 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004283 ASSERT_NO_FATAL_FAILURE(
4284 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4285 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4286 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4287 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4288 AKEYCODE_DPAD_UP, DISPLAY_ID));
4289 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4290 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004291
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004292 clearViewports();
4293 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004294 ASSERT_NO_FATAL_FAILURE(
4295 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4296 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4297 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4298 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4299 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4300 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4301 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302
4303 // Special case: if orientation changes while key is down, we still emit the same keycode
4304 // in the key up as we did in the key down.
4305 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004306 clearViewports();
4307 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4310 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4311 ASSERT_EQ(KEY_UP, args.scanCode);
4312 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
4313
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004314 clearViewports();
4315 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4318 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4319 ASSERT_EQ(KEY_UP, args.scanCode);
4320 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
4321}
4322
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004323TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
4324 // If the keyboard is not orientation aware,
4325 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004326 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004327
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004328 KeyboardInputMapper& mapper =
4329 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4330 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004331 NotifyKeyArgs args;
4332
4333 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004334 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4338 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
4339
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004340 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004341 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004343 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4345 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
4346}
4347
4348TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
4349 // If the keyboard is orientation aware,
4350 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004351 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004352
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004353 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004354 KeyboardInputMapper& mapper =
4355 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4356 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004357 NotifyKeyArgs args;
4358
4359 // Display id should be ADISPLAY_ID_NONE without any display configuration.
4360 // ^--- already checked by the previous test
4361
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004362 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004363 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004364 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004366 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4368 ASSERT_EQ(DISPLAY_ID, args.displayId);
4369
4370 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004371 clearViewports();
4372 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004373 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004374 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4378 ASSERT_EQ(newDisplayId, args.displayId);
4379}
4380
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004382 KeyboardInputMapper& mapper =
4383 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4384 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004385
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004386 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004387 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004388
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004389 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004390 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004391}
4392
Philip Junker4af3b3d2021-12-14 10:36:55 +01004393TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
4394 KeyboardInputMapper& mapper =
4395 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4396 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4397
4398 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
4399 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
4400 << "If a mapping is available, the result is equal to the mapping";
4401
4402 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
4403 << "If no mapping is available, the result is the key location";
4404}
4405
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004407 KeyboardInputMapper& mapper =
4408 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4409 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004410
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004411 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004412 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004413
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004414 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004415 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004416}
4417
4418TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004419 KeyboardInputMapper& mapper =
4420 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4421 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004423 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424
Michael Wrightd02c5b62014-02-10 15:10:22 -08004425 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004426 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427 ASSERT_TRUE(flags[0]);
4428 ASSERT_FALSE(flags[1]);
4429}
4430
4431TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004432 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4433 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4434 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4435 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4436 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4437 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004438
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004439 KeyboardInputMapper& mapper =
4440 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4441 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004442 // Initial metastate is AMETA_NONE.
4443 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004444
4445 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004446 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4447 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4448 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004449
4450 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004451 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4452 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004453 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4454 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4455 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004456 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004457
4458 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4460 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004461 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4462 ASSERT_TRUE(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 | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004465
4466 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004467 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004469 ASSERT_FALSE(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_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473
4474 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 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_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004480 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004481
4482 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004483 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4484 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004485 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4486 ASSERT_FALSE(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_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004489
4490 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4492 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 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_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004496 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497}
4498
Chris Yea52ade12020-08-27 16:49:20 -07004499TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
4500 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
4501 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
4502 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
4503 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
4504
4505 KeyboardInputMapper& mapper =
4506 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4507 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4508
Chris Yea52ade12020-08-27 16:49:20 -07004509 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004510 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07004511 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4512 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
4513 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
4514 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4515
4516 NotifyKeyArgs args;
4517 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07004519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4520 ASSERT_EQ(AMETA_NONE, args.metaState);
4521 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4522 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4523 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4524
4525 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004526 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
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_UP, args.action);
4531 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4532}
4533
Arthur Hung2c9a3342019-07-23 14:18:59 +08004534TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
4535 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004536 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4537 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4538 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4539 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004540
4541 // keyboard 2.
4542 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08004543 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08004544 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004545 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08004546 std::shared_ptr<InputDevice> device2 =
4547 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004548 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08004549
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004550 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4551 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4552 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4553 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004554
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004555 KeyboardInputMapper& mapper =
4556 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4557 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004558
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004559 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004560 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004561 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004562 std::list<NotifyArgs> unused =
4563 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4564 0 /*changes*/);
4565 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004566
4567 // Prepared displays and associated info.
4568 constexpr uint8_t hdmi1 = 0;
4569 constexpr uint8_t hdmi2 = 1;
4570 const std::string SECONDARY_UNIQUE_ID = "local:1";
4571
4572 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
4573 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
4574
4575 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004576 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4577 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004578 ASSERT_FALSE(device2->isEnabled());
4579
4580 // Prepare second display.
4581 constexpr int32_t newDisplayId = 2;
4582 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004583 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004584 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004585 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004586 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004587 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4588 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004589
4590 // Device should be enabled after the associated display is found.
4591 ASSERT_TRUE(mDevice->isEnabled());
4592 ASSERT_TRUE(device2->isEnabled());
4593
4594 // Test pad key events
4595 ASSERT_NO_FATAL_FAILURE(
4596 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4597 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4598 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4599 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4600 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4601 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4602 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4603
4604 ASSERT_NO_FATAL_FAILURE(
4605 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
4606 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4607 AKEYCODE_DPAD_RIGHT, newDisplayId));
4608 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4609 AKEYCODE_DPAD_DOWN, newDisplayId));
4610 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4611 AKEYCODE_DPAD_LEFT, newDisplayId));
4612}
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613
arthurhungc903df12020-08-11 15:08:42 +08004614TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
4615 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4616 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4617 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4618 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4619 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4620 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4621
4622 KeyboardInputMapper& mapper =
4623 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4624 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004625 // Initial metastate is AMETA_NONE.
4626 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004627
4628 // Initialization should have turned all of the lights off.
4629 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4630 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4631 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4632
4633 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004636 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4637 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4638
4639 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004640 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004642 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4643 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
4644
4645 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004646 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004648 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4649 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4650
4651 mFakeEventHub->removeDevice(EVENTHUB_ID);
4652 mReader->loopOnce();
4653
4654 // keyboard 2 should default toggle keys.
4655 const std::string USB2 = "USB2";
4656 const std::string DEVICE_NAME2 = "KEYBOARD2";
4657 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4658 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4659 std::shared_ptr<InputDevice> device2 =
4660 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004661 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004662 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4663 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4664 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4665 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4666 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4667 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4668
arthurhung6fe95782020-10-05 22:41:16 +08004669 KeyboardInputMapper& mapper2 =
4670 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4671 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004672 std::list<NotifyArgs> unused =
4673 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4674 0 /*changes*/);
4675 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08004676
4677 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4678 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4679 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004680 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4681 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004682}
4683
Arthur Hungcb40a002021-08-03 14:31:01 +00004684TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4685 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4686 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4687 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4688
4689 // Suppose we have two mappers. (DPAD + KEYBOARD)
4690 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4691 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4692 KeyboardInputMapper& mapper =
4693 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4694 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004695 // Initial metastate is AMETA_NONE.
4696 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004697
4698 mReader->toggleCapsLockState(DEVICE_ID);
4699 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4700}
4701
Arthur Hungfb3cc112022-04-13 07:39:50 +00004702TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4703 // keyboard 1.
4704 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4705 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4706 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4707 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4708 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4709 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4710
4711 KeyboardInputMapper& mapper1 =
4712 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4713 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4714
4715 // keyboard 2.
4716 const std::string USB2 = "USB2";
4717 const std::string DEVICE_NAME2 = "KEYBOARD2";
4718 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4719 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4720 std::shared_ptr<InputDevice> device2 =
4721 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4722 ftl::Flags<InputDeviceClass>(0));
4723 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4724 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4725 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4726 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4727 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4728 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4729
4730 KeyboardInputMapper& mapper2 =
4731 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4732 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004733 std::list<NotifyArgs> unused =
4734 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4735 0 /*changes*/);
4736 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004737
Arthur Hung95f68612022-04-07 14:08:22 +08004738 // Initial metastate is AMETA_NONE.
4739 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4740 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4741
4742 // Toggle num lock on and off.
4743 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4744 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004745 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4746 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4747 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4748
4749 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4750 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4751 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4752 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4753 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4754
4755 // Toggle caps lock on and off.
4756 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4757 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4758 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4759 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4760 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4761
4762 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4763 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4764 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4765 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4766 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4767
4768 // Toggle scroll lock on and off.
4769 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4770 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4771 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4772 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4773 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4774
4775 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4776 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4777 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4778 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4779 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4780}
4781
Arthur Hung2141d542022-08-23 07:45:21 +00004782TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4783 const int32_t USAGE_A = 0x070004;
4784 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4785 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4786
4787 KeyboardInputMapper& mapper =
4788 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4789 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4790 // Key down by scan code.
4791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4792 NotifyKeyArgs args;
4793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4794 ASSERT_EQ(DEVICE_ID, args.deviceId);
4795 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4796 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4797 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4798 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4799 ASSERT_EQ(KEY_HOME, args.scanCode);
4800 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4801
4802 // Disable device, it should synthesize cancellation events for down events.
4803 mFakePolicy->addDisabledDevice(DEVICE_ID);
4804 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4805
4806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4807 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4808 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4809 ASSERT_EQ(KEY_HOME, args.scanCode);
4810 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4811}
4812
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004813// --- KeyboardInputMapperTest_ExternalDevice ---
4814
4815class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4816protected:
Chris Yea52ade12020-08-27 16:49:20 -07004817 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004818};
4819
4820TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004821 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4822 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004823
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004824 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4825 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4826 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4827 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004828
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004829 KeyboardInputMapper& mapper =
4830 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4831 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004832
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004833 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004834 NotifyKeyArgs args;
4835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4836 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4837
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004838 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4840 ASSERT_EQ(uint32_t(0), args.policyFlags);
4841
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004842 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4844 ASSERT_EQ(uint32_t(0), args.policyFlags);
4845
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004846 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 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_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4852 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4853
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004854 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4856 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4857}
4858
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004859TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004860 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004861
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004862 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4863 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4864 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004865
Powei Fengd041c5d2019-05-03 17:11:33 -07004866 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004867 KeyboardInputMapper& mapper =
4868 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4869 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004870
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004871 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004872 NotifyKeyArgs args;
4873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4874 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4875
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004876 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4878 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4879
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4882 ASSERT_EQ(uint32_t(0), args.policyFlags);
4883
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004884 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4886 ASSERT_EQ(uint32_t(0), args.policyFlags);
4887
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004888 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4890 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4891
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004892 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4894 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4895}
4896
Michael Wrightd02c5b62014-02-10 15:10:22 -08004897// --- CursorInputMapperTest ---
4898
4899class CursorInputMapperTest : public InputMapperTest {
4900protected:
4901 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4902
Michael Wright17db18e2020-06-26 20:51:44 +01004903 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004904
Chris Yea52ade12020-08-27 16:49:20 -07004905 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004906 InputMapperTest::SetUp();
4907
Michael Wright17db18e2020-06-26 20:51:44 +01004908 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004909 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004910 }
4911
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004912 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4913 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004914
4915 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004916 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4917 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4918 }
4919
4920 void prepareSecondaryDisplay() {
4921 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4922 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4923 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004924 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004925
4926 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4927 float pressure) {
4928 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4929 0.0f, 0.0f, 0.0f, EPSILON));
4930 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004931};
4932
4933const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4934
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004935void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4936 int32_t originalY, int32_t rotatedX,
4937 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938 NotifyMotionArgs args;
4939
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004940 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4941 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4944 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004945 ASSERT_NO_FATAL_FAILURE(
4946 assertCursorPointerCoords(args.pointerCoords[0],
4947 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4948 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004949}
4950
4951TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004952 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004953 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004954
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004955 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004956}
4957
4958TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004959 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004960 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004961
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004962 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004963}
4964
4965TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004967 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004968
4969 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004970 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004971
4972 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004973 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4974 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004975 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4976 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4977
4978 // When the bounds are set, then there should be a valid motion range.
4979 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4980
4981 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004982 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004983
4984 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4985 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4986 1, 800 - 1, 0.0f, 0.0f));
4987 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4988 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4989 2, 480 - 1, 0.0f, 0.0f));
4990 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4991 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4992 0.0f, 1.0f, 0.0f, 0.0f));
4993}
4994
4995TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004996 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004997 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004998
4999 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005000 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005001
5002 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5003 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
5004 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
5005 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5006 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
5007 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
5008 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5009 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
5010 0.0f, 1.0f, 0.0f, 0.0f));
5011}
5012
5013TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005014 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005015 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005016
arthurhungdcef2dc2020-08-11 14:47:50 +08005017 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005018
5019 NotifyMotionArgs args;
5020
5021 // Button press.
5022 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005023 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5024 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5026 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5027 ASSERT_EQ(DEVICE_ID, args.deviceId);
5028 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
5029 ASSERT_EQ(uint32_t(0), args.policyFlags);
5030 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5031 ASSERT_EQ(0, args.flags);
5032 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5033 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
5034 ASSERT_EQ(0, args.edgeFlags);
5035 ASSERT_EQ(uint32_t(1), args.pointerCount);
5036 ASSERT_EQ(0, args.pointerProperties[0].id);
5037 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005038 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005039 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
5040 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
5041 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5042
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5044 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5045 ASSERT_EQ(DEVICE_ID, args.deviceId);
5046 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
5047 ASSERT_EQ(uint32_t(0), args.policyFlags);
5048 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
5049 ASSERT_EQ(0, args.flags);
5050 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5051 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
5052 ASSERT_EQ(0, args.edgeFlags);
5053 ASSERT_EQ(uint32_t(1), args.pointerCount);
5054 ASSERT_EQ(0, args.pointerProperties[0].id);
5055 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005056 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005057 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
5058 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
5059 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5060
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005062 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5063 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5065 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
5066 ASSERT_EQ(DEVICE_ID, args.deviceId);
5067 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
5068 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005069 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
5070 ASSERT_EQ(0, args.flags);
5071 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5072 ASSERT_EQ(0, args.buttonState);
5073 ASSERT_EQ(0, args.edgeFlags);
5074 ASSERT_EQ(uint32_t(1), args.pointerCount);
5075 ASSERT_EQ(0, args.pointerProperties[0].id);
5076 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005077 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005078 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
5079 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
5080 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5081
5082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5083 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
5084 ASSERT_EQ(DEVICE_ID, args.deviceId);
5085 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
5086 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005087 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
5088 ASSERT_EQ(0, args.flags);
5089 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5090 ASSERT_EQ(0, args.buttonState);
5091 ASSERT_EQ(0, args.edgeFlags);
5092 ASSERT_EQ(uint32_t(1), args.pointerCount);
5093 ASSERT_EQ(0, args.pointerProperties[0].id);
5094 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005095 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005096 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
5097 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
5098 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5099}
5100
5101TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005102 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005103 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005104
5105 NotifyMotionArgs args;
5106
5107 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5109 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005112 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
5113 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
5114 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005115
5116 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005117 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
5118 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5120 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005121 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
5122 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123}
5124
5125TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005126 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005127 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005128
5129 NotifyMotionArgs args;
5130
5131 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005132 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5135 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005136 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005137
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5139 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005140 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005141
Michael Wrightd02c5b62014-02-10 15:10:22 -08005142 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005146 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005147 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005148
5149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005150 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005151 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005152}
5153
5154TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005155 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005156 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005157
5158 NotifyMotionArgs args;
5159
5160 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
5163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5166 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005167 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
5168 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
5169 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005170
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5172 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005173 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
5174 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
5175 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005176
Michael Wrightd02c5b62014-02-10 15:10:22 -08005177 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005178 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
5179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
5180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005183 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
5184 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
5185 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005186
5187 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5189 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005191 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005192 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005193
5194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005195 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005196 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005197}
5198
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005199TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005200 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005201 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005202 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5203 // need to be rotated.
5204 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005205 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005206
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005207 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005208 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
5209 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
5210 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
5211 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
5212 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
5213 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
5214 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
5215 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
5216}
5217
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005218TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005219 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005220 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005221 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5222 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005223 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005224
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005225 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005226 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005227 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
5228 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
5229 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
5230 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
5231 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
5232 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
5233 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
5234 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
5235
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005236 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005237 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005238 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
5239 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
5240 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
5241 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
5242 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
5243 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
5244 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
5245 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005246
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005247 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005248 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005249 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
5250 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
5251 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
5252 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
5253 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
5254 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
5255 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
5256 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
5257
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005258 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005259 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005260 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
5261 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
5262 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
5263 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
5264 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
5265 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
5266 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
5267 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005268}
5269
5270TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005271 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005272 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005273
5274 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5275 mFakePointerController->setPosition(100, 200);
5276 mFakePointerController->setButtonState(0);
5277
5278 NotifyMotionArgs motionArgs;
5279 NotifyKeyArgs keyArgs;
5280
5281 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
5283 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5285 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5286 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5287 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005288 ASSERT_NO_FATAL_FAILURE(
5289 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005290
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5292 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5293 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5294 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005295 ASSERT_NO_FATAL_FAILURE(
5296 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005297
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
5299 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005301 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005302 ASSERT_EQ(0, motionArgs.buttonState);
5303 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005304 ASSERT_NO_FATAL_FAILURE(
5305 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005306
5307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005308 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005309 ASSERT_EQ(0, motionArgs.buttonState);
5310 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005311 ASSERT_NO_FATAL_FAILURE(
5312 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005313
5314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005315 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005316 ASSERT_EQ(0, motionArgs.buttonState);
5317 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005318 ASSERT_NO_FATAL_FAILURE(
5319 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005320
5321 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005322 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
5323 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
5324 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5326 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5327 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5328 motionArgs.buttonState);
5329 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5330 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005331 ASSERT_NO_FATAL_FAILURE(
5332 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005333
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5335 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5336 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005341
5342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5343 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5344 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5345 motionArgs.buttonState);
5346 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5347 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005348 ASSERT_NO_FATAL_FAILURE(
5349 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005350
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
5352 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005354 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005355 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5356 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005357 ASSERT_NO_FATAL_FAILURE(
5358 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005359
5360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005362 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5363 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005364 ASSERT_NO_FATAL_FAILURE(
5365 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005366
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005370 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5371 ASSERT_EQ(0, motionArgs.buttonState);
5372 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005373 ASSERT_NO_FATAL_FAILURE(
5374 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
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);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005377
5378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005379 ASSERT_EQ(0, motionArgs.buttonState);
5380 ASSERT_EQ(0, mFakePointerController->getButtonState());
5381 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005382 ASSERT_NO_FATAL_FAILURE(
5383 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005384
Michael Wrightd02c5b62014-02-10 15:10:22 -08005385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5386 ASSERT_EQ(0, motionArgs.buttonState);
5387 ASSERT_EQ(0, mFakePointerController->getButtonState());
5388 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005389 ASSERT_NO_FATAL_FAILURE(
5390 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005391
5392 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005393 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
5394 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5396 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5397 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005398
Michael Wrightd02c5b62014-02-10 15:10:22 -08005399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005400 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005401 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5402 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005403 ASSERT_NO_FATAL_FAILURE(
5404 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005405
5406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5407 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5408 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5409 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005410 ASSERT_NO_FATAL_FAILURE(
5411 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005412
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
5414 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005416 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005417 ASSERT_EQ(0, motionArgs.buttonState);
5418 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005419 ASSERT_NO_FATAL_FAILURE(
5420 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005421
5422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005423 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005424 ASSERT_EQ(0, motionArgs.buttonState);
5425 ASSERT_EQ(0, mFakePointerController->getButtonState());
5426
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005427 ASSERT_NO_FATAL_FAILURE(
5428 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5430 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5431 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5432
5433 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005434 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
5435 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5437 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5438 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005439
Michael Wrightd02c5b62014-02-10 15:10:22 -08005440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005441 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005442 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5443 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005444 ASSERT_NO_FATAL_FAILURE(
5445 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005446
5447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5448 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5449 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5450 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005451 ASSERT_NO_FATAL_FAILURE(
5452 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005453
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005454 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
5455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005457 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005458 ASSERT_EQ(0, motionArgs.buttonState);
5459 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005460 ASSERT_NO_FATAL_FAILURE(
5461 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005462
5463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5464 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5465 ASSERT_EQ(0, motionArgs.buttonState);
5466 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005467 ASSERT_NO_FATAL_FAILURE(
5468 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005469
Michael Wrightd02c5b62014-02-10 15:10:22 -08005470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5471 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5472 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5473
5474 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
5476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5478 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5479 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005480
Michael Wrightd02c5b62014-02-10 15:10:22 -08005481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005482 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005483 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5484 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005485 ASSERT_NO_FATAL_FAILURE(
5486 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005487
5488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5489 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5490 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5491 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005492 ASSERT_NO_FATAL_FAILURE(
5493 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005494
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005495 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
5496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005498 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005499 ASSERT_EQ(0, motionArgs.buttonState);
5500 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005501 ASSERT_NO_FATAL_FAILURE(
5502 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005503
5504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5505 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5506 ASSERT_EQ(0, motionArgs.buttonState);
5507 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005508 ASSERT_NO_FATAL_FAILURE(
5509 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005510
Michael Wrightd02c5b62014-02-10 15:10:22 -08005511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5512 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5513 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5514
5515 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
5517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5519 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5520 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005521
Michael Wrightd02c5b62014-02-10 15:10:22 -08005522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005523 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005524 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5525 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005526 ASSERT_NO_FATAL_FAILURE(
5527 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005528
5529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5530 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5531 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5532 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005533 ASSERT_NO_FATAL_FAILURE(
5534 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005535
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005536 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
5537 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005539 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005540 ASSERT_EQ(0, motionArgs.buttonState);
5541 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005542 ASSERT_NO_FATAL_FAILURE(
5543 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005544
5545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5546 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5547 ASSERT_EQ(0, motionArgs.buttonState);
5548 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005549 ASSERT_NO_FATAL_FAILURE(
5550 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005551
Michael Wrightd02c5b62014-02-10 15:10:22 -08005552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5553 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5554 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5555}
5556
5557TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005558 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005559 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005560
5561 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5562 mFakePointerController->setPosition(100, 200);
5563 mFakePointerController->setButtonState(0);
5564
5565 NotifyMotionArgs args;
5566
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005567 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5568 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005571 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5572 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5574 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 +01005575 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005576}
5577
5578TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005579 addConfigurationProperty("cursor.mode", "pointer");
5580 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005581 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005582
5583 NotifyDeviceResetArgs resetArgs;
5584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5585 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5586 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5587
5588 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5589 mFakePointerController->setPosition(100, 200);
5590 mFakePointerController->setButtonState(0);
5591
5592 NotifyMotionArgs args;
5593
5594 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5596 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5597 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5599 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5602 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 +01005603 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005604
5605 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005606 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5607 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5609 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5610 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5612 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5614 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5615 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
5616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5617 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5618
5619 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005620 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5621 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5623 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5624 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
5625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5626 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5628 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5629 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
5630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5631 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5632
5633 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005634 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
5635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
5636 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5638 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5639 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5641 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 +01005642 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005643
5644 // Disable pointer capture and check that the device generation got bumped
5645 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08005646 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005647 mFakePolicy->setPointerCapture(false);
5648 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08005649 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005650
5651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005652 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5653
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005654 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5655 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5658 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005659 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5661 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 +01005662 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005663}
5664
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00005665/**
5666 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
5667 * pointer acceleration or speed processing should not be applied.
5668 */
5669TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5670 addConfigurationProperty("cursor.mode", "pointer");
5671 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5672 100.f /*high threshold*/, 10.f /*acceleration*/);
5673 mFakePolicy->setVelocityControlParams(testParams);
5674 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5675
5676 NotifyDeviceResetArgs resetArgs;
5677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5678 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5679 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5680
5681 NotifyMotionArgs args;
5682
5683 // Move and verify scale is applied.
5684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5688 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5689 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5690 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5691 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5692 ASSERT_GT(relX, 10);
5693 ASSERT_GT(relY, 20);
5694
5695 // Enable Pointer Capture
5696 mFakePolicy->setPointerCapture(true);
5697 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5698 NotifyPointerCaptureChangedArgs captureArgs;
5699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5700 ASSERT_TRUE(captureArgs.request.enable);
5701
5702 // Move and verify scale is not applied.
5703 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5704 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5705 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5707 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5708 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5709 ASSERT_EQ(10, args.pointerCoords[0].getX());
5710 ASSERT_EQ(20, args.pointerCoords[0].getY());
5711}
5712
Prabir Pradhan208360b2022-06-24 18:37:04 +00005713TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5714 addConfigurationProperty("cursor.mode", "pointer");
5715 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5716
5717 NotifyDeviceResetArgs resetArgs;
5718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5719 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5720 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5721
5722 // Ensure the display is rotated.
5723 prepareDisplay(DISPLAY_ORIENTATION_90);
5724
5725 NotifyMotionArgs args;
5726
5727 // Verify that the coordinates are rotated.
5728 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5729 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5730 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5732 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5733 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5734 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5735 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5736
5737 // Enable Pointer Capture.
5738 mFakePolicy->setPointerCapture(true);
5739 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5740 NotifyPointerCaptureChangedArgs captureArgs;
5741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5742 ASSERT_TRUE(captureArgs.request.enable);
5743
5744 // Move and verify rotation is not applied.
5745 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5749 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5750 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5751 ASSERT_EQ(10, args.pointerCoords[0].getX());
5752 ASSERT_EQ(20, args.pointerCoords[0].getY());
5753}
5754
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005755TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005756 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005757
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005758 // Set up the default display.
5759 prepareDisplay(DISPLAY_ORIENTATION_90);
5760
5761 // Set up the secondary display as the display on which the pointer should be shown.
5762 // The InputDevice is not associated with any display.
5763 prepareSecondaryDisplay();
5764 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005765 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5766
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005767 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005768 mFakePointerController->setPosition(100, 200);
5769 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005770
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005771 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005772 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5773 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5774 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005776 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5777 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5778 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005779 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005780}
5781
5782TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5783 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5784
5785 // Set up the default display.
5786 prepareDisplay(DISPLAY_ORIENTATION_90);
5787
5788 // Set up the secondary display as the display on which the pointer should be shown,
5789 // and associate the InputDevice with the secondary display.
5790 prepareSecondaryDisplay();
5791 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5792 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5793 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5794
5795 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5796 mFakePointerController->setPosition(100, 200);
5797 mFakePointerController->setButtonState(0);
5798
5799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005803 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5804 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5805 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005806 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5807}
5808
5809TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5810 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5811
5812 // Set up the default display as the display on which the pointer should be shown.
5813 prepareDisplay(DISPLAY_ORIENTATION_90);
5814 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5815
5816 // Associate the InputDevice with the secondary display.
5817 prepareSecondaryDisplay();
5818 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5819 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5820
5821 // The mapper should not generate any events because it is associated with a display that is
5822 // different from the pointer display.
5823 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5825 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005827}
5828
Michael Wrightd02c5b62014-02-10 15:10:22 -08005829// --- TouchInputMapperTest ---
5830
5831class TouchInputMapperTest : public InputMapperTest {
5832protected:
5833 static const int32_t RAW_X_MIN;
5834 static const int32_t RAW_X_MAX;
5835 static const int32_t RAW_Y_MIN;
5836 static const int32_t RAW_Y_MAX;
5837 static const int32_t RAW_TOUCH_MIN;
5838 static const int32_t RAW_TOUCH_MAX;
5839 static const int32_t RAW_TOOL_MIN;
5840 static const int32_t RAW_TOOL_MAX;
5841 static const int32_t RAW_PRESSURE_MIN;
5842 static const int32_t RAW_PRESSURE_MAX;
5843 static const int32_t RAW_ORIENTATION_MIN;
5844 static const int32_t RAW_ORIENTATION_MAX;
5845 static const int32_t RAW_DISTANCE_MIN;
5846 static const int32_t RAW_DISTANCE_MAX;
5847 static const int32_t RAW_TILT_MIN;
5848 static const int32_t RAW_TILT_MAX;
5849 static const int32_t RAW_ID_MIN;
5850 static const int32_t RAW_ID_MAX;
5851 static const int32_t RAW_SLOT_MIN;
5852 static const int32_t RAW_SLOT_MAX;
5853 static const float X_PRECISION;
5854 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005855 static const float X_PRECISION_VIRTUAL;
5856 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005857
5858 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005859 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005860
5861 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5862
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005863 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005864 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005865
Michael Wrightd02c5b62014-02-10 15:10:22 -08005866 enum Axes {
5867 POSITION = 1 << 0,
5868 TOUCH = 1 << 1,
5869 TOOL = 1 << 2,
5870 PRESSURE = 1 << 3,
5871 ORIENTATION = 1 << 4,
5872 MINOR = 1 << 5,
5873 ID = 1 << 6,
5874 DISTANCE = 1 << 7,
5875 TILT = 1 << 8,
5876 SLOT = 1 << 9,
5877 TOOL_TYPE = 1 << 10,
5878 };
5879
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005880 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5881 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005882 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005883 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005884 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005885 int32_t toRawX(float displayX);
5886 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005887 int32_t toRotatedRawX(float displayX);
5888 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005889 float toCookedX(float rawX, float rawY);
5890 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005891 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005892 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005893 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005894 float toDisplayY(int32_t rawY, int32_t displayHeight);
5895
Michael Wrightd02c5b62014-02-10 15:10:22 -08005896};
5897
5898const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5899const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5900const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5901const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5902const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5903const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5904const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5905const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005906const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5907const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005908const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5909const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5910const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5911const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5912const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5913const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5914const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5915const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5916const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5917const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5918const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5919const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005920const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5921 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5922const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5923 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005924const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5925 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005926
5927const float TouchInputMapperTest::GEOMETRIC_SCALE =
5928 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5929 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5930
5931const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5932 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5933 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5934};
5935
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005936void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005937 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5938 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005939}
5940
5941void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5942 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5943 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005944}
5945
Santos Cordonfa5cf462017-04-05 10:37:00 -07005946void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005947 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5948 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5949 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005950}
5951
Michael Wrightd02c5b62014-02-10 15:10:22 -08005952void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005953 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5954 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5955 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5956 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005957}
5958
Jason Gerecke489fda82012-09-07 17:19:40 -07005959void TouchInputMapperTest::prepareLocationCalibration() {
5960 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5961}
5962
Michael Wrightd02c5b62014-02-10 15:10:22 -08005963int32_t TouchInputMapperTest::toRawX(float displayX) {
5964 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5965}
5966
5967int32_t TouchInputMapperTest::toRawY(float displayY) {
5968 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5969}
5970
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005971int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5972 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5973}
5974
5975int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5976 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5977}
5978
Jason Gerecke489fda82012-09-07 17:19:40 -07005979float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5980 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5981 return rawX;
5982}
5983
5984float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5985 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5986 return rawY;
5987}
5988
Michael Wrightd02c5b62014-02-10 15:10:22 -08005989float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005990 return toDisplayX(rawX, DISPLAY_WIDTH);
5991}
5992
5993float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5994 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005995}
5996
5997float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005998 return toDisplayY(rawY, DISPLAY_HEIGHT);
5999}
6000
6001float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
6002 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006003}
6004
6005
6006// --- SingleTouchInputMapperTest ---
6007
6008class SingleTouchInputMapperTest : public TouchInputMapperTest {
6009protected:
6010 void prepareButtons();
6011 void prepareAxes(int axes);
6012
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006013 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
6014 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
6015 void processUp(SingleTouchInputMapper& mappery);
6016 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
6017 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
6018 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
6019 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
6020 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
6021 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006022};
6023
6024void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006025 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006026}
6027
6028void SingleTouchInputMapperTest::prepareAxes(int axes) {
6029 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006030 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6031 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006032 }
6033 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006034 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
6035 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006036 }
6037 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006038 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
6039 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006040 }
6041 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006042 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
6043 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006044 }
6045 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006046 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
6047 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006048 }
6049}
6050
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006051void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
6054 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006055}
6056
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006057void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006058 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
6059 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006060}
6061
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006062void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006063 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006064}
6065
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006066void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006068}
6069
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006070void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
6071 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006072 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006073}
6074
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006075void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006076 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006077}
6078
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006079void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
6080 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006081 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
6082 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006083}
6084
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006085void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
6086 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006087 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006088}
6089
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006090void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006091 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006092}
6093
Michael Wrightd02c5b62014-02-10 15:10:22 -08006094TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006095 prepareButtons();
6096 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006097 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006098
Harry Cutts16a24cc2022-10-26 15:22:19 +00006099 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006100}
6101
Michael Wrightd02c5b62014-02-10 15:10:22 -08006102TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006103 prepareButtons();
6104 prepareAxes(POSITION);
6105 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006106 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006107
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006108 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006109}
6110
6111TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006112 addConfigurationProperty("touch.deviceType", "touchScreen");
6113 prepareDisplay(DISPLAY_ORIENTATION_0);
6114 prepareButtons();
6115 prepareAxes(POSITION);
6116 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006117 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006118
6119 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006120 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006121
6122 // Virtual key is down.
6123 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6124 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6125 processDown(mapper, x, y);
6126 processSync(mapper);
6127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
6128
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006129 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006130
6131 // Virtual key is up.
6132 processUp(mapper);
6133 processSync(mapper);
6134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
6135
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006136 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006137}
6138
6139TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006140 addConfigurationProperty("touch.deviceType", "touchScreen");
6141 prepareDisplay(DISPLAY_ORIENTATION_0);
6142 prepareButtons();
6143 prepareAxes(POSITION);
6144 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006145 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006146
6147 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006148 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006149
6150 // Virtual key is down.
6151 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6152 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6153 processDown(mapper, x, y);
6154 processSync(mapper);
6155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
6156
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006157 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006158
6159 // Virtual key is up.
6160 processUp(mapper);
6161 processSync(mapper);
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
6163
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006164 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165}
6166
6167TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006168 addConfigurationProperty("touch.deviceType", "touchScreen");
6169 prepareDisplay(DISPLAY_ORIENTATION_0);
6170 prepareButtons();
6171 prepareAxes(POSITION);
6172 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006173 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006174
Michael Wrightd02c5b62014-02-10 15:10:22 -08006175 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07006176 ASSERT_TRUE(
6177 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006178 ASSERT_TRUE(flags[0]);
6179 ASSERT_FALSE(flags[1]);
6180}
6181
6182TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006183 addConfigurationProperty("touch.deviceType", "touchScreen");
6184 prepareDisplay(DISPLAY_ORIENTATION_0);
6185 prepareButtons();
6186 prepareAxes(POSITION);
6187 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006188 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006189
arthurhungdcef2dc2020-08-11 14:47:50 +08006190 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006191
6192 NotifyKeyArgs args;
6193
6194 // Press virtual key.
6195 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6196 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6197 processDown(mapper, x, y);
6198 processSync(mapper);
6199
6200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
6201 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
6202 ASSERT_EQ(DEVICE_ID, args.deviceId);
6203 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
6204 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
6205 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
6206 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
6207 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
6208 ASSERT_EQ(KEY_HOME, args.scanCode);
6209 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
6210 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
6211
6212 // Release virtual key.
6213 processUp(mapper);
6214 processSync(mapper);
6215
6216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
6217 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
6218 ASSERT_EQ(DEVICE_ID, args.deviceId);
6219 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
6220 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
6221 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
6222 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
6223 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
6224 ASSERT_EQ(KEY_HOME, args.scanCode);
6225 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
6226 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
6227
6228 // Should not have sent any motions.
6229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6230}
6231
6232TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006233 addConfigurationProperty("touch.deviceType", "touchScreen");
6234 prepareDisplay(DISPLAY_ORIENTATION_0);
6235 prepareButtons();
6236 prepareAxes(POSITION);
6237 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006238 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006239
arthurhungdcef2dc2020-08-11 14:47:50 +08006240 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006241
6242 NotifyKeyArgs keyArgs;
6243
6244 // Press virtual key.
6245 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6246 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6247 processDown(mapper, x, y);
6248 processSync(mapper);
6249
6250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6251 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
6252 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
6253 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
6254 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
6255 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6256 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
6257 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
6258 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
6259 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
6260 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
6261
6262 // Move out of bounds. This should generate a cancel and a pointer down since we moved
6263 // into the display area.
6264 y -= 100;
6265 processMove(mapper, x, y);
6266 processSync(mapper);
6267
6268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6269 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
6270 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
6271 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
6272 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
6273 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6274 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
6275 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
6276 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
6277 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
6278 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
6279 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
6280
6281 NotifyMotionArgs motionArgs;
6282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6283 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6284 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6285 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6286 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6287 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6288 ASSERT_EQ(0, motionArgs.flags);
6289 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6290 ASSERT_EQ(0, motionArgs.buttonState);
6291 ASSERT_EQ(0, motionArgs.edgeFlags);
6292 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6293 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6294 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6295 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6296 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6297 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6298 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6299 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6300
6301 // Keep moving out of bounds. Should generate a pointer move.
6302 y -= 50;
6303 processMove(mapper, x, y);
6304 processSync(mapper);
6305
6306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6307 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6308 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6309 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6310 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6312 ASSERT_EQ(0, motionArgs.flags);
6313 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6314 ASSERT_EQ(0, motionArgs.buttonState);
6315 ASSERT_EQ(0, motionArgs.edgeFlags);
6316 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6317 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6318 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6319 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6320 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6321 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6322 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6323 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6324
6325 // Release out of bounds. Should generate a pointer up.
6326 processUp(mapper);
6327 processSync(mapper);
6328
6329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6330 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6331 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6332 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6333 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6334 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6335 ASSERT_EQ(0, motionArgs.flags);
6336 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6337 ASSERT_EQ(0, motionArgs.buttonState);
6338 ASSERT_EQ(0, motionArgs.edgeFlags);
6339 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6340 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6341 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6342 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6343 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6344 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6345 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6346 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6347
6348 // Should not have sent any more keys or motions.
6349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6351}
6352
6353TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006354 addConfigurationProperty("touch.deviceType", "touchScreen");
6355 prepareDisplay(DISPLAY_ORIENTATION_0);
6356 prepareButtons();
6357 prepareAxes(POSITION);
6358 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006359 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006360
arthurhungdcef2dc2020-08-11 14:47:50 +08006361 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006362
6363 NotifyMotionArgs motionArgs;
6364
6365 // Initially go down out of bounds.
6366 int32_t x = -10;
6367 int32_t y = -10;
6368 processDown(mapper, x, y);
6369 processSync(mapper);
6370
6371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6372
6373 // Move into the display area. Should generate a pointer down.
6374 x = 50;
6375 y = 75;
6376 processMove(mapper, x, y);
6377 processSync(mapper);
6378
6379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6380 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6381 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6382 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6383 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6384 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6385 ASSERT_EQ(0, motionArgs.flags);
6386 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6387 ASSERT_EQ(0, motionArgs.buttonState);
6388 ASSERT_EQ(0, motionArgs.edgeFlags);
6389 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6390 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6391 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6393 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6394 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6395 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6396 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6397
6398 // Release. Should generate a pointer up.
6399 processUp(mapper);
6400 processSync(mapper);
6401
6402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6403 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6404 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6405 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6406 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6407 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6408 ASSERT_EQ(0, motionArgs.flags);
6409 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6410 ASSERT_EQ(0, motionArgs.buttonState);
6411 ASSERT_EQ(0, motionArgs.edgeFlags);
6412 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6413 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6414 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6416 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6417 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6418 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6419 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6420
6421 // Should not have sent any more keys or motions.
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6424}
6425
Santos Cordonfa5cf462017-04-05 10:37:00 -07006426TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07006427 addConfigurationProperty("touch.deviceType", "touchScreen");
6428 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
6429
6430 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
6431 prepareButtons();
6432 prepareAxes(POSITION);
6433 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006434 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07006435
arthurhungdcef2dc2020-08-11 14:47:50 +08006436 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006437
6438 NotifyMotionArgs motionArgs;
6439
6440 // Down.
6441 int32_t x = 100;
6442 int32_t y = 125;
6443 processDown(mapper, x, y);
6444 processSync(mapper);
6445
6446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6447 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6448 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6449 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6450 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6451 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6452 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6453 ASSERT_EQ(0, motionArgs.flags);
6454 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6455 ASSERT_EQ(0, motionArgs.buttonState);
6456 ASSERT_EQ(0, motionArgs.edgeFlags);
6457 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6458 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6459 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6460 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6461 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6462 1, 0, 0, 0, 0, 0, 0, 0));
6463 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6464 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6465 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6466
6467 // Move.
6468 x += 50;
6469 y += 75;
6470 processMove(mapper, x, y);
6471 processSync(mapper);
6472
6473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6474 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6475 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6476 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6477 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6478 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6480 ASSERT_EQ(0, motionArgs.flags);
6481 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6482 ASSERT_EQ(0, motionArgs.buttonState);
6483 ASSERT_EQ(0, motionArgs.edgeFlags);
6484 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6485 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6486 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6488 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6489 1, 0, 0, 0, 0, 0, 0, 0));
6490 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6491 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6492 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6493
6494 // Up.
6495 processUp(mapper);
6496 processSync(mapper);
6497
6498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6499 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6500 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6501 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6502 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6503 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6504 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6505 ASSERT_EQ(0, motionArgs.flags);
6506 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6507 ASSERT_EQ(0, motionArgs.buttonState);
6508 ASSERT_EQ(0, motionArgs.edgeFlags);
6509 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6510 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6511 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6513 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6514 1, 0, 0, 0, 0, 0, 0, 0));
6515 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6516 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6517 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6518
6519 // Should not have sent any more keys or motions.
6520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6522}
6523
Michael Wrightd02c5b62014-02-10 15:10:22 -08006524TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006525 addConfigurationProperty("touch.deviceType", "touchScreen");
6526 prepareDisplay(DISPLAY_ORIENTATION_0);
6527 prepareButtons();
6528 prepareAxes(POSITION);
6529 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006530 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006531
arthurhungdcef2dc2020-08-11 14:47:50 +08006532 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006533
6534 NotifyMotionArgs motionArgs;
6535
6536 // Down.
6537 int32_t x = 100;
6538 int32_t y = 125;
6539 processDown(mapper, x, y);
6540 processSync(mapper);
6541
6542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6543 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6544 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6545 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6546 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6547 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6548 ASSERT_EQ(0, motionArgs.flags);
6549 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6550 ASSERT_EQ(0, motionArgs.buttonState);
6551 ASSERT_EQ(0, motionArgs.edgeFlags);
6552 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6553 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6554 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6556 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6557 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6558 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6559 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6560
6561 // Move.
6562 x += 50;
6563 y += 75;
6564 processMove(mapper, x, y);
6565 processSync(mapper);
6566
6567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6568 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6569 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6570 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6571 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6572 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6573 ASSERT_EQ(0, motionArgs.flags);
6574 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6575 ASSERT_EQ(0, motionArgs.buttonState);
6576 ASSERT_EQ(0, motionArgs.edgeFlags);
6577 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6578 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6579 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6581 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6582 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6583 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6584 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6585
6586 // Up.
6587 processUp(mapper);
6588 processSync(mapper);
6589
6590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6591 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6592 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6593 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6594 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6595 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6596 ASSERT_EQ(0, motionArgs.flags);
6597 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6598 ASSERT_EQ(0, motionArgs.buttonState);
6599 ASSERT_EQ(0, motionArgs.edgeFlags);
6600 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6601 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6604 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6605 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6606 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6607 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6608
6609 // Should not have sent any more keys or motions.
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6612}
6613
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006614TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006615 addConfigurationProperty("touch.deviceType", "touchScreen");
6616 prepareButtons();
6617 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006618 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
6619 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006620 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006621
6622 NotifyMotionArgs args;
6623
6624 // Rotation 90.
6625 prepareDisplay(DISPLAY_ORIENTATION_90);
6626 processDown(mapper, toRawX(50), toRawY(75));
6627 processSync(mapper);
6628
6629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6630 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6631 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6632
6633 processUp(mapper);
6634 processSync(mapper);
6635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6636}
6637
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006638TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006639 addConfigurationProperty("touch.deviceType", "touchScreen");
6640 prepareButtons();
6641 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006642 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6643 // orientation-aware are affected by display rotation.
6644 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006645 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006646
6647 NotifyMotionArgs args;
6648
6649 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006650 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006651 prepareDisplay(DISPLAY_ORIENTATION_0);
6652 processDown(mapper, toRawX(50), toRawY(75));
6653 processSync(mapper);
6654
6655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6656 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6657 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6658
6659 processUp(mapper);
6660 processSync(mapper);
6661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6662
6663 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006664 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006665 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006666 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006667 processSync(mapper);
6668
6669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6670 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6671 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6672
6673 processUp(mapper);
6674 processSync(mapper);
6675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6676
6677 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006678 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006679 prepareDisplay(DISPLAY_ORIENTATION_180);
6680 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6681 processSync(mapper);
6682
6683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6684 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6685 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6686
6687 processUp(mapper);
6688 processSync(mapper);
6689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6690
6691 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006692 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006693 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006694 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006695 processSync(mapper);
6696
6697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6698 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6699 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6700
6701 processUp(mapper);
6702 processSync(mapper);
6703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6704}
6705
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006706TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6707 addConfigurationProperty("touch.deviceType", "touchScreen");
6708 prepareButtons();
6709 prepareAxes(POSITION);
6710 addConfigurationProperty("touch.orientationAware", "1");
6711 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6712 clearViewports();
6713 prepareDisplay(DISPLAY_ORIENTATION_0);
6714 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6715 NotifyMotionArgs args;
6716
6717 // Orientation 0.
6718 processDown(mapper, toRawX(50), toRawY(75));
6719 processSync(mapper);
6720
6721 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6722 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6723 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6724
6725 processUp(mapper);
6726 processSync(mapper);
6727 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6728}
6729
6730TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6731 addConfigurationProperty("touch.deviceType", "touchScreen");
6732 prepareButtons();
6733 prepareAxes(POSITION);
6734 addConfigurationProperty("touch.orientationAware", "1");
6735 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6736 clearViewports();
6737 prepareDisplay(DISPLAY_ORIENTATION_0);
6738 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6739 NotifyMotionArgs args;
6740
6741 // Orientation 90.
6742 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6743 processSync(mapper);
6744
6745 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6746 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6747 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6748
6749 processUp(mapper);
6750 processSync(mapper);
6751 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6752}
6753
6754TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6755 addConfigurationProperty("touch.deviceType", "touchScreen");
6756 prepareButtons();
6757 prepareAxes(POSITION);
6758 addConfigurationProperty("touch.orientationAware", "1");
6759 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6760 clearViewports();
6761 prepareDisplay(DISPLAY_ORIENTATION_0);
6762 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6763 NotifyMotionArgs args;
6764
6765 // Orientation 180.
6766 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6767 processSync(mapper);
6768
6769 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6770 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6771 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6772
6773 processUp(mapper);
6774 processSync(mapper);
6775 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6776}
6777
6778TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6779 addConfigurationProperty("touch.deviceType", "touchScreen");
6780 prepareButtons();
6781 prepareAxes(POSITION);
6782 addConfigurationProperty("touch.orientationAware", "1");
6783 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6784 clearViewports();
6785 prepareDisplay(DISPLAY_ORIENTATION_0);
6786 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6787 NotifyMotionArgs args;
6788
6789 // Orientation 270.
6790 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6791 processSync(mapper);
6792
6793 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6794 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6795 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6796
6797 processUp(mapper);
6798 processSync(mapper);
6799 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6800}
6801
6802TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6803 addConfigurationProperty("touch.deviceType", "touchScreen");
6804 prepareButtons();
6805 prepareAxes(POSITION);
6806 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6807 // orientation-aware are affected by display rotation.
6808 addConfigurationProperty("touch.orientationAware", "0");
6809 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6810 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6811
6812 NotifyMotionArgs args;
6813
6814 // Orientation 90, Rotation 0.
6815 clearViewports();
6816 prepareDisplay(DISPLAY_ORIENTATION_0);
6817 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6818 processSync(mapper);
6819
6820 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6821 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6822 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6823
6824 processUp(mapper);
6825 processSync(mapper);
6826 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6827
6828 // Orientation 90, Rotation 90.
6829 clearViewports();
6830 prepareDisplay(DISPLAY_ORIENTATION_90);
6831 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6832 processSync(mapper);
6833
6834 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6835 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6836 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6837
6838 processUp(mapper);
6839 processSync(mapper);
6840 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6841
6842 // Orientation 90, Rotation 180.
6843 clearViewports();
6844 prepareDisplay(DISPLAY_ORIENTATION_180);
6845 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6846 processSync(mapper);
6847
6848 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6849 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6850 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6851
6852 processUp(mapper);
6853 processSync(mapper);
6854 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6855
6856 // Orientation 90, Rotation 270.
6857 clearViewports();
6858 prepareDisplay(DISPLAY_ORIENTATION_270);
6859 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6860 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6861 processSync(mapper);
6862
6863 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6864 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6865 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6866
6867 processUp(mapper);
6868 processSync(mapper);
6869 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6870}
6871
Michael Wrightd02c5b62014-02-10 15:10:22 -08006872TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006873 addConfigurationProperty("touch.deviceType", "touchScreen");
6874 prepareDisplay(DISPLAY_ORIENTATION_0);
6875 prepareButtons();
6876 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006877 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006878
6879 // These calculations are based on the input device calibration documentation.
6880 int32_t rawX = 100;
6881 int32_t rawY = 200;
6882 int32_t rawPressure = 10;
6883 int32_t rawToolMajor = 12;
6884 int32_t rawDistance = 2;
6885 int32_t rawTiltX = 30;
6886 int32_t rawTiltY = 110;
6887
6888 float x = toDisplayX(rawX);
6889 float y = toDisplayY(rawY);
6890 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6891 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6892 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6893 float distance = float(rawDistance);
6894
6895 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6896 float tiltScale = M_PI / 180;
6897 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6898 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6899 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6900 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6901
6902 processDown(mapper, rawX, rawY);
6903 processPressure(mapper, rawPressure);
6904 processToolMajor(mapper, rawToolMajor);
6905 processDistance(mapper, rawDistance);
6906 processTilt(mapper, rawTiltX, rawTiltY);
6907 processSync(mapper);
6908
6909 NotifyMotionArgs args;
6910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6912 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6913 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6914}
6915
Jason Gerecke489fda82012-09-07 17:19:40 -07006916TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006917 addConfigurationProperty("touch.deviceType", "touchScreen");
6918 prepareDisplay(DISPLAY_ORIENTATION_0);
6919 prepareLocationCalibration();
6920 prepareButtons();
6921 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006922 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006923
6924 int32_t rawX = 100;
6925 int32_t rawY = 200;
6926
6927 float x = toDisplayX(toCookedX(rawX, rawY));
6928 float y = toDisplayY(toCookedY(rawX, rawY));
6929
6930 processDown(mapper, rawX, rawY);
6931 processSync(mapper);
6932
6933 NotifyMotionArgs args;
6934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6935 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6936 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6937}
6938
Michael Wrightd02c5b62014-02-10 15:10:22 -08006939TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006940 addConfigurationProperty("touch.deviceType", "touchScreen");
6941 prepareDisplay(DISPLAY_ORIENTATION_0);
6942 prepareButtons();
6943 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006944 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006945
6946 NotifyMotionArgs motionArgs;
6947 NotifyKeyArgs keyArgs;
6948
6949 processDown(mapper, 100, 200);
6950 processSync(mapper);
6951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6952 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6953 ASSERT_EQ(0, motionArgs.buttonState);
6954
6955 // press BTN_LEFT, release BTN_LEFT
6956 processKey(mapper, BTN_LEFT, 1);
6957 processSync(mapper);
6958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6960 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6961
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6963 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6964 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6965
Michael Wrightd02c5b62014-02-10 15:10:22 -08006966 processKey(mapper, BTN_LEFT, 0);
6967 processSync(mapper);
6968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006969 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006970 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006971
6972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006973 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006974 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006975
6976 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6977 processKey(mapper, BTN_RIGHT, 1);
6978 processKey(mapper, BTN_MIDDLE, 1);
6979 processSync(mapper);
6980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6981 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6982 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6983 motionArgs.buttonState);
6984
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6986 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6987 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6988
6989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6990 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6991 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6992 motionArgs.buttonState);
6993
Michael Wrightd02c5b62014-02-10 15:10:22 -08006994 processKey(mapper, BTN_RIGHT, 0);
6995 processSync(mapper);
6996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006997 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006998 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006999
7000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007001 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007002 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007003
7004 processKey(mapper, BTN_MIDDLE, 0);
7005 processSync(mapper);
7006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007007 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007008 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007009
7010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007011 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007012 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007013
7014 // press BTN_BACK, release BTN_BACK
7015 processKey(mapper, BTN_BACK, 1);
7016 processSync(mapper);
7017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7018 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7019 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007020
Michael Wrightd02c5b62014-02-10 15:10:22 -08007021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007022 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007023 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7024
7025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7026 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7027 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007028
7029 processKey(mapper, BTN_BACK, 0);
7030 processSync(mapper);
7031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007032 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007033 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007034
7035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007036 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007037 ASSERT_EQ(0, motionArgs.buttonState);
7038
Michael Wrightd02c5b62014-02-10 15:10:22 -08007039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7040 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7041 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7042
7043 // press BTN_SIDE, release BTN_SIDE
7044 processKey(mapper, BTN_SIDE, 1);
7045 processSync(mapper);
7046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7047 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7048 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007049
Michael Wrightd02c5b62014-02-10 15:10:22 -08007050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007052 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7053
7054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7055 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7056 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007057
7058 processKey(mapper, BTN_SIDE, 0);
7059 processSync(mapper);
7060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007061 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007062 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007063
7064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007065 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007066 ASSERT_EQ(0, motionArgs.buttonState);
7067
Michael Wrightd02c5b62014-02-10 15:10:22 -08007068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7069 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7070 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7071
7072 // press BTN_FORWARD, release BTN_FORWARD
7073 processKey(mapper, BTN_FORWARD, 1);
7074 processSync(mapper);
7075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7076 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7077 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007078
Michael Wrightd02c5b62014-02-10 15:10:22 -08007079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007080 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007081 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7082
7083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7084 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7085 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007086
7087 processKey(mapper, BTN_FORWARD, 0);
7088 processSync(mapper);
7089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007090 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007091 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007092
7093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007095 ASSERT_EQ(0, motionArgs.buttonState);
7096
Michael Wrightd02c5b62014-02-10 15:10:22 -08007097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7098 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7099 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7100
7101 // press BTN_EXTRA, release BTN_EXTRA
7102 processKey(mapper, BTN_EXTRA, 1);
7103 processSync(mapper);
7104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7105 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7106 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007107
Michael Wrightd02c5b62014-02-10 15:10:22 -08007108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007109 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007110 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7111
7112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7113 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7114 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007115
7116 processKey(mapper, BTN_EXTRA, 0);
7117 processSync(mapper);
7118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007119 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007120 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007121
7122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007124 ASSERT_EQ(0, motionArgs.buttonState);
7125
Michael Wrightd02c5b62014-02-10 15:10:22 -08007126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7127 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7128 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7129
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7131
Michael Wrightd02c5b62014-02-10 15:10:22 -08007132 // press BTN_STYLUS, release BTN_STYLUS
7133 processKey(mapper, BTN_STYLUS, 1);
7134 processSync(mapper);
7135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7136 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007137 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7138
7139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7140 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7141 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007142
7143 processKey(mapper, BTN_STYLUS, 0);
7144 processSync(mapper);
7145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007146 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007147 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007148
7149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007151 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007152
7153 // press BTN_STYLUS2, release BTN_STYLUS2
7154 processKey(mapper, BTN_STYLUS2, 1);
7155 processSync(mapper);
7156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7157 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007158 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7159
7160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7161 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7162 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007163
7164 processKey(mapper, BTN_STYLUS2, 0);
7165 processSync(mapper);
7166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007167 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007168 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007169
7170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007171 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007172 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007173
7174 // release touch
7175 processUp(mapper);
7176 processSync(mapper);
7177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7178 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7179 ASSERT_EQ(0, motionArgs.buttonState);
7180}
7181
7182TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007183 addConfigurationProperty("touch.deviceType", "touchScreen");
7184 prepareDisplay(DISPLAY_ORIENTATION_0);
7185 prepareButtons();
7186 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007187 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007188
7189 NotifyMotionArgs motionArgs;
7190
7191 // default tool type is finger
7192 processDown(mapper, 100, 200);
7193 processSync(mapper);
7194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7195 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7196 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7197
7198 // eraser
7199 processKey(mapper, BTN_TOOL_RUBBER, 1);
7200 processSync(mapper);
7201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7202 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7203 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7204
7205 // stylus
7206 processKey(mapper, BTN_TOOL_RUBBER, 0);
7207 processKey(mapper, BTN_TOOL_PEN, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
7212
7213 // brush
7214 processKey(mapper, BTN_TOOL_PEN, 0);
7215 processKey(mapper, BTN_TOOL_BRUSH, 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 // pencil
7222 processKey(mapper, BTN_TOOL_BRUSH, 0);
7223 processKey(mapper, BTN_TOOL_PENCIL, 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
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007229 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007230 processKey(mapper, BTN_TOOL_PENCIL, 0);
7231 processKey(mapper, BTN_TOOL_AIRBRUSH, 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
7237 // mouse
7238 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7239 processKey(mapper, BTN_TOOL_MOUSE, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
7244
7245 // lens
7246 processKey(mapper, BTN_TOOL_MOUSE, 0);
7247 processKey(mapper, BTN_TOOL_LENS, 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 // double-tap
7254 processKey(mapper, BTN_TOOL_LENS, 0);
7255 processKey(mapper, BTN_TOOL_DOUBLETAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
7260
7261 // triple-tap
7262 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7263 processKey(mapper, BTN_TOOL_TRIPLETAP, 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 // quad-tap
7270 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7271 processKey(mapper, BTN_TOOL_QUADTAP, 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 // finger
7278 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7279 processKey(mapper, BTN_TOOL_FINGER, 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 // stylus trumps finger
7286 processKey(mapper, BTN_TOOL_PEN, 1);
7287 processSync(mapper);
7288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7290 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7291
7292 // eraser trumps stylus
7293 processKey(mapper, BTN_TOOL_RUBBER, 1);
7294 processSync(mapper);
7295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7297 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7298
7299 // mouse trumps eraser
7300 processKey(mapper, BTN_TOOL_MOUSE, 1);
7301 processSync(mapper);
7302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7305
7306 // back to default tool type
7307 processKey(mapper, BTN_TOOL_MOUSE, 0);
7308 processKey(mapper, BTN_TOOL_RUBBER, 0);
7309 processKey(mapper, BTN_TOOL_PEN, 0);
7310 processKey(mapper, BTN_TOOL_FINGER, 0);
7311 processSync(mapper);
7312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7313 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7314 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7315}
7316
7317TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007318 addConfigurationProperty("touch.deviceType", "touchScreen");
7319 prepareDisplay(DISPLAY_ORIENTATION_0);
7320 prepareButtons();
7321 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007322 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007323 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007324
7325 NotifyMotionArgs motionArgs;
7326
7327 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7328 processKey(mapper, BTN_TOOL_FINGER, 1);
7329 processMove(mapper, 100, 200);
7330 processSync(mapper);
7331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7332 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7334 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7335
7336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7337 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7339 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7340
7341 // move a little
7342 processMove(mapper, 150, 250);
7343 processSync(mapper);
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(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7348
7349 // down when BTN_TOUCH is pressed, pressure defaults to 1
7350 processKey(mapper, BTN_TOUCH, 1);
7351 processSync(mapper);
7352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7353 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7358 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7360 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7361
7362 // up when BTN_TOUCH is released, hover restored
7363 processKey(mapper, BTN_TOUCH, 0);
7364 processSync(mapper);
7365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7366 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7371 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7372 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7373 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7374
7375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7376 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7378 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7379
7380 // exit hover when pointer goes away
7381 processKey(mapper, BTN_TOOL_FINGER, 0);
7382 processSync(mapper);
7383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7384 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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
7389TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007390 addConfigurationProperty("touch.deviceType", "touchScreen");
7391 prepareDisplay(DISPLAY_ORIENTATION_0);
7392 prepareButtons();
7393 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007394 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007395
7396 NotifyMotionArgs motionArgs;
7397
7398 // initially hovering because pressure is 0
7399 processDown(mapper, 100, 200);
7400 processPressure(mapper, 0);
7401 processSync(mapper);
7402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7403 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7404 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7405 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7406
7407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7408 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7410 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7411
7412 // move a little
7413 processMove(mapper, 150, 250);
7414 processSync(mapper);
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(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7419
7420 // down when pressure is non-zero
7421 processPressure(mapper, RAW_PRESSURE_MAX);
7422 processSync(mapper);
7423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7424 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7429 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7431 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7432
7433 // up when pressure becomes 0, hover restored
7434 processPressure(mapper, 0);
7435 processSync(mapper);
7436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7437 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7442 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7444 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7445
7446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7447 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7448 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7449 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7450
7451 // exit hover when pointer goes away
7452 processUp(mapper);
7453 processSync(mapper);
7454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7455 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007460TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
7461 addConfigurationProperty("touch.deviceType", "touchScreen");
7462 prepareDisplay(DISPLAY_ORIENTATION_0);
7463 prepareButtons();
7464 prepareAxes(POSITION | PRESSURE);
7465 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7466
7467 // Touch down.
7468 processDown(mapper, 100, 200);
7469 processPressure(mapper, 1);
7470 processSync(mapper);
7471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7472 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
7473
7474 // Reset the mapper. This should cancel the ongoing gesture.
7475 resetMapper(mapper, ARBITRARY_TIME);
7476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7477 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
7478
7479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7480}
7481
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007482TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
7483 addConfigurationProperty("touch.deviceType", "touchScreen");
7484 prepareDisplay(DISPLAY_ORIENTATION_0);
7485 prepareButtons();
7486 prepareAxes(POSITION | PRESSURE);
7487 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7488
7489 // Set the initial state for the touch pointer.
7490 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
7491 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
7492 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
7493 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7494
7495 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007496 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
7497 // does not generate any events.
7498 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007499
7500 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
7501 // the recreated touch state to generate a down event.
7502 processSync(mapper);
7503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7504 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
7505
7506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7507}
7508
lilinnan687e58f2022-07-19 16:00:50 +08007509TEST_F(SingleTouchInputMapperTest,
7510 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
7511 addConfigurationProperty("touch.deviceType", "touchScreen");
7512 prepareDisplay(DISPLAY_ORIENTATION_0);
7513 prepareButtons();
7514 prepareAxes(POSITION);
7515 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7516 NotifyMotionArgs motionArgs;
7517
7518 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00007519 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08007520 processSync(mapper);
7521
7522 // We should receive a down event
7523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7524 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7525
7526 // Change display id
7527 clearViewports();
7528 prepareSecondaryDisplay(ViewportType::INTERNAL);
7529
7530 // We should receive a cancel event
7531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7532 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7533 // Then receive reset called
7534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7535}
7536
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007537TEST_F(SingleTouchInputMapperTest,
7538 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
7539 addConfigurationProperty("touch.deviceType", "touchScreen");
7540 prepareDisplay(DISPLAY_ORIENTATION_0);
7541 prepareButtons();
7542 prepareAxes(POSITION);
7543 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7545 NotifyMotionArgs motionArgs;
7546
7547 // Start a new gesture.
7548 processDown(mapper, 100, 200);
7549 processSync(mapper);
7550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7551 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7552
7553 // Make the viewport inactive. This will put the device in disabled mode.
7554 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7555 viewport->isActive = false;
7556 mFakePolicy->updateViewport(*viewport);
7557 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7558
7559 // We should receive a cancel event for the ongoing gesture.
7560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7561 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7562 // Then we should be notified that the device was reset.
7563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7564
7565 // No events are generated while the viewport is inactive.
7566 processMove(mapper, 101, 201);
7567 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007568 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007569 processSync(mapper);
7570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7571
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007572 // Start a new gesture while the viewport is still inactive.
7573 processDown(mapper, 300, 400);
7574 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
7575 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
7576 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7577 processSync(mapper);
7578
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007579 // Make the viewport active again. The device should resume processing events.
7580 viewport->isActive = true;
7581 mFakePolicy->updateViewport(*viewport);
7582 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7583
7584 // The device is reset because it changes back to direct mode, without generating any events.
7585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7587
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007588 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007589 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7591 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007592
7593 // No more events.
7594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
7596}
7597
Prabir Pradhan211ba622022-10-31 21:09:21 +00007598TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
7599 addConfigurationProperty("touch.deviceType", "touchScreen");
7600 prepareDisplay(DISPLAY_ORIENTATION_0);
7601 prepareButtons();
7602 prepareAxes(POSITION);
7603 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7605
7606 // Press a stylus button.
7607 processKey(mapper, BTN_STYLUS, 1);
7608 processSync(mapper);
7609
7610 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
7611 processDown(mapper, 100, 200);
7612 processSync(mapper);
7613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7614 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7615 WithCoords(toDisplayX(100), toDisplayY(200)),
7616 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7618 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7619 WithCoords(toDisplayX(100), toDisplayY(200)),
7620 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7621
7622 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
7623 // the button has not actually been released, since there will be no pointers through which the
7624 // button state can be reported. The event is generated at the location of the pointer before
7625 // it went up.
7626 processUp(mapper);
7627 processSync(mapper);
7628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7629 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7630 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7632 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7633 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7634}
7635
Prabir Pradhan5632d622021-09-06 07:57:20 -07007636// --- TouchDisplayProjectionTest ---
7637
7638class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7639public:
7640 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7641 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7642 // rotated equivalent of the given un-rotated physical display bounds.
7643 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
7644 uint32_t inverseRotationFlags;
7645 auto width = DISPLAY_WIDTH;
7646 auto height = DISPLAY_HEIGHT;
7647 switch (orientation) {
7648 case DISPLAY_ORIENTATION_90:
7649 inverseRotationFlags = ui::Transform::ROT_270;
7650 std::swap(width, height);
7651 break;
7652 case DISPLAY_ORIENTATION_180:
7653 inverseRotationFlags = ui::Transform::ROT_180;
7654 break;
7655 case DISPLAY_ORIENTATION_270:
7656 inverseRotationFlags = ui::Transform::ROT_90;
7657 std::swap(width, height);
7658 break;
7659 case DISPLAY_ORIENTATION_0:
7660 inverseRotationFlags = ui::Transform::ROT_0;
7661 break;
7662 default:
7663 FAIL() << "Invalid orientation: " << orientation;
7664 }
7665
7666 const ui::Transform rotation(inverseRotationFlags, width, height);
7667 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7668
7669 std::optional<DisplayViewport> internalViewport =
7670 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7671 DisplayViewport& v = *internalViewport;
7672 v.displayId = DISPLAY_ID;
7673 v.orientation = orientation;
7674
7675 v.logicalLeft = 0;
7676 v.logicalTop = 0;
7677 v.logicalRight = 100;
7678 v.logicalBottom = 100;
7679
7680 v.physicalLeft = rotatedPhysicalDisplay.left;
7681 v.physicalTop = rotatedPhysicalDisplay.top;
7682 v.physicalRight = rotatedPhysicalDisplay.right;
7683 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7684
7685 v.deviceWidth = width;
7686 v.deviceHeight = height;
7687
7688 v.isActive = true;
7689 v.uniqueId = UNIQUE_ID;
7690 v.type = ViewportType::INTERNAL;
7691 mFakePolicy->updateViewport(v);
7692 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7693 }
7694
7695 void assertReceivedMove(const Point& point) {
7696 NotifyMotionArgs motionArgs;
7697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7698 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7699 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7701 1, 0, 0, 0, 0, 0, 0, 0));
7702 }
7703};
7704
7705TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7706 addConfigurationProperty("touch.deviceType", "touchScreen");
7707 prepareDisplay(DISPLAY_ORIENTATION_0);
7708
7709 prepareButtons();
7710 prepareAxes(POSITION);
7711 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7712
7713 NotifyMotionArgs motionArgs;
7714
7715 // Configure the DisplayViewport such that the logical display maps to a subsection of
7716 // the display panel called the physical display. Here, the physical display is bounded by the
7717 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7718 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7719 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7720 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7721
7722 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7723 DISPLAY_ORIENTATION_270}) {
7724 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7725
7726 // Touches outside the physical display should be ignored, and should not generate any
7727 // events. Ensure touches at the following points that lie outside of the physical display
7728 // area do not generate any events.
7729 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7730 processDown(mapper, toRawX(point.x), toRawY(point.y));
7731 processSync(mapper);
7732 processUp(mapper);
7733 processSync(mapper);
7734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7735 << "Unexpected event generated for touch outside physical display at point: "
7736 << point.x << ", " << point.y;
7737 }
7738 }
7739}
7740
7741TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7742 addConfigurationProperty("touch.deviceType", "touchScreen");
7743 prepareDisplay(DISPLAY_ORIENTATION_0);
7744
7745 prepareButtons();
7746 prepareAxes(POSITION);
7747 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7748
7749 NotifyMotionArgs motionArgs;
7750
7751 // Configure the DisplayViewport such that the logical display maps to a subsection of
7752 // the display panel called the physical display. Here, the physical display is bounded by the
7753 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7754 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7755
7756 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7757 DISPLAY_ORIENTATION_270}) {
7758 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7759
7760 // Touches that start outside the physical display should be ignored until it enters the
7761 // physical display bounds, at which point it should generate a down event. Start a touch at
7762 // the point (5, 100), which is outside the physical display bounds.
7763 static const Point kOutsidePoint{5, 100};
7764 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7765 processSync(mapper);
7766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7767
7768 // Move the touch into the physical display area. This should generate a pointer down.
7769 processMove(mapper, toRawX(11), toRawY(21));
7770 processSync(mapper);
7771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7772 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7773 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7774 ASSERT_NO_FATAL_FAILURE(
7775 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7776
7777 // Move the touch inside the physical display area. This should generate a pointer move.
7778 processMove(mapper, toRawX(69), toRawY(159));
7779 processSync(mapper);
7780 assertReceivedMove({69, 159});
7781
7782 // Move outside the physical display area. Since the pointer is already down, this should
7783 // now continue generating events.
7784 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7785 processSync(mapper);
7786 assertReceivedMove(kOutsidePoint);
7787
7788 // Release. This should generate a pointer up.
7789 processUp(mapper);
7790 processSync(mapper);
7791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7792 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7794 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7795
7796 // Ensure no more events were generated.
7797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7799 }
7800}
7801
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007802// --- ExternalStylusFusionTest ---
7803
7804class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7805public:
7806 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7807 addConfigurationProperty("touch.deviceType", "touchScreen");
7808 prepareDisplay(DISPLAY_ORIENTATION_0);
7809 prepareButtons();
7810 prepareAxes(POSITION);
7811 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7812
7813 mStylusState.when = ARBITRARY_TIME;
7814 mStylusState.pressure = 0.f;
7815 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7816 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
7817 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
7818 processExternalStylusState(mapper);
7819 return mapper;
7820 }
7821
7822 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7823 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7824 for (const NotifyArgs& args : generatedArgs) {
7825 mFakeListener->notify(args);
7826 }
7827 // Loop the reader to flush the input listener queue.
7828 mReader->loopOnce();
7829 return generatedArgs;
7830 }
7831
7832protected:
7833 StylusState mStylusState{};
7834 static constexpr uint32_t EXPECTED_SOURCE =
7835 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7836
7837 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7838 auto toolTypeSource =
7839 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7840
7841 // The first pointer is withheld.
7842 processDown(mapper, 100, 200);
7843 processSync(mapper);
7844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7845 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7846 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7847
7848 // The external stylus reports pressure. The withheld finger pointer is released as a
7849 // stylus.
7850 mStylusState.pressure = 1.f;
7851 processExternalStylusState(mapper);
7852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7853 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7854 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7855
7856 // Subsequent pointer events are not withheld.
7857 processMove(mapper, 101, 201);
7858 processSync(mapper);
7859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7860 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7861
7862 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7864 }
7865
7866 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7867 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7868
7869 // Releasing the touch pointer ends the gesture.
7870 processUp(mapper);
7871 processSync(mapper);
7872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7873 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7874 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7875
7876 mStylusState.pressure = 0.f;
7877 processExternalStylusState(mapper);
7878 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7880 }
7881
7882 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7883 auto toolTypeSource =
7884 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER));
7885
7886 // The first pointer is withheld when an external stylus is connected,
7887 // and a timeout is requested.
7888 processDown(mapper, 100, 200);
7889 processSync(mapper);
7890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7891 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7892 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7893
7894 // If the timeout expires early, it is requested again.
7895 handleTimeout(mapper, ARBITRARY_TIME + 1);
7896 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7897 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7898
7899 // When the timeout expires, the withheld touch is released as a finger pointer.
7900 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7902 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7903
7904 // Subsequent pointer events are not withheld.
7905 processMove(mapper, 101, 201);
7906 processSync(mapper);
7907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7908 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7909 processUp(mapper);
7910 processSync(mapper);
7911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7912 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7913
7914 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7916 }
7917
7918private:
7919 InputDeviceInfo mExternalStylusDeviceInfo{};
7920};
7921
7922TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7923 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7924 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7925}
7926
7927TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7928 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7929 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7930}
7931
7932TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7933 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7934 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7935}
7936
7937// Test a successful stylus fusion gesture where the pressure is reported by the external
7938// before the touch is reported by the touchscreen.
7939TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7940 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7941 auto toolTypeSource =
7942 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7943
7944 // The external stylus reports pressure first. It is ignored for now.
7945 mStylusState.pressure = 1.f;
7946 processExternalStylusState(mapper);
7947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7948 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7949
7950 // When the touch goes down afterwards, it is reported as a stylus pointer.
7951 processDown(mapper, 100, 200);
7952 processSync(mapper);
7953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7954 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7955 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7956
7957 processMove(mapper, 101, 201);
7958 processSync(mapper);
7959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7960 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7961 processUp(mapper);
7962 processSync(mapper);
7963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7964 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7965
7966 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7968}
7969
7970TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7971 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7972
7973 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7974 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7975
7976 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7977 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7978 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7979 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7980}
7981
7982TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7983 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7984 auto toolTypeSource =
7985 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7986
7987 mStylusState.pressure = 0.8f;
7988 processExternalStylusState(mapper);
7989 processDown(mapper, 100, 200);
7990 processSync(mapper);
7991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7992 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7993 WithPressure(0.8f))));
7994 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7995
7996 // The external stylus reports a pressure change. We wait for some time for a touch event.
7997 mStylusState.pressure = 0.6f;
7998 processExternalStylusState(mapper);
7999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8000 ASSERT_NO_FATAL_FAILURE(
8001 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8002
8003 // If a touch is reported within the timeout, it reports the updated pressure.
8004 processMove(mapper, 101, 201);
8005 processSync(mapper);
8006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8007 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8008 WithPressure(0.6f))));
8009 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8010
8011 // There is another pressure change.
8012 mStylusState.pressure = 0.5f;
8013 processExternalStylusState(mapper);
8014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8015 ASSERT_NO_FATAL_FAILURE(
8016 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8017
8018 // If a touch is not reported within the timeout, a move event is generated to report
8019 // the new pressure.
8020 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
8021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8022 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8023 WithPressure(0.5f))));
8024
8025 // If a zero pressure is reported before the touch goes up, the previous pressure value is
8026 // repeated indefinitely.
8027 mStylusState.pressure = 0.0f;
8028 processExternalStylusState(mapper);
8029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8030 ASSERT_NO_FATAL_FAILURE(
8031 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8032 processMove(mapper, 102, 202);
8033 processSync(mapper);
8034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8035 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8036 WithPressure(0.5f))));
8037 processMove(mapper, 103, 203);
8038 processSync(mapper);
8039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8040 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8041 WithPressure(0.5f))));
8042
8043 processUp(mapper);
8044 processSync(mapper);
8045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8046 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
8047 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
8048
8049 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8051}
8052
8053TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
8054 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8055 auto source = WithSource(EXPECTED_SOURCE);
8056
8057 mStylusState.pressure = 1.f;
8058 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_ERASER;
8059 processExternalStylusState(mapper);
8060 processDown(mapper, 100, 200);
8061 processSync(mapper);
8062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8063 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
8064 WithToolType(AMOTION_EVENT_TOOL_TYPE_ERASER))));
8065 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8066
8067 // The external stylus reports a tool change. We wait for some time for a touch event.
8068 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
8069 processExternalStylusState(mapper);
8070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8071 ASSERT_NO_FATAL_FAILURE(
8072 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8073
8074 // If a touch is reported within the timeout, it reports the updated pressure.
8075 processMove(mapper, 101, 201);
8076 processSync(mapper);
8077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8078 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8079 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
8080 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8081
8082 // There is another tool type change.
8083 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
8084 processExternalStylusState(mapper);
8085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8086 ASSERT_NO_FATAL_FAILURE(
8087 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8088
8089 // If a touch is not reported within the timeout, a move event is generated to report
8090 // the new tool type.
8091 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
8092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8093 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8094 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
8095
8096 processUp(mapper);
8097 processSync(mapper);
8098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8099 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
8100 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
8101
8102 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8104}
8105
8106TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
8107 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8108 auto toolTypeSource =
8109 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
8110
8111 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
8112
8113 // The external stylus reports a button change. We wait for some time for a touch event.
8114 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
8115 processExternalStylusState(mapper);
8116 ASSERT_NO_FATAL_FAILURE(
8117 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8118
8119 // If a touch is reported within the timeout, it reports the updated button state.
8120 processMove(mapper, 101, 201);
8121 processSync(mapper);
8122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8123 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8124 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8126 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8127 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8128 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8129
8130 // The button is now released.
8131 mStylusState.buttons = 0;
8132 processExternalStylusState(mapper);
8133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8134 ASSERT_NO_FATAL_FAILURE(
8135 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8136
8137 // If a touch is not reported within the timeout, a move event is generated to report
8138 // the new button state.
8139 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8141 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
8142 WithButtonState(0))));
8143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00008144 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8145 WithButtonState(0))));
8146
8147 processUp(mapper);
8148 processSync(mapper);
8149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008150 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8151
8152 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8154}
8155
Michael Wrightd02c5b62014-02-10 15:10:22 -08008156// --- MultiTouchInputMapperTest ---
8157
8158class MultiTouchInputMapperTest : public TouchInputMapperTest {
8159protected:
8160 void prepareAxes(int axes);
8161
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008162 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
8163 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
8164 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
8165 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
8166 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
8167 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
8168 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
8169 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
8170 void processId(MultiTouchInputMapper& mapper, int32_t id);
8171 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
8172 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
8173 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008174 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008175 void processMTSync(MultiTouchInputMapper& mapper);
8176 void processSync(MultiTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008177};
8178
8179void MultiTouchInputMapperTest::prepareAxes(int axes) {
8180 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008181 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
8182 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008183 }
8184 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008185 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
8186 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008187 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008188 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
8189 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008190 }
8191 }
8192 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008193 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8194 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008195 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008196 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008197 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008198 }
8199 }
8200 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008201 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
8202 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008203 }
8204 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008205 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
8206 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008207 }
8208 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008209 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
8210 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008211 }
8212 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008213 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
8214 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008215 }
8216 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008217 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
8218 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008219 }
8220 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008221 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008222 }
8223}
8224
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008225void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
8226 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008227 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
8228 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008229}
8230
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008231void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
8232 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008234}
8235
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008236void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
8237 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008238 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008239}
8240
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008241void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008242 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008243}
8244
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008245void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008246 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008247}
8248
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008249void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
8250 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008251 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008252}
8253
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008254void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008255 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008256}
8257
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008258void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008259 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008260}
8261
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008262void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008264}
8265
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008266void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008268}
8269
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008270void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008271 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008272}
8273
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008274void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
8275 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008276 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008277}
8278
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008279void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
8280 int32_t value) {
8281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
8282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
8283}
8284
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008285void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008286 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008287}
8288
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008289void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008291}
8292
Michael Wrightd02c5b62014-02-10 15:10:22 -08008293TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008294 addConfigurationProperty("touch.deviceType", "touchScreen");
8295 prepareDisplay(DISPLAY_ORIENTATION_0);
8296 prepareAxes(POSITION);
8297 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008298 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008299
arthurhungdcef2dc2020-08-11 14:47:50 +08008300 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008301
8302 NotifyMotionArgs motionArgs;
8303
8304 // Two fingers down at once.
8305 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8306 processPosition(mapper, x1, y1);
8307 processMTSync(mapper);
8308 processPosition(mapper, x2, y2);
8309 processMTSync(mapper);
8310 processSync(mapper);
8311
8312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8313 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8314 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8315 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8316 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8317 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8318 ASSERT_EQ(0, motionArgs.flags);
8319 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8320 ASSERT_EQ(0, motionArgs.buttonState);
8321 ASSERT_EQ(0, motionArgs.edgeFlags);
8322 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8323 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8324 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8326 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8327 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8328 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8329 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8330
8331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8332 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8333 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8334 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8335 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008336 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008337 ASSERT_EQ(0, motionArgs.flags);
8338 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8339 ASSERT_EQ(0, motionArgs.buttonState);
8340 ASSERT_EQ(0, motionArgs.edgeFlags);
8341 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8342 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8343 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8344 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8345 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8347 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8349 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8350 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8351 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8352 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8353
8354 // Move.
8355 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8356 processPosition(mapper, x1, y1);
8357 processMTSync(mapper);
8358 processPosition(mapper, x2, y2);
8359 processMTSync(mapper);
8360 processSync(mapper);
8361
8362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8363 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8364 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8365 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8366 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8368 ASSERT_EQ(0, motionArgs.flags);
8369 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8370 ASSERT_EQ(0, motionArgs.buttonState);
8371 ASSERT_EQ(0, motionArgs.edgeFlags);
8372 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8373 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8374 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8375 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8376 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8378 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8380 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8381 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8382 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8383 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8384
8385 // First finger up.
8386 x2 += 15; y2 -= 20;
8387 processPosition(mapper, x2, y2);
8388 processMTSync(mapper);
8389 processSync(mapper);
8390
8391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8392 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8393 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8394 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8395 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008396 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008397 ASSERT_EQ(0, motionArgs.flags);
8398 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8399 ASSERT_EQ(0, motionArgs.buttonState);
8400 ASSERT_EQ(0, motionArgs.edgeFlags);
8401 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8402 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8403 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8404 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8405 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8407 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8408 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8409 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8410 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8411 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8412 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8413
8414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8415 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8416 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8417 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8418 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8420 ASSERT_EQ(0, motionArgs.flags);
8421 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8422 ASSERT_EQ(0, motionArgs.buttonState);
8423 ASSERT_EQ(0, motionArgs.edgeFlags);
8424 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8425 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8426 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8428 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8429 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8430 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8431 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8432
8433 // Move.
8434 x2 += 20; y2 -= 25;
8435 processPosition(mapper, x2, y2);
8436 processMTSync(mapper);
8437 processSync(mapper);
8438
8439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8440 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8441 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8442 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8443 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8445 ASSERT_EQ(0, motionArgs.flags);
8446 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8447 ASSERT_EQ(0, motionArgs.buttonState);
8448 ASSERT_EQ(0, motionArgs.edgeFlags);
8449 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8450 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8451 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8453 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8454 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8455 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8456 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8457
8458 // New finger down.
8459 int32_t x3 = 700, y3 = 300;
8460 processPosition(mapper, x2, y2);
8461 processMTSync(mapper);
8462 processPosition(mapper, x3, y3);
8463 processMTSync(mapper);
8464 processSync(mapper);
8465
8466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8467 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8468 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8469 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8470 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008471 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008472 ASSERT_EQ(0, motionArgs.flags);
8473 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8474 ASSERT_EQ(0, motionArgs.buttonState);
8475 ASSERT_EQ(0, motionArgs.edgeFlags);
8476 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8477 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8478 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8479 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8480 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8481 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8482 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8484 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8485 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8486 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8487 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8488
8489 // Second finger up.
8490 x3 += 30; y3 -= 20;
8491 processPosition(mapper, x3, y3);
8492 processMTSync(mapper);
8493 processSync(mapper);
8494
8495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8496 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8497 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8498 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8499 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008500 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008501 ASSERT_EQ(0, motionArgs.flags);
8502 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8503 ASSERT_EQ(0, motionArgs.buttonState);
8504 ASSERT_EQ(0, motionArgs.edgeFlags);
8505 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8506 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8507 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8508 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8509 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8511 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8513 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8514 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8515 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8516 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8517
8518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8519 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8520 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8521 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8522 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8523 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8524 ASSERT_EQ(0, motionArgs.flags);
8525 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8526 ASSERT_EQ(0, motionArgs.buttonState);
8527 ASSERT_EQ(0, motionArgs.edgeFlags);
8528 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8529 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8530 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8532 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8533 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8534 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8535 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8536
8537 // Last finger up.
8538 processMTSync(mapper);
8539 processSync(mapper);
8540
8541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8542 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8543 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8544 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8545 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8546 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8547 ASSERT_EQ(0, motionArgs.flags);
8548 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8549 ASSERT_EQ(0, motionArgs.buttonState);
8550 ASSERT_EQ(0, motionArgs.edgeFlags);
8551 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8552 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8554 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8555 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8556 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8557 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8558 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8559
8560 // Should not have sent any more keys or motions.
8561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8563}
8564
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008565TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8566 addConfigurationProperty("touch.deviceType", "touchScreen");
8567 prepareDisplay(DISPLAY_ORIENTATION_0);
8568
8569 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8570 /*fuzz*/ 0, /*resolution*/ 10);
8571 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8572 /*fuzz*/ 0, /*resolution*/ 11);
8573 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8574 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8575 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8576 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8577 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8578 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8579 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8580 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8581
8582 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8583
8584 // X and Y axes
8585 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8586 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8587 // Touch major and minor
8588 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8589 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8590 // Tool major and minor
8591 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8592 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8593}
8594
8595TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8596 addConfigurationProperty("touch.deviceType", "touchScreen");
8597 prepareDisplay(DISPLAY_ORIENTATION_0);
8598
8599 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8600 /*fuzz*/ 0, /*resolution*/ 10);
8601 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8602 /*fuzz*/ 0, /*resolution*/ 11);
8603
8604 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8605
8606 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8607
8608 // Touch major and minor
8609 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8610 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8611 // Tool major and minor
8612 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8613 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8614}
8615
Michael Wrightd02c5b62014-02-10 15:10:22 -08008616TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008617 addConfigurationProperty("touch.deviceType", "touchScreen");
8618 prepareDisplay(DISPLAY_ORIENTATION_0);
8619 prepareAxes(POSITION | ID);
8620 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008621 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008622
arthurhungdcef2dc2020-08-11 14:47:50 +08008623 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008624
8625 NotifyMotionArgs motionArgs;
8626
8627 // Two fingers down at once.
8628 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8629 processPosition(mapper, x1, y1);
8630 processId(mapper, 1);
8631 processMTSync(mapper);
8632 processPosition(mapper, x2, y2);
8633 processId(mapper, 2);
8634 processMTSync(mapper);
8635 processSync(mapper);
8636
8637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8638 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8639 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8640 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8641 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8642 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8643 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8644
8645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008646 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008647 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8648 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8649 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8650 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8651 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8652 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8653 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8655 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8656
8657 // Move.
8658 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8659 processPosition(mapper, x1, y1);
8660 processId(mapper, 1);
8661 processMTSync(mapper);
8662 processPosition(mapper, x2, y2);
8663 processId(mapper, 2);
8664 processMTSync(mapper);
8665 processSync(mapper);
8666
8667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8668 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8669 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8670 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8671 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8672 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8673 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8674 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8675 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8677 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8678
8679 // First finger up.
8680 x2 += 15; y2 -= 20;
8681 processPosition(mapper, x2, y2);
8682 processId(mapper, 2);
8683 processMTSync(mapper);
8684 processSync(mapper);
8685
8686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008687 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008688 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8689 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8690 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8691 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8692 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8694 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8696 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8697
8698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8699 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8700 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8701 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8702 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8704 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8705
8706 // Move.
8707 x2 += 20; y2 -= 25;
8708 processPosition(mapper, x2, y2);
8709 processId(mapper, 2);
8710 processMTSync(mapper);
8711 processSync(mapper);
8712
8713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8714 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8715 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8716 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8719 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8720
8721 // New finger down.
8722 int32_t x3 = 700, y3 = 300;
8723 processPosition(mapper, x2, y2);
8724 processId(mapper, 2);
8725 processMTSync(mapper);
8726 processPosition(mapper, x3, y3);
8727 processId(mapper, 3);
8728 processMTSync(mapper);
8729 processSync(mapper);
8730
8731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008732 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008733 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8734 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8735 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8736 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8737 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8739 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8741 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8742
8743 // Second finger up.
8744 x3 += 30; y3 -= 20;
8745 processPosition(mapper, x3, y3);
8746 processId(mapper, 3);
8747 processMTSync(mapper);
8748 processSync(mapper);
8749
8750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008751 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008752 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8753 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8754 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8755 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8756 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8758 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8760 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8761
8762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8763 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8764 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8765 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8766 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8767 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8768 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8769
8770 // Last finger up.
8771 processMTSync(mapper);
8772 processSync(mapper);
8773
8774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8775 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8776 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8777 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8778 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8780 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8781
8782 // Should not have sent any more keys or motions.
8783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8785}
8786
8787TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008788 addConfigurationProperty("touch.deviceType", "touchScreen");
8789 prepareDisplay(DISPLAY_ORIENTATION_0);
8790 prepareAxes(POSITION | ID | SLOT);
8791 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008792 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008793
arthurhungdcef2dc2020-08-11 14:47:50 +08008794 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008795
8796 NotifyMotionArgs motionArgs;
8797
8798 // Two fingers down at once.
8799 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8800 processPosition(mapper, x1, y1);
8801 processId(mapper, 1);
8802 processSlot(mapper, 1);
8803 processPosition(mapper, x2, y2);
8804 processId(mapper, 2);
8805 processSync(mapper);
8806
8807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8808 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8809 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8810 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8811 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8813 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8814
8815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008816 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008817 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8818 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8819 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8820 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8821 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8823 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8825 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8826
8827 // Move.
8828 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8829 processSlot(mapper, 0);
8830 processPosition(mapper, x1, y1);
8831 processSlot(mapper, 1);
8832 processPosition(mapper, x2, y2);
8833 processSync(mapper);
8834
8835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8836 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8837 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8838 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8839 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8840 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8843 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8845 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8846
8847 // First finger up.
8848 x2 += 15; y2 -= 20;
8849 processSlot(mapper, 0);
8850 processId(mapper, -1);
8851 processSlot(mapper, 1);
8852 processPosition(mapper, x2, y2);
8853 processSync(mapper);
8854
8855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008856 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008857 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8858 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8859 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8860 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8861 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8863 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8864 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8865 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8866
8867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8868 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8869 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8870 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8871 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8872 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8873 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8874
8875 // Move.
8876 x2 += 20; y2 -= 25;
8877 processPosition(mapper, x2, y2);
8878 processSync(mapper);
8879
8880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8881 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8882 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8883 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8884 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8886 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8887
8888 // New finger down.
8889 int32_t x3 = 700, y3 = 300;
8890 processPosition(mapper, x2, y2);
8891 processSlot(mapper, 0);
8892 processId(mapper, 3);
8893 processPosition(mapper, x3, y3);
8894 processSync(mapper);
8895
8896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008897 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008898 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8899 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8900 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8901 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8902 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8903 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8904 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8906 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8907
8908 // Second finger up.
8909 x3 += 30; y3 -= 20;
8910 processSlot(mapper, 1);
8911 processId(mapper, -1);
8912 processSlot(mapper, 0);
8913 processPosition(mapper, x3, y3);
8914 processSync(mapper);
8915
8916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008917 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008918 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8919 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8920 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8921 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8922 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8923 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8924 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8926 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8927
8928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8929 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8930 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8931 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8932 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8934 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8935
8936 // Last finger up.
8937 processId(mapper, -1);
8938 processSync(mapper);
8939
8940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8941 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8942 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8943 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8944 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8945 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8946 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8947
8948 // Should not have sent any more keys or motions.
8949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8951}
8952
8953TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008954 addConfigurationProperty("touch.deviceType", "touchScreen");
8955 prepareDisplay(DISPLAY_ORIENTATION_0);
8956 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008957 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008958
8959 // These calculations are based on the input device calibration documentation.
8960 int32_t rawX = 100;
8961 int32_t rawY = 200;
8962 int32_t rawTouchMajor = 7;
8963 int32_t rawTouchMinor = 6;
8964 int32_t rawToolMajor = 9;
8965 int32_t rawToolMinor = 8;
8966 int32_t rawPressure = 11;
8967 int32_t rawDistance = 0;
8968 int32_t rawOrientation = 3;
8969 int32_t id = 5;
8970
8971 float x = toDisplayX(rawX);
8972 float y = toDisplayY(rawY);
8973 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8974 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8975 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8976 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8977 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8978 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8979 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8980 float distance = float(rawDistance);
8981
8982 processPosition(mapper, rawX, rawY);
8983 processTouchMajor(mapper, rawTouchMajor);
8984 processTouchMinor(mapper, rawTouchMinor);
8985 processToolMajor(mapper, rawToolMajor);
8986 processToolMinor(mapper, rawToolMinor);
8987 processPressure(mapper, rawPressure);
8988 processOrientation(mapper, rawOrientation);
8989 processDistance(mapper, rawDistance);
8990 processId(mapper, id);
8991 processMTSync(mapper);
8992 processSync(mapper);
8993
8994 NotifyMotionArgs args;
8995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8996 ASSERT_EQ(0, args.pointerProperties[0].id);
8997 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8998 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8999 orientation, distance));
9000}
9001
9002TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009003 addConfigurationProperty("touch.deviceType", "touchScreen");
9004 prepareDisplay(DISPLAY_ORIENTATION_0);
9005 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
9006 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009007 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009008
9009 // These calculations are based on the input device calibration documentation.
9010 int32_t rawX = 100;
9011 int32_t rawY = 200;
9012 int32_t rawTouchMajor = 140;
9013 int32_t rawTouchMinor = 120;
9014 int32_t rawToolMajor = 180;
9015 int32_t rawToolMinor = 160;
9016
9017 float x = toDisplayX(rawX);
9018 float y = toDisplayY(rawY);
9019 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
9020 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
9021 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
9022 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
9023 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
9024
9025 processPosition(mapper, rawX, rawY);
9026 processTouchMajor(mapper, rawTouchMajor);
9027 processTouchMinor(mapper, rawTouchMinor);
9028 processToolMajor(mapper, rawToolMajor);
9029 processToolMinor(mapper, rawToolMinor);
9030 processMTSync(mapper);
9031 processSync(mapper);
9032
9033 NotifyMotionArgs args;
9034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9035 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9036 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
9037}
9038
9039TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009040 addConfigurationProperty("touch.deviceType", "touchScreen");
9041 prepareDisplay(DISPLAY_ORIENTATION_0);
9042 prepareAxes(POSITION | TOUCH | TOOL);
9043 addConfigurationProperty("touch.size.calibration", "diameter");
9044 addConfigurationProperty("touch.size.scale", "10");
9045 addConfigurationProperty("touch.size.bias", "160");
9046 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009047 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009048
9049 // These calculations are based on the input device calibration documentation.
9050 // Note: We only provide a single common touch/tool value because the device is assumed
9051 // not to emit separate values for each pointer (isSummed = 1).
9052 int32_t rawX = 100;
9053 int32_t rawY = 200;
9054 int32_t rawX2 = 150;
9055 int32_t rawY2 = 250;
9056 int32_t rawTouchMajor = 5;
9057 int32_t rawToolMajor = 8;
9058
9059 float x = toDisplayX(rawX);
9060 float y = toDisplayY(rawY);
9061 float x2 = toDisplayX(rawX2);
9062 float y2 = toDisplayY(rawY2);
9063 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
9064 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
9065 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
9066
9067 processPosition(mapper, rawX, rawY);
9068 processTouchMajor(mapper, rawTouchMajor);
9069 processToolMajor(mapper, rawToolMajor);
9070 processMTSync(mapper);
9071 processPosition(mapper, rawX2, rawY2);
9072 processTouchMajor(mapper, rawTouchMajor);
9073 processToolMajor(mapper, rawToolMajor);
9074 processMTSync(mapper);
9075 processSync(mapper);
9076
9077 NotifyMotionArgs args;
9078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9079 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9080
9081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009082 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009083 ASSERT_EQ(size_t(2), args.pointerCount);
9084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9085 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
9086 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
9087 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
9088}
9089
9090TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009091 addConfigurationProperty("touch.deviceType", "touchScreen");
9092 prepareDisplay(DISPLAY_ORIENTATION_0);
9093 prepareAxes(POSITION | TOUCH | TOOL);
9094 addConfigurationProperty("touch.size.calibration", "area");
9095 addConfigurationProperty("touch.size.scale", "43");
9096 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009097 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009098
9099 // These calculations are based on the input device calibration documentation.
9100 int32_t rawX = 100;
9101 int32_t rawY = 200;
9102 int32_t rawTouchMajor = 5;
9103 int32_t rawToolMajor = 8;
9104
9105 float x = toDisplayX(rawX);
9106 float y = toDisplayY(rawY);
9107 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
9108 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
9109 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
9110
9111 processPosition(mapper, rawX, rawY);
9112 processTouchMajor(mapper, rawTouchMajor);
9113 processToolMajor(mapper, rawToolMajor);
9114 processMTSync(mapper);
9115 processSync(mapper);
9116
9117 NotifyMotionArgs args;
9118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9119 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9120 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
9121}
9122
9123TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009124 addConfigurationProperty("touch.deviceType", "touchScreen");
9125 prepareDisplay(DISPLAY_ORIENTATION_0);
9126 prepareAxes(POSITION | PRESSURE);
9127 addConfigurationProperty("touch.pressure.calibration", "amplitude");
9128 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009129 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009130
Michael Wrightaa449c92017-12-13 21:21:43 +00009131 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009132 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00009133 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
9134 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
9135 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
9136
Michael Wrightd02c5b62014-02-10 15:10:22 -08009137 // These calculations are based on the input device calibration documentation.
9138 int32_t rawX = 100;
9139 int32_t rawY = 200;
9140 int32_t rawPressure = 60;
9141
9142 float x = toDisplayX(rawX);
9143 float y = toDisplayY(rawY);
9144 float pressure = float(rawPressure) * 0.01f;
9145
9146 processPosition(mapper, rawX, rawY);
9147 processPressure(mapper, rawPressure);
9148 processMTSync(mapper);
9149 processSync(mapper);
9150
9151 NotifyMotionArgs args;
9152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9154 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
9155}
9156
9157TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009158 addConfigurationProperty("touch.deviceType", "touchScreen");
9159 prepareDisplay(DISPLAY_ORIENTATION_0);
9160 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009161 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009162
9163 NotifyMotionArgs motionArgs;
9164 NotifyKeyArgs keyArgs;
9165
9166 processId(mapper, 1);
9167 processPosition(mapper, 100, 200);
9168 processSync(mapper);
9169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9170 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9171 ASSERT_EQ(0, motionArgs.buttonState);
9172
9173 // press BTN_LEFT, release BTN_LEFT
9174 processKey(mapper, BTN_LEFT, 1);
9175 processSync(mapper);
9176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9177 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9178 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
9179
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9181 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9182 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
9183
Michael Wrightd02c5b62014-02-10 15:10:22 -08009184 processKey(mapper, BTN_LEFT, 0);
9185 processSync(mapper);
9186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009187 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009188 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009189
9190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009191 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009192 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009193
9194 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
9195 processKey(mapper, BTN_RIGHT, 1);
9196 processKey(mapper, BTN_MIDDLE, 1);
9197 processSync(mapper);
9198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9199 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9200 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
9201 motionArgs.buttonState);
9202
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9204 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9205 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
9206
9207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9208 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9209 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
9210 motionArgs.buttonState);
9211
Michael Wrightd02c5b62014-02-10 15:10:22 -08009212 processKey(mapper, BTN_RIGHT, 0);
9213 processSync(mapper);
9214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009215 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009216 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009217
9218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009219 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009220 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009221
9222 processKey(mapper, BTN_MIDDLE, 0);
9223 processSync(mapper);
9224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009225 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009226 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009227
9228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009230 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009231
9232 // press BTN_BACK, release BTN_BACK
9233 processKey(mapper, BTN_BACK, 1);
9234 processSync(mapper);
9235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9236 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9237 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009238
Michael Wrightd02c5b62014-02-10 15:10:22 -08009239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009241 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
9242
9243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9244 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9245 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009246
9247 processKey(mapper, BTN_BACK, 0);
9248 processSync(mapper);
9249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009250 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009251 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009252
9253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009254 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009255 ASSERT_EQ(0, motionArgs.buttonState);
9256
Michael Wrightd02c5b62014-02-10 15:10:22 -08009257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9258 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9259 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
9260
9261 // press BTN_SIDE, release BTN_SIDE
9262 processKey(mapper, BTN_SIDE, 1);
9263 processSync(mapper);
9264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9265 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9266 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009267
Michael Wrightd02c5b62014-02-10 15:10:22 -08009268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009270 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
9271
9272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9273 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9274 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009275
9276 processKey(mapper, BTN_SIDE, 0);
9277 processSync(mapper);
9278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009279 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009280 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009281
9282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009284 ASSERT_EQ(0, motionArgs.buttonState);
9285
Michael Wrightd02c5b62014-02-10 15:10:22 -08009286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9287 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9288 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
9289
9290 // press BTN_FORWARD, release BTN_FORWARD
9291 processKey(mapper, BTN_FORWARD, 1);
9292 processSync(mapper);
9293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9294 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9295 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009296
Michael Wrightd02c5b62014-02-10 15:10:22 -08009297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009299 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9300
9301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9302 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9303 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009304
9305 processKey(mapper, BTN_FORWARD, 0);
9306 processSync(mapper);
9307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009308 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009309 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009310
9311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009312 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009313 ASSERT_EQ(0, motionArgs.buttonState);
9314
Michael Wrightd02c5b62014-02-10 15:10:22 -08009315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9316 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9317 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9318
9319 // press BTN_EXTRA, release BTN_EXTRA
9320 processKey(mapper, BTN_EXTRA, 1);
9321 processSync(mapper);
9322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9323 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9324 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009325
Michael Wrightd02c5b62014-02-10 15:10:22 -08009326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009328 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9329
9330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9331 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9332 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009333
9334 processKey(mapper, BTN_EXTRA, 0);
9335 processSync(mapper);
9336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009337 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009338 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009339
9340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009342 ASSERT_EQ(0, motionArgs.buttonState);
9343
Michael Wrightd02c5b62014-02-10 15:10:22 -08009344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9345 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9346 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9347
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9349
Michael Wrightd02c5b62014-02-10 15:10:22 -08009350 // press BTN_STYLUS, release BTN_STYLUS
9351 processKey(mapper, BTN_STYLUS, 1);
9352 processSync(mapper);
9353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009355 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
9356
9357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9358 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9359 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009360
9361 processKey(mapper, BTN_STYLUS, 0);
9362 processSync(mapper);
9363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009364 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009365 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009366
9367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009369 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009370
9371 // press BTN_STYLUS2, release BTN_STYLUS2
9372 processKey(mapper, BTN_STYLUS2, 1);
9373 processSync(mapper);
9374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009376 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
9377
9378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9379 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9380 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009381
9382 processKey(mapper, BTN_STYLUS2, 0);
9383 processSync(mapper);
9384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009385 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009386 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009387
9388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009390 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009391
9392 // release touch
9393 processId(mapper, -1);
9394 processSync(mapper);
9395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9396 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9397 ASSERT_EQ(0, motionArgs.buttonState);
9398}
9399
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009400TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
9401 addConfigurationProperty("touch.deviceType", "touchScreen");
9402 prepareDisplay(DISPLAY_ORIENTATION_0);
9403 prepareAxes(POSITION | ID | SLOT);
9404 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9405
9406 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
9407 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
9408
9409 // Touch down.
9410 processId(mapper, 1);
9411 processPosition(mapper, 100, 200);
9412 processSync(mapper);
9413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9414 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
9415
9416 // Press and release button mapped to the primary stylus button.
9417 processKey(mapper, BTN_A, 1);
9418 processSync(mapper);
9419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9420 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9421 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9423 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9424 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9425
9426 processKey(mapper, BTN_A, 0);
9427 processSync(mapper);
9428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9429 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9431 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9432
9433 // Press and release the HID usage mapped to the secondary stylus button.
9434 processHidUsage(mapper, 0xabcd, 1);
9435 processSync(mapper);
9436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9437 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9438 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9440 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9441 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9442
9443 processHidUsage(mapper, 0xabcd, 0);
9444 processSync(mapper);
9445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9446 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9448 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9449
9450 // Release touch.
9451 processId(mapper, -1);
9452 processSync(mapper);
9453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9454 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
9455}
9456
Michael Wrightd02c5b62014-02-10 15:10:22 -08009457TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009458 addConfigurationProperty("touch.deviceType", "touchScreen");
9459 prepareDisplay(DISPLAY_ORIENTATION_0);
9460 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009461 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009462
9463 NotifyMotionArgs motionArgs;
9464
9465 // default tool type is finger
9466 processId(mapper, 1);
9467 processPosition(mapper, 100, 200);
9468 processSync(mapper);
9469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9470 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9472
9473 // eraser
9474 processKey(mapper, BTN_TOOL_RUBBER, 1);
9475 processSync(mapper);
9476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9477 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9478 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
9479
9480 // stylus
9481 processKey(mapper, BTN_TOOL_RUBBER, 0);
9482 processKey(mapper, BTN_TOOL_PEN, 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_STYLUS, motionArgs.pointerProperties[0].toolType);
9487
9488 // brush
9489 processKey(mapper, BTN_TOOL_PEN, 0);
9490 processKey(mapper, BTN_TOOL_BRUSH, 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 // pencil
9497 processKey(mapper, BTN_TOOL_BRUSH, 0);
9498 processKey(mapper, BTN_TOOL_PENCIL, 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
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009504 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009505 processKey(mapper, BTN_TOOL_PENCIL, 0);
9506 processKey(mapper, BTN_TOOL_AIRBRUSH, 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
9512 // mouse
9513 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9514 processKey(mapper, BTN_TOOL_MOUSE, 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_MOUSE, motionArgs.pointerProperties[0].toolType);
9519
9520 // lens
9521 processKey(mapper, BTN_TOOL_MOUSE, 0);
9522 processKey(mapper, BTN_TOOL_LENS, 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 // double-tap
9529 processKey(mapper, BTN_TOOL_LENS, 0);
9530 processKey(mapper, BTN_TOOL_DOUBLETAP, 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_FINGER, motionArgs.pointerProperties[0].toolType);
9535
9536 // triple-tap
9537 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9538 processKey(mapper, BTN_TOOL_TRIPLETAP, 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 // quad-tap
9545 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9546 processKey(mapper, BTN_TOOL_QUADTAP, 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 // finger
9553 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9554 processKey(mapper, BTN_TOOL_FINGER, 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 // stylus trumps finger
9561 processKey(mapper, BTN_TOOL_PEN, 1);
9562 processSync(mapper);
9563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9564 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9565 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9566
9567 // eraser trumps stylus
9568 processKey(mapper, BTN_TOOL_RUBBER, 1);
9569 processSync(mapper);
9570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
9573
9574 // mouse trumps eraser
9575 processKey(mapper, BTN_TOOL_MOUSE, 1);
9576 processSync(mapper);
9577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9578 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9579 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
9580
9581 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9582 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9583 processSync(mapper);
9584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9585 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9586 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9587
9588 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9589 processToolType(mapper, MT_TOOL_PEN);
9590 processSync(mapper);
9591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9592 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9593 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9594
9595 // back to default tool type
9596 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9597 processKey(mapper, BTN_TOOL_MOUSE, 0);
9598 processKey(mapper, BTN_TOOL_RUBBER, 0);
9599 processKey(mapper, BTN_TOOL_PEN, 0);
9600 processKey(mapper, BTN_TOOL_FINGER, 0);
9601 processSync(mapper);
9602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9603 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9604 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9605}
9606
9607TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009608 addConfigurationProperty("touch.deviceType", "touchScreen");
9609 prepareDisplay(DISPLAY_ORIENTATION_0);
9610 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009611 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009612 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009613
9614 NotifyMotionArgs motionArgs;
9615
9616 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9617 processId(mapper, 1);
9618 processPosition(mapper, 100, 200);
9619 processSync(mapper);
9620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9621 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9623 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9624
9625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9626 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9628 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9629
9630 // move a little
9631 processPosition(mapper, 150, 250);
9632 processSync(mapper);
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(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9637
9638 // down when BTN_TOUCH is pressed, pressure defaults to 1
9639 processKey(mapper, BTN_TOUCH, 1);
9640 processSync(mapper);
9641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9642 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9647 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9649 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9650
9651 // up when BTN_TOUCH is released, hover restored
9652 processKey(mapper, BTN_TOUCH, 0);
9653 processSync(mapper);
9654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9655 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9660 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9662 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9663
9664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9665 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9667 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9668
9669 // exit hover when pointer goes away
9670 processId(mapper, -1);
9671 processSync(mapper);
9672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9673 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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
9678TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009679 addConfigurationProperty("touch.deviceType", "touchScreen");
9680 prepareDisplay(DISPLAY_ORIENTATION_0);
9681 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009682 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009683
9684 NotifyMotionArgs motionArgs;
9685
9686 // initially hovering because pressure is 0
9687 processId(mapper, 1);
9688 processPosition(mapper, 100, 200);
9689 processPressure(mapper, 0);
9690 processSync(mapper);
9691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9692 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9694 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9695
9696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9697 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9699 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9700
9701 // move a little
9702 processPosition(mapper, 150, 250);
9703 processSync(mapper);
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(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9708
9709 // down when pressure becomes non-zero
9710 processPressure(mapper, RAW_PRESSURE_MAX);
9711 processSync(mapper);
9712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9713 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9718 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9719 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9720 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9721
9722 // up when pressure becomes 0, hover restored
9723 processPressure(mapper, 0);
9724 processSync(mapper);
9725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9726 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9731 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9733 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9734
9735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9736 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9738 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9739
9740 // exit hover when pointer goes away
9741 processId(mapper, -1);
9742 processSync(mapper);
9743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9744 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, 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
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009749/**
9750 * Set the input device port <--> display port associations, and check that the
9751 * events are routed to the display that matches the display port.
9752 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9753 */
9754TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009755 const std::string usb2 = "USB2";
9756 const uint8_t hdmi1 = 0;
9757 const uint8_t hdmi2 = 1;
9758 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009759 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009760
9761 addConfigurationProperty("touch.deviceType", "touchScreen");
9762 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009763 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009764
9765 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9766 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9767
9768 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9769 // for this input device is specified, and the matching viewport is not present,
9770 // the input device should be disabled (at the mapper level).
9771
9772 // Add viewport for display 2 on hdmi2
9773 prepareSecondaryDisplay(type, hdmi2);
9774 // Send a touch event
9775 processPosition(mapper, 100, 100);
9776 processSync(mapper);
9777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9778
9779 // Add viewport for display 1 on hdmi1
9780 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
9781 // Send a touch event again
9782 processPosition(mapper, 100, 100);
9783 processSync(mapper);
9784
9785 NotifyMotionArgs args;
9786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9787 ASSERT_EQ(DISPLAY_ID, args.displayId);
9788}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009789
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009790TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9791 addConfigurationProperty("touch.deviceType", "touchScreen");
9792 prepareAxes(POSITION);
9793 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9794
9795 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9796
9797 prepareDisplay(DISPLAY_ORIENTATION_0);
9798 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9799
9800 // Send a touch event
9801 processPosition(mapper, 100, 100);
9802 processSync(mapper);
9803
9804 NotifyMotionArgs args;
9805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9806 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9807}
9808
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009809TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009810 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009811 std::shared_ptr<FakePointerController> fakePointerController =
9812 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009813 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009814 fakePointerController->setPosition(100, 200);
9815 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009816 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009817
Garfield Tan888a6a42020-01-09 11:39:16 -08009818 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009819 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009820
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009821 prepareDisplay(DISPLAY_ORIENTATION_0);
9822 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009823 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009824
Harry Cutts16a24cc2022-10-26 15:22:19 +00009825 // Check source is a touchpad that would obtain the PointerController.
9826 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009827
9828 NotifyMotionArgs motionArgs;
9829 processPosition(mapper, 100, 100);
9830 processSync(mapper);
9831
9832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9833 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9834 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9835}
9836
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009837/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009838 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9839 */
9840TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9841 addConfigurationProperty("touch.deviceType", "touchScreen");
9842 prepareAxes(POSITION);
9843 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9844
9845 prepareDisplay(DISPLAY_ORIENTATION_0);
9846 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
9847 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
9848 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
9849 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9850
9851 NotifyMotionArgs args;
9852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9853 ASSERT_EQ(26, args.readTime);
9854
9855 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
9856 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
9857 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9858
9859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9860 ASSERT_EQ(33, args.readTime);
9861}
9862
9863/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009864 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9865 * events should not be delivered to the listener.
9866 */
9867TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9868 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009869 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009870 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9871 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
9872 ViewportType::INTERNAL);
9873 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9874 prepareAxes(POSITION);
9875 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9876
9877 NotifyMotionArgs motionArgs;
9878 processPosition(mapper, 100, 100);
9879 processSync(mapper);
9880
9881 mFakeListener->assertNotifyMotionWasNotCalled();
9882}
9883
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009884/**
9885 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9886 * the touch mapper can process the events and the events can be delivered to the listener.
9887 */
9888TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9889 addConfigurationProperty("touch.deviceType", "touchScreen");
9890 addConfigurationProperty("touch.enableForInactiveViewport", "1");
9891 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9892 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
9893 ViewportType::INTERNAL);
9894 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9895 prepareAxes(POSITION);
9896 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9897
9898 NotifyMotionArgs motionArgs;
9899 processPosition(mapper, 100, 100);
9900 processSync(mapper);
9901
9902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9903 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9904}
9905
Garfield Tanc734e4f2021-01-15 20:01:39 -08009906TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9907 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009908 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Garfield Tanc734e4f2021-01-15 20:01:39 -08009909 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9910 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
9911 ViewportType::INTERNAL);
9912 std::optional<DisplayViewport> optionalDisplayViewport =
9913 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9914 ASSERT_TRUE(optionalDisplayViewport.has_value());
9915 DisplayViewport displayViewport = *optionalDisplayViewport;
9916
9917 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9918 prepareAxes(POSITION);
9919 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9920
9921 // Finger down
9922 int32_t x = 100, y = 100;
9923 processPosition(mapper, x, y);
9924 processSync(mapper);
9925
9926 NotifyMotionArgs motionArgs;
9927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9928 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9929
9930 // Deactivate display viewport
9931 displayViewport.isActive = false;
9932 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9933 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9934
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009935 // The ongoing touch should be canceled immediately
9936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9937 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9938
9939 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009940 x += 10, y += 10;
9941 processPosition(mapper, x, y);
9942 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009944
9945 // Reactivate display viewport
9946 displayViewport.isActive = true;
9947 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9948 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9949
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009950 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009951 x += 10, y += 10;
9952 processPosition(mapper, x, y);
9953 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9955 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009956}
9957
Arthur Hung7c645402019-01-25 17:45:42 +08009958TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9959 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009960 prepareAxes(POSITION | ID | SLOT);
9961 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009962 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009963
9964 // Create the second touch screen device, and enable multi fingers.
9965 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009966 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009967 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009968 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009969 std::shared_ptr<InputDevice> device2 =
9970 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009971 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009972
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009973 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9974 0 /*flat*/, 0 /*fuzz*/);
9975 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9976 0 /*flat*/, 0 /*fuzz*/);
9977 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
9978 0 /*flat*/, 0 /*fuzz*/);
9979 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
9980 0 /*flat*/, 0 /*fuzz*/);
9981 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
9982 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9983 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009984
9985 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009986 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009987 std::list<NotifyArgs> unused =
9988 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9989 0 /*changes*/);
9990 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009991
9992 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009993 std::shared_ptr<FakePointerController> fakePointerController =
9994 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009995 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009996
9997 // Setup policy for associated displays and show touches.
9998 const uint8_t hdmi1 = 0;
9999 const uint8_t hdmi2 = 1;
10000 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
10001 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
10002 mFakePolicy->setShowTouches(true);
10003
10004 // Create displays.
10005 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010006 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +080010007
10008 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010009 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10010 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
10011 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +080010012
10013 // Two fingers down at default display.
10014 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
10015 processPosition(mapper, x1, y1);
10016 processId(mapper, 1);
10017 processSlot(mapper, 1);
10018 processPosition(mapper, x2, y2);
10019 processId(mapper, 2);
10020 processSync(mapper);
10021
10022 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
10023 fakePointerController->getSpots().find(DISPLAY_ID);
10024 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
10025 ASSERT_EQ(size_t(2), iter->second.size());
10026
10027 // Two fingers down at second display.
10028 processPosition(mapper2, x1, y1);
10029 processId(mapper2, 1);
10030 processSlot(mapper2, 1);
10031 processPosition(mapper2, x2, y2);
10032 processId(mapper2, 2);
10033 processSync(mapper2);
10034
10035 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
10036 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
10037 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +000010038
10039 // Disable the show touches configuration and ensure the spots are cleared.
10040 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010041 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10042 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +000010043
10044 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +080010045}
10046
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010047TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010048 prepareAxes(POSITION);
10049 addConfigurationProperty("touch.deviceType", "touchScreen");
10050 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010051 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010052
10053 NotifyMotionArgs motionArgs;
10054 // Unrotated video frame
10055 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10056 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010057 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010058 processPosition(mapper, 100, 200);
10059 processSync(mapper);
10060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10061 ASSERT_EQ(frames, motionArgs.videoFrames);
10062
10063 // Subsequent touch events should not have any videoframes
10064 // This is implemented separately in FakeEventHub,
10065 // but that should match the behaviour of TouchVideoDevice.
10066 processPosition(mapper, 200, 200);
10067 processSync(mapper);
10068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10069 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
10070}
10071
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010072TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010073 prepareAxes(POSITION);
10074 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010075 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010076 // Unrotated video frame
10077 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10078 NotifyMotionArgs motionArgs;
10079
10080 // Test all 4 orientations
10081 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010082 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
10083 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
10084 clearViewports();
10085 prepareDisplay(orientation);
10086 std::vector<TouchVideoFrame> frames{frame};
10087 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
10088 processPosition(mapper, 100, 200);
10089 processSync(mapper);
10090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10091 ASSERT_EQ(frames, motionArgs.videoFrames);
10092 }
10093}
10094
10095TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
10096 prepareAxes(POSITION);
10097 addConfigurationProperty("touch.deviceType", "touchScreen");
10098 // Since InputReader works in the un-rotated coordinate space, only devices that are not
10099 // orientation-aware are affected by display rotation.
10100 addConfigurationProperty("touch.orientationAware", "0");
10101 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10102 // Unrotated video frame
10103 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10104 NotifyMotionArgs motionArgs;
10105
10106 // Test all 4 orientations
10107 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010108 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
10109 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
10110 clearViewports();
10111 prepareDisplay(orientation);
10112 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010113 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010114 processPosition(mapper, 100, 200);
10115 processSync(mapper);
10116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010117 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
10118 // compared to the display. This is so that when the window transform (which contains the
10119 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
10120 // window's coordinate space.
10121 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010122 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +080010123
10124 // Release finger.
10125 processSync(mapper);
10126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010127 }
10128}
10129
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010130TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010131 prepareAxes(POSITION);
10132 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010133 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010134 // Unrotated video frames. There's no rule that they must all have the same dimensions,
10135 // so mix these.
10136 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10137 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
10138 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
10139 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
10140 NotifyMotionArgs motionArgs;
10141
10142 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010143 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010144 processPosition(mapper, 100, 200);
10145 processSync(mapper);
10146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010147 ASSERT_EQ(frames, motionArgs.videoFrames);
10148}
10149
10150TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
10151 prepareAxes(POSITION);
10152 addConfigurationProperty("touch.deviceType", "touchScreen");
10153 // Since InputReader works in the un-rotated coordinate space, only devices that are not
10154 // orientation-aware are affected by display rotation.
10155 addConfigurationProperty("touch.orientationAware", "0");
10156 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10157 // Unrotated video frames. There's no rule that they must all have the same dimensions,
10158 // so mix these.
10159 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10160 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
10161 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
10162 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
10163 NotifyMotionArgs motionArgs;
10164
10165 prepareDisplay(DISPLAY_ORIENTATION_90);
10166 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
10167 processPosition(mapper, 100, 200);
10168 processSync(mapper);
10169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10170 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
10171 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
10172 // compared to the display. This is so that when the window transform (which contains the
10173 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
10174 // window's coordinate space.
10175 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
10176 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010177 ASSERT_EQ(frames, motionArgs.videoFrames);
10178}
10179
Arthur Hung9da14732019-09-02 16:16:58 +080010180/**
10181 * If we had defined port associations, but the viewport is not ready, the touch device would be
10182 * expected to be disabled, and it should be enabled after the viewport has found.
10183 */
10184TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +080010185 constexpr uint8_t hdmi2 = 1;
10186 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010187 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +080010188
10189 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
10190
10191 addConfigurationProperty("touch.deviceType", "touchScreen");
10192 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010193 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +080010194
10195 ASSERT_EQ(mDevice->isEnabled(), false);
10196
10197 // Add display on hdmi2, the device should be enabled and can receive touch event.
10198 prepareSecondaryDisplay(type, hdmi2);
10199 ASSERT_EQ(mDevice->isEnabled(), true);
10200
10201 // Send a touch event.
10202 processPosition(mapper, 100, 100);
10203 processSync(mapper);
10204
10205 NotifyMotionArgs args;
10206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10207 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
10208}
10209
Arthur Hung421eb1c2020-01-16 00:09:42 +080010210TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010211 addConfigurationProperty("touch.deviceType", "touchScreen");
10212 prepareDisplay(DISPLAY_ORIENTATION_0);
10213 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010214 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010215
10216 NotifyMotionArgs motionArgs;
10217
10218 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10219 // finger down
10220 processId(mapper, 1);
10221 processPosition(mapper, x1, y1);
10222 processSync(mapper);
10223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10224 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10225 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10226
10227 // finger move
10228 processId(mapper, 1);
10229 processPosition(mapper, x2, y2);
10230 processSync(mapper);
10231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10233 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10234
10235 // finger up.
10236 processId(mapper, -1);
10237 processSync(mapper);
10238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10239 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10240 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10241
10242 // new finger down
10243 processId(mapper, 1);
10244 processPosition(mapper, x3, y3);
10245 processSync(mapper);
10246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10247 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10248 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10249}
10250
10251/**
arthurhungcc7f9802020-04-30 17:55:40 +080010252 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
10253 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +080010254 */
arthurhungcc7f9802020-04-30 17:55:40 +080010255TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010256 addConfigurationProperty("touch.deviceType", "touchScreen");
10257 prepareDisplay(DISPLAY_ORIENTATION_0);
10258 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010259 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010260
10261 NotifyMotionArgs motionArgs;
10262
10263 // default tool type is finger
10264 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +080010265 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010266 processPosition(mapper, x1, y1);
10267 processSync(mapper);
10268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10269 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10270 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10271
10272 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
10273 processToolType(mapper, MT_TOOL_PALM);
10274 processSync(mapper);
10275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10276 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10277
10278 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +080010279 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010280 processPosition(mapper, x2, y2);
10281 processSync(mapper);
10282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10283
10284 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +080010285 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010286 processSync(mapper);
10287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10288
10289 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +080010290 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010291 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010292 processPosition(mapper, x3, y3);
10293 processSync(mapper);
10294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10295 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10296 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10297}
10298
arthurhungbf89a482020-04-17 17:37:55 +080010299/**
arthurhungcc7f9802020-04-30 17:55:40 +080010300 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10301 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +080010302 */
arthurhungcc7f9802020-04-30 17:55:40 +080010303TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +080010304 addConfigurationProperty("touch.deviceType", "touchScreen");
10305 prepareDisplay(DISPLAY_ORIENTATION_0);
10306 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
10307 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10308
10309 NotifyMotionArgs motionArgs;
10310
10311 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +080010312 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10313 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010314 processPosition(mapper, x1, y1);
10315 processSync(mapper);
10316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10317 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10318 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10319
10320 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +080010321 processSlot(mapper, SECOND_SLOT);
10322 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010323 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +080010324 processSync(mapper);
10325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010326 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010327 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
10328
10329 // If the tool type of the first finger changes to MT_TOOL_PALM,
10330 // we expect to receive ACTION_POINTER_UP with cancel flag.
10331 processSlot(mapper, FIRST_SLOT);
10332 processId(mapper, FIRST_TRACKING_ID);
10333 processToolType(mapper, MT_TOOL_PALM);
10334 processSync(mapper);
10335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010336 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010337 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10338
10339 // The following MOVE events of second finger should be processed.
10340 processSlot(mapper, SECOND_SLOT);
10341 processId(mapper, SECOND_TRACKING_ID);
10342 processPosition(mapper, x2 + 1, y2 + 1);
10343 processSync(mapper);
10344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10346 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10347
10348 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
10349 // it. Second finger receive move.
10350 processSlot(mapper, FIRST_SLOT);
10351 processId(mapper, INVALID_TRACKING_ID);
10352 processSync(mapper);
10353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10355 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10356
10357 // Second finger keeps moving.
10358 processSlot(mapper, SECOND_SLOT);
10359 processId(mapper, SECOND_TRACKING_ID);
10360 processPosition(mapper, x2 + 2, y2 + 2);
10361 processSync(mapper);
10362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10363 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10364 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10365
10366 // Second finger up.
10367 processId(mapper, INVALID_TRACKING_ID);
10368 processSync(mapper);
10369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10370 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10371 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10372}
10373
10374/**
10375 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
10376 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
10377 */
10378TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
10379 addConfigurationProperty("touch.deviceType", "touchScreen");
10380 prepareDisplay(DISPLAY_ORIENTATION_0);
10381 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
10382 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10383
10384 NotifyMotionArgs motionArgs;
10385
10386 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10387 // First finger down.
10388 processId(mapper, FIRST_TRACKING_ID);
10389 processPosition(mapper, x1, y1);
10390 processSync(mapper);
10391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10392 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10394
10395 // Second finger down.
10396 processSlot(mapper, SECOND_SLOT);
10397 processId(mapper, SECOND_TRACKING_ID);
10398 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +080010399 processSync(mapper);
10400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010401 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +080010402 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10403
arthurhungcc7f9802020-04-30 17:55:40 +080010404 // If the tool type of the first finger changes to MT_TOOL_PALM,
10405 // we expect to receive ACTION_POINTER_UP with cancel flag.
10406 processSlot(mapper, FIRST_SLOT);
10407 processId(mapper, FIRST_TRACKING_ID);
10408 processToolType(mapper, MT_TOOL_PALM);
10409 processSync(mapper);
10410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010411 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010412 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10413
10414 // Second finger keeps moving.
10415 processSlot(mapper, SECOND_SLOT);
10416 processId(mapper, SECOND_TRACKING_ID);
10417 processPosition(mapper, x2 + 1, y2 + 1);
10418 processSync(mapper);
10419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10420 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10421
10422 // second finger becomes palm, receive cancel due to only 1 finger is active.
10423 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010424 processToolType(mapper, MT_TOOL_PALM);
10425 processSync(mapper);
10426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10427 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10428
arthurhungcc7f9802020-04-30 17:55:40 +080010429 // third finger down.
10430 processSlot(mapper, THIRD_SLOT);
10431 processId(mapper, THIRD_TRACKING_ID);
10432 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +080010433 processPosition(mapper, x3, y3);
10434 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +080010435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10436 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10437 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010438 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10439
10440 // third finger move
10441 processId(mapper, THIRD_TRACKING_ID);
10442 processPosition(mapper, x3 + 1, y3 + 1);
10443 processSync(mapper);
10444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10445 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10446
10447 // first finger up, third finger receive move.
10448 processSlot(mapper, FIRST_SLOT);
10449 processId(mapper, INVALID_TRACKING_ID);
10450 processSync(mapper);
10451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10453 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10454
10455 // second finger up, third finger receive move.
10456 processSlot(mapper, SECOND_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 // third finger up.
10464 processSlot(mapper, THIRD_SLOT);
10465 processId(mapper, INVALID_TRACKING_ID);
10466 processSync(mapper);
10467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10468 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10469 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10470}
10471
10472/**
10473 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10474 * and the active finger could still be allowed to receive the events
10475 */
10476TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
10477 addConfigurationProperty("touch.deviceType", "touchScreen");
10478 prepareDisplay(DISPLAY_ORIENTATION_0);
10479 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
10480 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10481
10482 NotifyMotionArgs motionArgs;
10483
10484 // default tool type is finger
10485 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10486 processId(mapper, FIRST_TRACKING_ID);
10487 processPosition(mapper, x1, y1);
10488 processSync(mapper);
10489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10490 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10491 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10492
10493 // Second finger down.
10494 processSlot(mapper, SECOND_SLOT);
10495 processId(mapper, SECOND_TRACKING_ID);
10496 processPosition(mapper, x2, y2);
10497 processSync(mapper);
10498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010499 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010500 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10501
10502 // If the tool type of the second finger changes to MT_TOOL_PALM,
10503 // we expect to receive ACTION_POINTER_UP with cancel flag.
10504 processId(mapper, SECOND_TRACKING_ID);
10505 processToolType(mapper, MT_TOOL_PALM);
10506 processSync(mapper);
10507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010508 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010509 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10510
10511 // The following MOVE event should be processed.
10512 processSlot(mapper, FIRST_SLOT);
10513 processId(mapper, FIRST_TRACKING_ID);
10514 processPosition(mapper, x1 + 1, y1 + 1);
10515 processSync(mapper);
10516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10517 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10518 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10519
10520 // second finger up.
10521 processSlot(mapper, SECOND_SLOT);
10522 processId(mapper, INVALID_TRACKING_ID);
10523 processSync(mapper);
10524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10525 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10526
10527 // first finger keep moving
10528 processSlot(mapper, FIRST_SLOT);
10529 processId(mapper, FIRST_TRACKING_ID);
10530 processPosition(mapper, x1 + 2, y1 + 2);
10531 processSync(mapper);
10532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10533 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10534
10535 // first finger up.
10536 processId(mapper, INVALID_TRACKING_ID);
10537 processSync(mapper);
10538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10539 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10540 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010541}
10542
Arthur Hung9ad18942021-06-19 02:04:46 +000010543/**
10544 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10545 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10546 * cause slot be valid again.
10547 */
10548TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10549 addConfigurationProperty("touch.deviceType", "touchScreen");
10550 prepareDisplay(DISPLAY_ORIENTATION_0);
10551 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10552 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10553
10554 NotifyMotionArgs motionArgs;
10555
10556 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10557 // First finger down.
10558 processId(mapper, FIRST_TRACKING_ID);
10559 processPosition(mapper, x1, y1);
10560 processPressure(mapper, RAW_PRESSURE_MAX);
10561 processSync(mapper);
10562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10563 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10564 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10565
10566 // First finger move.
10567 processId(mapper, FIRST_TRACKING_ID);
10568 processPosition(mapper, x1 + 1, y1 + 1);
10569 processPressure(mapper, RAW_PRESSURE_MAX);
10570 processSync(mapper);
10571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10572 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10573 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10574
10575 // Second finger down.
10576 processSlot(mapper, SECOND_SLOT);
10577 processId(mapper, SECOND_TRACKING_ID);
10578 processPosition(mapper, x2, y2);
10579 processPressure(mapper, RAW_PRESSURE_MAX);
10580 processSync(mapper);
10581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010582 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010583 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10584
10585 // second finger up with some unexpected data.
10586 processSlot(mapper, SECOND_SLOT);
10587 processId(mapper, INVALID_TRACKING_ID);
10588 processPosition(mapper, x2, y2);
10589 processSync(mapper);
10590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010591 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010592 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10593
10594 // first finger up with some unexpected data.
10595 processSlot(mapper, FIRST_SLOT);
10596 processId(mapper, INVALID_TRACKING_ID);
10597 processPosition(mapper, x2, y2);
10598 processPressure(mapper, RAW_PRESSURE_MAX);
10599 processSync(mapper);
10600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10601 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10602 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10603}
10604
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010605TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10606 addConfigurationProperty("touch.deviceType", "touchScreen");
10607 prepareDisplay(DISPLAY_ORIENTATION_0);
10608 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10609 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10610
10611 // First finger down.
10612 processId(mapper, FIRST_TRACKING_ID);
10613 processPosition(mapper, 100, 200);
10614 processPressure(mapper, RAW_PRESSURE_MAX);
10615 processSync(mapper);
10616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10617 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10618
10619 // Second finger down.
10620 processSlot(mapper, SECOND_SLOT);
10621 processId(mapper, SECOND_TRACKING_ID);
10622 processPosition(mapper, 300, 400);
10623 processPressure(mapper, RAW_PRESSURE_MAX);
10624 processSync(mapper);
10625 ASSERT_NO_FATAL_FAILURE(
10626 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10627
10628 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010629 // preserved. Resetting should cancel the ongoing gesture.
10630 resetMapper(mapper, ARBITRARY_TIME);
10631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10632 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010633
10634 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10635 // the existing touch state to generate a down event.
10636 processPosition(mapper, 301, 302);
10637 processSync(mapper);
10638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10639 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10641 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10642
10643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10644}
10645
10646TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10647 addConfigurationProperty("touch.deviceType", "touchScreen");
10648 prepareDisplay(DISPLAY_ORIENTATION_0);
10649 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10650 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10651
10652 // First finger touches down and releases.
10653 processId(mapper, FIRST_TRACKING_ID);
10654 processPosition(mapper, 100, 200);
10655 processPressure(mapper, RAW_PRESSURE_MAX);
10656 processSync(mapper);
10657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10658 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10659 processId(mapper, INVALID_TRACKING_ID);
10660 processSync(mapper);
10661 ASSERT_NO_FATAL_FAILURE(
10662 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10663
10664 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10665 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010666 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10668
10669 // Send an empty sync frame. Since there are no pointers, no events are generated.
10670 processSync(mapper);
10671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10672}
10673
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010674// --- MultiTouchInputMapperTest_ExternalDevice ---
10675
10676class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10677protected:
Chris Yea52ade12020-08-27 16:49:20 -070010678 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010679};
10680
10681/**
10682 * Expect fallback to internal viewport if device is external and external viewport is not present.
10683 */
10684TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10685 prepareAxes(POSITION);
10686 addConfigurationProperty("touch.deviceType", "touchScreen");
10687 prepareDisplay(DISPLAY_ORIENTATION_0);
10688 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10689
10690 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10691
10692 NotifyMotionArgs motionArgs;
10693
10694 // Expect the event to be sent to the internal viewport,
10695 // because an external viewport is not present.
10696 processPosition(mapper, 100, 100);
10697 processSync(mapper);
10698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10699 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10700
10701 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010702 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010703 processPosition(mapper, 100, 100);
10704 processSync(mapper);
10705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10706 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10707}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010708
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010709TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10710 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10711 std::shared_ptr<FakePointerController> fakePointerController =
10712 std::make_shared<FakePointerController>();
10713 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10714 fakePointerController->setPosition(0, 0);
10715 fakePointerController->setButtonState(0);
10716
10717 // prepare device and capture
10718 prepareDisplay(DISPLAY_ORIENTATION_0);
10719 prepareAxes(POSITION | ID | SLOT);
10720 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10721 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10722 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010723 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010724 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10725
10726 // captured touchpad should be a touchpad source
10727 NotifyDeviceResetArgs resetArgs;
10728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10729 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10730
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010731 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010732
10733 const InputDeviceInfo::MotionRange* relRangeX =
10734 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10735 ASSERT_NE(relRangeX, nullptr);
10736 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10737 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10738 const InputDeviceInfo::MotionRange* relRangeY =
10739 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10740 ASSERT_NE(relRangeY, nullptr);
10741 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10742 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10743
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010744 // run captured pointer tests - note that this is unscaled, so input listener events should be
10745 // identical to what the hardware sends (accounting for any
10746 // calibration).
10747 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010748 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010749 processId(mapper, 1);
10750 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10751 processKey(mapper, BTN_TOUCH, 1);
10752 processSync(mapper);
10753
10754 // expect coord[0] to contain initial location of touch 0
10755 NotifyMotionArgs args;
10756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10757 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10758 ASSERT_EQ(1U, args.pointerCount);
10759 ASSERT_EQ(0, args.pointerProperties[0].id);
10760 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10761 ASSERT_NO_FATAL_FAILURE(
10762 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10763
10764 // FINGER 1 DOWN
10765 processSlot(mapper, 1);
10766 processId(mapper, 2);
10767 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10768 processSync(mapper);
10769
10770 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010772 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010773 ASSERT_EQ(2U, args.pointerCount);
10774 ASSERT_EQ(0, args.pointerProperties[0].id);
10775 ASSERT_EQ(1, args.pointerProperties[1].id);
10776 ASSERT_NO_FATAL_FAILURE(
10777 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10778 ASSERT_NO_FATAL_FAILURE(
10779 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10780
10781 // FINGER 1 MOVE
10782 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10783 processSync(mapper);
10784
10785 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10786 // from move
10787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10788 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10789 ASSERT_NO_FATAL_FAILURE(
10790 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10791 ASSERT_NO_FATAL_FAILURE(
10792 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10793
10794 // FINGER 0 MOVE
10795 processSlot(mapper, 0);
10796 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10797 processSync(mapper);
10798
10799 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10801 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10802 ASSERT_NO_FATAL_FAILURE(
10803 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10804 ASSERT_NO_FATAL_FAILURE(
10805 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10806
10807 // BUTTON DOWN
10808 processKey(mapper, BTN_LEFT, 1);
10809 processSync(mapper);
10810
10811 // touchinputmapper design sends a move before button press
10812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10813 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10815 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10816
10817 // BUTTON UP
10818 processKey(mapper, BTN_LEFT, 0);
10819 processSync(mapper);
10820
10821 // touchinputmapper design sends a move after button release
10822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10823 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10825 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10826
10827 // FINGER 0 UP
10828 processId(mapper, -1);
10829 processSync(mapper);
10830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10831 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10832
10833 // FINGER 1 MOVE
10834 processSlot(mapper, 1);
10835 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10836 processSync(mapper);
10837
10838 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10840 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10841 ASSERT_EQ(1U, args.pointerCount);
10842 ASSERT_EQ(1, args.pointerProperties[0].id);
10843 ASSERT_NO_FATAL_FAILURE(
10844 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10845
10846 // FINGER 1 UP
10847 processId(mapper, -1);
10848 processKey(mapper, BTN_TOUCH, 0);
10849 processSync(mapper);
10850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10851 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10852
Harry Cutts16a24cc2022-10-26 15:22:19 +000010853 // A non captured touchpad should have a mouse and touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010854 mFakePolicy->setPointerCapture(false);
10855 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Harry Cutts16a24cc2022-10-26 15:22:19 +000010857 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010858}
10859
10860TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10861 std::shared_ptr<FakePointerController> fakePointerController =
10862 std::make_shared<FakePointerController>();
10863 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10864 fakePointerController->setPosition(0, 0);
10865 fakePointerController->setButtonState(0);
10866
10867 // prepare device and capture
10868 prepareDisplay(DISPLAY_ORIENTATION_0);
10869 prepareAxes(POSITION | ID | SLOT);
10870 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10871 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010872 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010873 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10874 // run uncaptured pointer tests - pushes out generic events
10875 // FINGER 0 DOWN
10876 processId(mapper, 3);
10877 processPosition(mapper, 100, 100);
10878 processKey(mapper, BTN_TOUCH, 1);
10879 processSync(mapper);
10880
10881 // start at (100,100), cursor should be at (0,0) * scale
10882 NotifyMotionArgs args;
10883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10884 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10885 ASSERT_NO_FATAL_FAILURE(
10886 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10887
10888 // FINGER 0 MOVE
10889 processPosition(mapper, 200, 200);
10890 processSync(mapper);
10891
10892 // compute scaling to help with touch position checking
10893 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10894 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10895 float scale =
10896 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10897
10898 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10900 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10902 0, 0, 0, 0, 0, 0, 0));
10903}
10904
10905TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10906 std::shared_ptr<FakePointerController> fakePointerController =
10907 std::make_shared<FakePointerController>();
10908
10909 prepareDisplay(DISPLAY_ORIENTATION_0);
10910 prepareAxes(POSITION | ID | SLOT);
10911 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010912 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010913 mFakePolicy->setPointerCapture(false);
10914 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10915
Harry Cutts16a24cc2022-10-26 15:22:19 +000010916 // An uncaptured touchpad should be a pointer device, with additional touchpad source.
10917 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010918
Harry Cutts16a24cc2022-10-26 15:22:19 +000010919 // A captured touchpad should just have a touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010920 mFakePolicy->setPointerCapture(true);
10921 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10922 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10923}
10924
HQ Liue6983c72022-04-19 22:14:56 +000010925class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10926protected:
10927 float mPointerMovementScale;
10928 float mPointerXZoomScale;
10929 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10930 addConfigurationProperty("touch.deviceType", "pointer");
10931 std::shared_ptr<FakePointerController> fakePointerController =
10932 std::make_shared<FakePointerController>();
10933 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10934 fakePointerController->setPosition(0, 0);
10935 fakePointerController->setButtonState(0);
10936 prepareDisplay(DISPLAY_ORIENTATION_0);
10937
10938 prepareAxes(POSITION);
10939 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10940 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10941 // needs to be disabled, and the pointer gesture needs to be enabled.
10942 mFakePolicy->setPointerCapture(false);
10943 mFakePolicy->setPointerGestureEnabled(true);
10944 mFakePolicy->setPointerController(fakePointerController);
10945
10946 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10947 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10948 mPointerMovementScale =
10949 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10950 mPointerXZoomScale =
10951 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10952 }
10953
10954 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10955 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10956 /*flat*/ 0,
10957 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10958 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10959 /*flat*/ 0,
10960 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10961 }
10962};
10963
10964/**
10965 * Two fingers down on a pointer mode touch pad. The width
10966 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10967 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10968 * be greater than the both value to be freeform gesture, so that after two
10969 * fingers start to move downwards, the gesture should be swipe.
10970 */
10971TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10972 // The min freeform gesture width is 25units/mm x 30mm = 750
10973 // which is greater than fraction of the diagnal length of the touchpad (349).
10974 // Thus, MaxSwipWidth is 750.
10975 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10976 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10977 NotifyMotionArgs motionArgs;
10978
10979 // Two fingers down at once.
10980 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10981 // Pointer's initial position is used the [0,0] coordinate.
10982 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10983
10984 processId(mapper, FIRST_TRACKING_ID);
10985 processPosition(mapper, x1, y1);
10986 processMTSync(mapper);
10987 processId(mapper, SECOND_TRACKING_ID);
10988 processPosition(mapper, x2, y2);
10989 processMTSync(mapper);
10990 processSync(mapper);
10991
10992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10993 ASSERT_EQ(1U, motionArgs.pointerCount);
10994 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10995 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010996 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010997 ASSERT_NO_FATAL_FAILURE(
10998 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10999
11000 // It should be recognized as a SWIPE gesture when two fingers start to move down,
11001 // that there should be 1 pointer.
11002 int32_t movingDistance = 200;
11003 y1 += movingDistance;
11004 y2 += movingDistance;
11005
11006 processId(mapper, FIRST_TRACKING_ID);
11007 processPosition(mapper, x1, y1);
11008 processMTSync(mapper);
11009 processId(mapper, SECOND_TRACKING_ID);
11010 processPosition(mapper, x2, y2);
11011 processMTSync(mapper);
11012 processSync(mapper);
11013
11014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11015 ASSERT_EQ(1U, motionArgs.pointerCount);
11016 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11017 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011018 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
11020 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11021 0, 0, 0, 0));
11022}
11023
11024/**
11025 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
11026 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
11027 * the touch pack diagnal length. Two fingers' distance must be greater than the both
11028 * value to be freeform gesture, so that after two fingers start to move downwards,
11029 * the gesture should be swipe.
11030 */
11031TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
11032 // The min freeform gesture width is 5units/mm x 30mm = 150
11033 // which is greater than fraction of the diagnal length of the touchpad (349).
11034 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
11035 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
11036 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11037 NotifyMotionArgs motionArgs;
11038
11039 // Two fingers down at once.
11040 // The two fingers are 250 units apart, expects the current gesture to be PRESS
11041 // Pointer's initial position is used the [0,0] coordinate.
11042 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
11043
11044 processId(mapper, FIRST_TRACKING_ID);
11045 processPosition(mapper, x1, y1);
11046 processMTSync(mapper);
11047 processId(mapper, SECOND_TRACKING_ID);
11048 processPosition(mapper, x2, y2);
11049 processMTSync(mapper);
11050 processSync(mapper);
11051
11052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11053 ASSERT_EQ(1U, motionArgs.pointerCount);
11054 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11055 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011056 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011057 ASSERT_NO_FATAL_FAILURE(
11058 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11059
11060 // It should be recognized as a SWIPE gesture when two fingers start to move down,
11061 // and there should be 1 pointer.
11062 int32_t movingDistance = 200;
11063 y1 += movingDistance;
11064 y2 += movingDistance;
11065
11066 processId(mapper, FIRST_TRACKING_ID);
11067 processPosition(mapper, x1, y1);
11068 processMTSync(mapper);
11069 processId(mapper, SECOND_TRACKING_ID);
11070 processPosition(mapper, x2, y2);
11071 processMTSync(mapper);
11072 processSync(mapper);
11073
11074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11075 ASSERT_EQ(1U, motionArgs.pointerCount);
11076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11077 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011078 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011079 // New coordinate is the scaled relative coordinate from the initial coordinate.
11080 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
11081 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11082 0, 0, 0, 0));
11083}
11084
11085/**
11086 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
11087 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
11088 * freeform gestures after two fingers start to move downwards.
11089 */
11090TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
11091 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
11092 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11093
11094 NotifyMotionArgs motionArgs;
11095
11096 // Two fingers down at once. Wider than the max swipe width.
11097 // The gesture is expected to be PRESS, then transformed to FREEFORM
11098 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
11099
11100 processId(mapper, FIRST_TRACKING_ID);
11101 processPosition(mapper, x1, y1);
11102 processMTSync(mapper);
11103 processId(mapper, SECOND_TRACKING_ID);
11104 processPosition(mapper, x2, y2);
11105 processMTSync(mapper);
11106 processSync(mapper);
11107
11108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11109 ASSERT_EQ(1U, motionArgs.pointerCount);
11110 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11111 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011112 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011113 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
11114 ASSERT_NO_FATAL_FAILURE(
11115 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11116
11117 int32_t movingDistance = 200;
11118
11119 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
11120 // then two down events for two pointers.
11121 y1 += movingDistance;
11122 y2 += movingDistance;
11123
11124 processId(mapper, FIRST_TRACKING_ID);
11125 processPosition(mapper, x1, y1);
11126 processMTSync(mapper);
11127 processId(mapper, SECOND_TRACKING_ID);
11128 processPosition(mapper, x2, y2);
11129 processMTSync(mapper);
11130 processSync(mapper);
11131
11132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11133 // The previous PRESS gesture is cancelled, because it is transformed to freeform
11134 ASSERT_EQ(1U, motionArgs.pointerCount);
11135 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
11136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11137 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
11138 ASSERT_EQ(1U, motionArgs.pointerCount);
11139 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11141 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011142 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011143 ASSERT_EQ(2U, motionArgs.pointerCount);
11144 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
11145 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011146 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011147 // Two pointers' scaled relative coordinates from their initial centroid.
11148 // Initial y coordinates are 0 as y1 and y2 have the same value.
11149 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
11150 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
11151 // When pointers move, the new coordinates equal to the initial coordinates plus
11152 // scaled moving distance.
11153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11154 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11155 0, 0, 0, 0));
11156 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11157 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11158 0, 0, 0, 0));
11159
11160 // Move two fingers down again, expect one MOVE motion event.
11161 y1 += movingDistance;
11162 y2 += movingDistance;
11163
11164 processId(mapper, FIRST_TRACKING_ID);
11165 processPosition(mapper, x1, y1);
11166 processMTSync(mapper);
11167 processId(mapper, SECOND_TRACKING_ID);
11168 processPosition(mapper, x2, y2);
11169 processMTSync(mapper);
11170 processSync(mapper);
11171
11172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11173 ASSERT_EQ(2U, motionArgs.pointerCount);
11174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11175 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011176 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11178 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11179 0, 0, 0, 0, 0));
11180 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11181 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11182 0, 0, 0, 0, 0));
11183}
11184
Harry Cutts39b7ca22022-10-05 15:55:48 +000011185TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
11186 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
11187 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11188 NotifyMotionArgs motionArgs;
11189
11190 // Place two fingers down.
11191 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
11192
11193 processId(mapper, FIRST_TRACKING_ID);
11194 processPosition(mapper, x1, y1);
11195 processMTSync(mapper);
11196 processId(mapper, SECOND_TRACKING_ID);
11197 processPosition(mapper, x2, y2);
11198 processMTSync(mapper);
11199 processSync(mapper);
11200
11201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11202 ASSERT_EQ(1U, motionArgs.pointerCount);
11203 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11204 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
11205 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
11206 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
11207
11208 // Move the two fingers down and to the left.
11209 int32_t movingDistance = 200;
11210 x1 -= movingDistance;
11211 y1 += movingDistance;
11212 x2 -= movingDistance;
11213 y2 += movingDistance;
11214
11215 processId(mapper, FIRST_TRACKING_ID);
11216 processPosition(mapper, x1, y1);
11217 processMTSync(mapper);
11218 processId(mapper, SECOND_TRACKING_ID);
11219 processPosition(mapper, x2, y2);
11220 processMTSync(mapper);
11221 processSync(mapper);
11222
11223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11224 ASSERT_EQ(1U, motionArgs.pointerCount);
11225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11226 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
11227 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
11228 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
11229}
11230
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011231TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
11232 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
11233 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
11234 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
11236
11237 // Start a stylus gesture.
11238 processKey(mapper, BTN_TOOL_PEN, 1);
11239 processId(mapper, FIRST_TRACKING_ID);
11240 processPosition(mapper, 100, 200);
11241 processSync(mapper);
11242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11243 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
11244 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11245 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11246 // TODO(b/257078296): Pointer mode generates extra event.
11247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11248 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11249 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11250 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11252
11253 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
11254 // gesture should be disabled.
11255 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
11256 viewport->isActive = false;
11257 mFakePolicy->updateViewport(*viewport);
11258 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
11259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11260 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11261 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11262 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11263 // TODO(b/257078296): Pointer mode generates extra event.
11264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11265 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11266 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11267 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11269}
11270
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011271// --- JoystickInputMapperTest ---
11272
11273class JoystickInputMapperTest : public InputMapperTest {
11274protected:
11275 static const int32_t RAW_X_MIN;
11276 static const int32_t RAW_X_MAX;
11277 static const int32_t RAW_Y_MIN;
11278 static const int32_t RAW_Y_MAX;
11279
11280 void SetUp() override {
11281 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
11282 }
11283 void prepareAxes() {
11284 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
11285 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
11286 }
11287
11288 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
11289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
11290 }
11291
11292 void processSync(JoystickInputMapper& mapper) {
11293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
11294 }
11295
11296 void prepareVirtualDisplay(int32_t orientation) {
11297 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
11298 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
11299 NO_PORT, ViewportType::VIRTUAL);
11300 }
11301};
11302
11303const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
11304const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
11305const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
11306const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
11307
11308TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
11309 prepareAxes();
11310 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
11311
11312 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
11313
11314 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
11315
11316 // Send an axis event
11317 processAxis(mapper, ABS_X, 100);
11318 processSync(mapper);
11319
11320 NotifyMotionArgs args;
11321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11322 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11323
11324 // Send another axis event
11325 processAxis(mapper, ABS_Y, 100);
11326 processSync(mapper);
11327
11328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11329 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11330}
11331
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011332// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011333
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011334class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011335protected:
11336 static const char* DEVICE_NAME;
11337 static const char* DEVICE_LOCATION;
11338 static const int32_t DEVICE_ID;
11339 static const int32_t DEVICE_GENERATION;
11340 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011341 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011342 static const int32_t EVENTHUB_ID;
11343
11344 std::shared_ptr<FakeEventHub> mFakeEventHub;
11345 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011346 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011347 std::unique_ptr<InstrumentedInputReader> mReader;
11348 std::shared_ptr<InputDevice> mDevice;
11349
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011350 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011351 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011352 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011353 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011354 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011355 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011356 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11357 }
11358
11359 void SetUp() override { SetUp(DEVICE_CLASSES); }
11360
11361 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011362 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011363 mFakePolicy.clear();
11364 }
11365
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070011366 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011367 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
11368 mReader->requestRefreshConfiguration(changes);
11369 mReader->loopOnce();
11370 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070011371 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011372 }
11373
11374 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11375 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011376 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011377 InputDeviceIdentifier identifier;
11378 identifier.name = name;
11379 identifier.location = location;
11380 std::shared_ptr<InputDevice> device =
11381 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11382 identifier);
11383 mReader->pushNextDevice(device);
11384 mFakeEventHub->addDevice(eventHubId, name, classes);
11385 mReader->loopOnce();
11386 return device;
11387 }
11388
11389 template <class T, typename... Args>
11390 T& addControllerAndConfigure(Args... args) {
11391 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11392
11393 return controller;
11394 }
11395};
11396
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011397const char* PeripheralControllerTest::DEVICE_NAME = "device";
11398const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11399const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11400const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11401const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011402const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11403 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011404const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011405
11406// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011407class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011408protected:
11409 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011410 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011411 }
11412};
11413
11414TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011415 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011416
11417 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
11418 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
11419}
11420
11421TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011422 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011423
11424 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
11425 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
11426}
11427
11428// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011429class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011430protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011431 void SetUp() override {
11432 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11433 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011434};
11435
Chris Ye85758332021-05-16 23:05:17 -070011436TEST_F(LightControllerTest, MonoLight) {
11437 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011438 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011439 .maxBrightness = 255,
11440 .flags = InputLightClass::BRIGHTNESS,
11441 .path = ""};
11442 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011443
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011444 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011445 InputDeviceInfo info;
11446 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011447 std::vector<InputDeviceLightInfo> lights = info.getLights();
11448 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011449 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11450 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11451
11452 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11453 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11454}
11455
11456TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11457 RawLightInfo infoMono = {.id = 1,
11458 .name = "mono_keyboard_backlight",
11459 .maxBrightness = 255,
11460 .flags = InputLightClass::BRIGHTNESS |
11461 InputLightClass::KEYBOARD_BACKLIGHT,
11462 .path = ""};
11463 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11464
11465 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11466 InputDeviceInfo info;
11467 controller.populateDeviceInfo(&info);
11468 std::vector<InputDeviceLightInfo> lights = info.getLights();
11469 ASSERT_EQ(1U, lights.size());
11470 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11471 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011472
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011473 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11474 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011475}
11476
11477TEST_F(LightControllerTest, RGBLight) {
11478 RawLightInfo infoRed = {.id = 1,
11479 .name = "red",
11480 .maxBrightness = 255,
11481 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11482 .path = ""};
11483 RawLightInfo infoGreen = {.id = 2,
11484 .name = "green",
11485 .maxBrightness = 255,
11486 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11487 .path = ""};
11488 RawLightInfo infoBlue = {.id = 3,
11489 .name = "blue",
11490 .maxBrightness = 255,
11491 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11492 .path = ""};
11493 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11494 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11495 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11496
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011497 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011498 InputDeviceInfo info;
11499 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011500 std::vector<InputDeviceLightInfo> lights = info.getLights();
11501 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011502 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11503 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11504 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11505
11506 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11507 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11508}
11509
11510TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11511 RawLightInfo infoRed = {.id = 1,
11512 .name = "red_keyboard_backlight",
11513 .maxBrightness = 255,
11514 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11515 InputLightClass::KEYBOARD_BACKLIGHT,
11516 .path = ""};
11517 RawLightInfo infoGreen = {.id = 2,
11518 .name = "green_keyboard_backlight",
11519 .maxBrightness = 255,
11520 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11521 InputLightClass::KEYBOARD_BACKLIGHT,
11522 .path = ""};
11523 RawLightInfo infoBlue = {.id = 3,
11524 .name = "blue_keyboard_backlight",
11525 .maxBrightness = 255,
11526 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11527 InputLightClass::KEYBOARD_BACKLIGHT,
11528 .path = ""};
11529 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11530 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11531 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11532
11533 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11534 InputDeviceInfo info;
11535 controller.populateDeviceInfo(&info);
11536 std::vector<InputDeviceLightInfo> lights = info.getLights();
11537 ASSERT_EQ(1U, lights.size());
11538 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11539 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11540 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11541
11542 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11543 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11544}
11545
11546TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11547 RawLightInfo infoRed = {.id = 1,
11548 .name = "red",
11549 .maxBrightness = 255,
11550 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11551 .path = ""};
11552 RawLightInfo infoGreen = {.id = 2,
11553 .name = "green",
11554 .maxBrightness = 255,
11555 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11556 .path = ""};
11557 RawLightInfo infoBlue = {.id = 3,
11558 .name = "blue",
11559 .maxBrightness = 255,
11560 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11561 .path = ""};
11562 RawLightInfo infoGlobal = {.id = 3,
11563 .name = "global_keyboard_backlight",
11564 .maxBrightness = 255,
11565 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11566 InputLightClass::KEYBOARD_BACKLIGHT,
11567 .path = ""};
11568 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11569 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11570 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11571 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11572
11573 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11574 InputDeviceInfo info;
11575 controller.populateDeviceInfo(&info);
11576 std::vector<InputDeviceLightInfo> lights = info.getLights();
11577 ASSERT_EQ(1U, lights.size());
11578 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11579 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11580 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011581
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011582 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11583 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011584}
11585
11586TEST_F(LightControllerTest, MultiColorRGBLight) {
11587 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011588 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011589 .maxBrightness = 255,
11590 .flags = InputLightClass::BRIGHTNESS |
11591 InputLightClass::MULTI_INTENSITY |
11592 InputLightClass::MULTI_INDEX,
11593 .path = ""};
11594
11595 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11596
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011597 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011598 InputDeviceInfo info;
11599 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011600 std::vector<InputDeviceLightInfo> lights = info.getLights();
11601 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011602 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11603 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11604 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11605
11606 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11607 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11608}
11609
11610TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11611 RawLightInfo infoColor = {.id = 1,
11612 .name = "multi_color_keyboard_backlight",
11613 .maxBrightness = 255,
11614 .flags = InputLightClass::BRIGHTNESS |
11615 InputLightClass::MULTI_INTENSITY |
11616 InputLightClass::MULTI_INDEX |
11617 InputLightClass::KEYBOARD_BACKLIGHT,
11618 .path = ""};
11619
11620 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11621
11622 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11623 InputDeviceInfo info;
11624 controller.populateDeviceInfo(&info);
11625 std::vector<InputDeviceLightInfo> lights = info.getLights();
11626 ASSERT_EQ(1U, lights.size());
11627 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11628 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11629 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011630
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011631 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11632 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011633}
11634
11635TEST_F(LightControllerTest, PlayerIdLight) {
11636 RawLightInfo info1 = {.id = 1,
11637 .name = "player1",
11638 .maxBrightness = 255,
11639 .flags = InputLightClass::BRIGHTNESS,
11640 .path = ""};
11641 RawLightInfo info2 = {.id = 2,
11642 .name = "player2",
11643 .maxBrightness = 255,
11644 .flags = InputLightClass::BRIGHTNESS,
11645 .path = ""};
11646 RawLightInfo info3 = {.id = 3,
11647 .name = "player3",
11648 .maxBrightness = 255,
11649 .flags = InputLightClass::BRIGHTNESS,
11650 .path = ""};
11651 RawLightInfo info4 = {.id = 4,
11652 .name = "player4",
11653 .maxBrightness = 255,
11654 .flags = InputLightClass::BRIGHTNESS,
11655 .path = ""};
11656 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11657 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11658 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11659 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11660
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011661 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011662 InputDeviceInfo info;
11663 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011664 std::vector<InputDeviceLightInfo> lights = info.getLights();
11665 ASSERT_EQ(1U, lights.size());
11666 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011667 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11668 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011669
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011670 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11671 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11672 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011673}
11674
Michael Wrightd02c5b62014-02-10 15:10:22 -080011675} // namespace android